Skip to content

Commit 3143a53

Browse files
committed
Handling of Nullable<T> should be done by System.Text.Json
1 parent df3dbaa commit 3143a53

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>7.2.1</VersionPrefix>
5+
<VersionPrefix>7.2.2</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

src/Thinktecture.Runtime.Extensions.Json/Text/Json/Serialization/ValueObjectJsonConverterFactory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public sealed class ValueObjectJsonConverterFactory : JsonConverterFactory
6060
/// <inheritdoc />
6161
public override bool CanConvert(Type typeToConvert)
6262
{
63+
// Handling of Nullable<T> should be done by System.Text.Json
64+
if (typeToConvert.IsValueType && Nullable.GetUnderlyingType(typeToConvert) is not null)
65+
return false;
66+
6367
return KeyedValueObjectMetadataLookup.Find(typeToConvert) is not null
6468
|| typeToConvert.GetCustomAttributes<ValueObjectFactoryAttribute>().Any(a => a.UseForSerialization.HasFlag(SerializationFrameworks.SystemTextJson));
6569
}

test/Thinktecture.Runtime.Extensions.Json.Tests/Text/Json/Serialization/ValueObjectJsonConverterFactoryTests/RoundTrip.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ public void Should_deserialize_decimal_from_string_with_corresponding_NumberHand
200200
public void Should_roundtrip_serialize_dictionary_with_string_based_enum_key()
201201
{
202202
var dictionary = new Dictionary<TestSmartEnum_Class_StringBased, int>
203-
{
204-
{ TestSmartEnum_Class_StringBased.Value1, 1 },
205-
{ TestSmartEnum_Class_StringBased.Value2, 2 }
206-
};
203+
{
204+
{ TestSmartEnum_Class_StringBased.Value1, 1 },
205+
{ TestSmartEnum_Class_StringBased.Value2, 2 }
206+
};
207207

208208
var options = new JsonSerializerOptions { Converters = { new ValueObjectJsonConverterFactory() } };
209209

@@ -212,4 +212,37 @@ public void Should_roundtrip_serialize_dictionary_with_string_based_enum_key()
212212

213213
dictionary.Should().BeEquivalentTo(deserializedDictionary);
214214
}
215+
216+
public static IEnumerable<object[]> ObjectWithStructTestData =
217+
[
218+
[new { Prop = IntBasedStructValueObject.Create(42) }, """{"Prop":42}"""],
219+
[new { Prop = (IntBasedStructValueObject?)IntBasedStructValueObject.Create(42) }, """{"Prop":42}"""],
220+
[new { Prop = IntBasedReferenceValueObject.Create(42) }, """{"Prop":42}"""],
221+
[new TestStruct<IntBasedStructValueObject>(IntBasedStructValueObject.Create(42)), """{"Prop":42}"""],
222+
[new TestStruct<IntBasedStructValueObject?>(IntBasedStructValueObject.Create(42)), """{"Prop":42}"""],
223+
[new TestStruct<IntBasedReferenceValueObject>(IntBasedReferenceValueObject.Create(42)), """{"Prop":42}"""],
224+
];
225+
226+
[Theory]
227+
[MemberData(nameof(ObjectWithStructTestData))]
228+
public void Should_roundtrip_serialize_types_with_struct_properties_using_non_generic_factory(object obj, string expectedJson)
229+
{
230+
var options = new JsonSerializerOptions { Converters = { new ValueObjectJsonConverterFactory() } };
231+
232+
var json = JsonSerializer.Serialize(obj, options);
233+
json.Should().Be(expectedJson);
234+
235+
var deserializedObj = JsonSerializer.Deserialize(json, obj.GetType(), options);
236+
obj.Should().BeEquivalentTo(deserializedObj);
237+
}
238+
239+
private struct TestStruct<T>
240+
{
241+
public T Prop { get; set; }
242+
243+
public TestStruct(T prop)
244+
{
245+
Prop = prop;
246+
}
247+
}
215248
}

0 commit comments

Comments
 (0)