Refactor ArgumentJsonConverter to improve type alias handling
Introduced logic to handle type aliases more accurately for arrays and collections. Enhanced the final type alias determination by accommodating cases where type aliases are present. Cleaned up unnecessary whitespace for better code readability.
This commit is contained in:
parent
cf2ac05e92
commit
babf1125ff
|
|
@ -20,13 +20,13 @@ public class ArgumentJsonConverter : JsonConverter<ArgumentDefinition>
|
|||
{
|
||||
_wellKnownTypeRegistry = wellKnownTypeRegistry;
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, ArgumentDefinition value, JsonSerializerOptions options)
|
||||
{
|
||||
var newOptions = new JsonSerializerOptions(options);
|
||||
newOptions.Converters.RemoveWhere(x => x is ArgumentJsonConverterFactory);
|
||||
|
||||
|
||||
var jsonObject = (JsonObject)JsonSerializer.SerializeToNode(value, value.GetType(), newOptions)!;
|
||||
var typeName = value.Type;
|
||||
var typeAlias = _wellKnownTypeRegistry.TryGetAlias(typeName, out var alias) ? alias : null;
|
||||
|
|
@ -34,11 +34,12 @@ public class ArgumentJsonConverter : JsonConverter<ArgumentDefinition>
|
|||
var isCollection = typeName.IsCollectionType();
|
||||
var elementTypeName = isArray ? typeName.GetElementType() : isCollection ? typeName.GenericTypeArguments[0] : typeName;
|
||||
var elementTypeAlias = _wellKnownTypeRegistry.GetAliasOrDefault(elementTypeName);
|
||||
var finalTypeAlias = isArray || isCollection ? elementTypeAlias : typeAlias;
|
||||
|
||||
if(isArray) jsonObject["isArray"] = isArray;
|
||||
if(isCollection) jsonObject["isCollection"] = isCollection;
|
||||
|
||||
var isAliasedArray = (isArray || isCollection) && typeAlias != null;
|
||||
var finalTypeAlias = isArray || isCollection ? typeAlias ?? elementTypeAlias : elementTypeAlias;
|
||||
|
||||
if (isArray && !isAliasedArray) jsonObject["isArray"] = isArray;
|
||||
if (isCollection) jsonObject["isCollection"] = isCollection;
|
||||
|
||||
jsonObject["type"] = finalTypeAlias;
|
||||
JsonSerializer.Serialize(writer, jsonObject, newOptions);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue