feat(commons): native-typed JSON for List values; Decode reads both forms

This commit is contained in:
Joseph Doherty
2026-06-16 17:32:40 -04:00
parent 69f7c526d0
commit 180d55482b
2 changed files with 63 additions and 10 deletions
@@ -64,7 +64,7 @@ public class AttributeValueCodecTests
try
{
CultureInfo.CurrentCulture = new CultureInfo("de-DE");
Assert.Equal("[\"1.5\",\"2.5\"]",
Assert.Equal("[1.5,2.5]",
AttributeValueCodec.Encode(new List<double> { 1.5, 2.5 }));
}
finally
@@ -73,6 +73,53 @@ public class AttributeValueCodecTests
}
}
[Fact]
public void Encode_Int32List_ProducesNativeNumbers() =>
Assert.Equal("[10,20,30]",
AttributeValueCodec.Encode(new List<int> { 10, 20, 30 }));
[Fact]
public void Encode_BoolList_ProducesNativeBooleans() =>
Assert.Equal("[true,false]",
AttributeValueCodec.Encode(new List<bool> { true, false }));
[Fact]
public void Encode_StringList_StaysQuoted() =>
Assert.Equal("[\"a\",\"b\"]",
AttributeValueCodec.Encode(new List<string> { "a", "b" }));
[Fact]
public void Encode_DateTimeList_IsIso8601()
{
var json = AttributeValueCodec.Encode(
new List<DateTime> { new(2026, 6, 16, 0, 0, 0, DateTimeKind.Utc) });
Assert.Contains("2026-06-16T00:00:00", json);
Assert.DoesNotContain("06/16/2026", json);
}
[Fact]
public void Decode_NewNativeIntForm_Parses()
{
var back = (IList<int>)AttributeValueCodec.Decode("[10,20]", DataType.List, DataType.Int32)!;
Assert.Equal(new[] { 10, 20 }, back);
}
[Fact]
public void Decode_OldStringIntForm_BackwardCompatible()
{
var back = (IList<int>)AttributeValueCodec.Decode("[\"10\",\"20\"]", DataType.List, DataType.Int32)!;
Assert.Equal(new[] { 10, 20 }, back);
}
[Theory]
[InlineData("[true,false]")]
[InlineData("[\"True\",\"False\"]")]
public void Decode_BoolForms_BothParse(string json)
{
var back = (IList<bool>)AttributeValueCodec.Decode(json, DataType.List, DataType.Boolean)!;
Assert.Equal(new[] { true, false }, back);
}
[Fact]
public void RoundTrip_Int32List()
{