public static void NullableCustomValueTypeUsingAttributes() { { TestStructClass myStructClass = JsonSerializer.Deserialize <TestStructClass>(@"{""MyStruct"":null}"); Assert.False(myStructClass.MyStruct.HasValue); } { TestStructClass myStructClass = JsonSerializer.Deserialize <TestStructClass>(@"{""MyStruct"":1}"); Assert.True(myStructClass.MyStruct.HasValue); Assert.Equal(1, myStructClass.MyStruct.Value.InnerValue); } }
public static void NullableCustomValueTypeChoosesAttributeOverOptions() { var options = new JsonSerializerOptions(); options.Converters.Add(new JsonTestStructThrowingConverter()); // Chooses JsonTestStructThrowingConverter on options, which will throw. Assert.Throws <NotSupportedException>(() => JsonSerializer.Deserialize <TestStruct?>("1", options)); // Chooses JsonTestStructConverter on attribute, which will not throw. TestStructClass myStructClass = JsonSerializer.Deserialize <TestStructClass>(@"{""MyStruct"":null}", options); Assert.False(myStructClass.MyStruct.HasValue); }