public static void RoundtripValues <T>(string expectedValueAsString, T value) { JsonSerializerOptions options = new() { Converters = { new Int128Converter(), new UInt128Converter(), new HalfConverter(), new BigIntegerConverter() } }; ClassWithProperty <T> wrappedValue = new() { Property = value }; string json = JsonSerializer.Serialize(value, options); Assert.Equal(expectedValueAsString, json); T deserializedValue = JsonSerializer.Deserialize <T>(json, options); Assert.Equal(value, deserializedValue); json = JsonSerializer.Serialize(wrappedValue, options); Assert.Equal($"{{\"Property\":{expectedValueAsString}}}", json); ClassWithProperty <T> deserializedWrappedValue = JsonSerializer.Deserialize <ClassWithProperty <T> >(json, options); Assert.Equal(wrappedValue.Property, deserializedWrappedValue.Property); }
public static void CustomArrayConverterInProperty() { const string json = @"{""Array1"":""1,2,3"",""Array2"":""4,5""}"; var options = new JsonSerializerOptions(); options.Converters.Add(new LongArrayConverter()); ClassWithProperty obj = JsonSerializer.Deserialize <ClassWithProperty>(json, options); Assert.Equal(1, obj.Array1[0]); Assert.Equal(2, obj.Array1[1]); Assert.Equal(3, obj.Array1[2]); Assert.Equal(4, obj.Array2[0]); Assert.Equal(5, obj.Array2[1]); string jsonSerialized = JsonSerializer.Serialize(obj, options); Assert.Equal(json, jsonSerialized); }