Handle collection and array serialization in TypeJsonConverter
Refactored TypeJsonConverter to distinguish and properly handle serialization of arrays and generic collections. Updated integration tests to include cases for round-tripping primitive arrays and collections for improved coverage.
This commit is contained in:
parent
60db7cb964
commit
b2331f73db
|
|
@ -31,13 +31,21 @@ public class TypeJsonConverter : JsonConverter<Type>
|
|||
{
|
||||
var typeAlias = reader.GetString()!;
|
||||
|
||||
// Handle collection types.
|
||||
// Handle array types.
|
||||
if (typeAlias.EndsWith("[]"))
|
||||
{
|
||||
var elementTypeAlias = typeAlias[..^"[]".Length];
|
||||
var elementTypeAlias = typeAlias[..^2];
|
||||
var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementTypeAlias)!;
|
||||
return elementType.MakeArrayType();
|
||||
}
|
||||
|
||||
// Handle collection types.
|
||||
if (typeAlias.EndsWith("()"))
|
||||
{
|
||||
var elementTypeAlias = typeAlias[..^"()".Length];
|
||||
var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementTypeAlias)!;
|
||||
return typeof(List<>).MakeGenericType(elementType);
|
||||
}
|
||||
|
||||
return _wellKnownTypeRegistry.TryGetType(typeAlias, out var type) ? type : Type.GetType(typeAlias);
|
||||
}
|
||||
|
|
@ -45,6 +53,15 @@ public class TypeJsonConverter : JsonConverter<Type>
|
|||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options)
|
||||
{
|
||||
// Handle array types.
|
||||
if (value.IsArray)
|
||||
{
|
||||
var elementType = value.GetElementType()!;
|
||||
var elementTypeAlias = _wellKnownTypeRegistry.TryGetAlias(elementType, out var elementTypeAliasValue) ? elementTypeAliasValue : elementType.GetSimpleAssemblyQualifiedName();
|
||||
writer.WriteStringValue($"{elementTypeAlias}[]");
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle collection types.
|
||||
if (value is { IsGenericType: true, GenericTypeArguments.Length: 1 })
|
||||
{
|
||||
|
|
@ -53,7 +70,7 @@ public class TypeJsonConverter : JsonConverter<Type>
|
|||
|
||||
if (typedEnumerable.IsAssignableFrom(value) && _wellKnownTypeRegistry.TryGetAlias(elementType, out var elementTypeAlias))
|
||||
{
|
||||
writer.WriteStringValue($"{elementTypeAlias}[]");
|
||||
writer.WriteStringValue($"{elementTypeAlias}()");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,7 +74,8 @@ public class SerializationTests(ITestOutputHelper testOutputHelper)
|
|||
{
|
||||
var dict = new Dictionary<string, object>
|
||||
{
|
||||
{ "Content", new List<TestObject>()
|
||||
{
|
||||
"Content", new List<TestObject>()
|
||||
{
|
||||
new()
|
||||
{
|
||||
|
|
@ -94,9 +95,10 @@ public class SerializationTests(ITestOutputHelper testOutputHelper)
|
|||
{
|
||||
var dict = new Dictionary<string, object>
|
||||
{
|
||||
{ "Content", new List<Guid>
|
||||
{
|
||||
"Content", new List<Guid>
|
||||
{
|
||||
Guid.NewGuid()
|
||||
Guid.NewGuid()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -106,6 +108,24 @@ public class SerializationTests(ITestOutputHelper testOutputHelper)
|
|||
Assert.Equal(typeof(List<Guid>), result.GetType());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoundtripPrimitiveArrays()
|
||||
{
|
||||
var dict = new Dictionary<string, object>
|
||||
{
|
||||
{
|
||||
"Content", new[]
|
||||
{
|
||||
Guid.NewGuid()
|
||||
}
|
||||
}
|
||||
};
|
||||
var jsonSerialized = SerializeUsingPayloadSerializer(dict);
|
||||
var transformationModel = DeSerializeDictionaryUsingPayloadSerializer(jsonSerialized);
|
||||
var result = transformationModel["Content"];
|
||||
Assert.Equal(typeof(Guid[]), result.GetType());
|
||||
}
|
||||
|
||||
private string SerializeUsingPayloadSerializer(object obj)
|
||||
{
|
||||
var payloadSerializer = _services.GetRequiredService<IPayloadSerializer>();
|
||||
|
|
@ -134,10 +154,11 @@ public class SerializationTests(ITestOutputHelper testOutputHelper)
|
|||
|
||||
var dict = new Dictionary<string, object>
|
||||
{
|
||||
{ "StatusCode", "Created" },
|
||||
{ "Content",isArray ?
|
||||
(type == typeof(JArray)? JArray.Parse(jsonContent):JsonArray.Parse(jsonContent)):
|
||||
(type == typeof(JObject)? JObject.Parse(jsonContent):JsonObject.Parse(jsonContent))
|
||||
{
|
||||
"StatusCode", "Created"
|
||||
},
|
||||
{
|
||||
"Content", isArray ? (type == typeof(JArray) ? JArray.Parse(jsonContent) : JsonArray.Parse(jsonContent)) : (type == typeof(JObject) ? JObject.Parse(jsonContent) : JsonObject.Parse(jsonContent))
|
||||
}
|
||||
};
|
||||
return dict;
|
||||
|
|
|
|||
Loading…
Reference in a new issue