C#C
C#7mo ago
TeBeCo

✅ [Challenge / Help] Sanitizing C# before serialization with STJ:

Here's a minimal reproducable UnitTest for people that want to run it:
public class TestObject
{
    public string NonEmptyString { get; set; } = nameof(NonEmptyString);

    public string TrimmableString { get; set; } = $"{nameof(TrimmableString)}        ";

    public string NonTruncatableString { get; set; } = "1234567890123456789012345678901234567890";

    public string EmptyString { get; set; } = "";

    public string? NullString { get; set; } = null;

    public string WhiteSpaceString { get; set; } = "           ";

    public string[] EmptyList { get; set; } = [];

    public string?[] ListWithNullOrWhitespaceString { get; set; } = ["", "   ", null];

    public string?[] ListWithMixedString { get; set; } = ["", "   ", null, "TrimmableEntry        ", "NonTrimmableEntry", "1234567890123456789012345678901234567890"];

    public List<TestObject> ListWithObjects { get; set; } = [];
}

public class JsonSerializerTests
{
    [Fact]
    public void Test()
    {
        var jsonSerializerOptions = new JsonSerializerOptions()
        {
            WriteIndented = true,
            Converters =
            {
                // ADD HERE LIST OF CONVERTERS
            },
            TypeInfoResolver = new DefaultJsonTypeInfoResolver
            {
                Modifiers =
                {
                    // ADD HERE LIST OF MODIFIERS
                }
            }
        };

        var testObject = new TestObject
        {
            ListWithObjects = new List<TestObject>
            {
                new TestObject() { ListWithObjects = []},
            }
        };

        var json = JsonSerializer.Serialize(testObject, jsonSerializerOptions);

        Assert.Equal("""
            {
              "NonEmptyString": "NonEmptyString",
              "TrimmableString": "TrimmableString",
              "NonTruncatableString": "1234567890123456789012345678901234567890",
              "ListWithMixedString": [
                "TrimmableEntry",
                "NonTrimmableEntry",
                "12345678901234567890123456789012..."
              ],
              "ListWithObjects": [
                {
                  "NonEmptyString": "NonEmptyString",
                  "TrimmableString": "TrimmableString",
                  "NonTruncatableString": "1234567890123456789012345678901234567890",
                  "ListWithMixedString": [
                    "TrimmableEntry",
                    "NonTrimmableEntry",
                    "12345678901234567890123456789012..."
                  ]
                }
              ]
            }
            """,
            json);
    }
}

rules:
  • null object should not be serialized
  • string should be trimmed (not to confused with truncated)
  • null / empty / whitespace string should not be serialized
  • empty array should not be serialized
  • if the array is an array of string:
    • all string inside should be sanitized with rules above
    • if no string is left then the array is empty, then the entire array should not be serialized
  • Truncate magic rule: if the array of string cotnains string longer than 35 (arbitrary), then the string should be truncated with ... to be exactly 35 char length
  • The object contains JsonElement (JSON Dom for STJ) and the code should properly traverse it as well
Was this page helpful?