Skip to content

Commit

Permalink
Add support for Dictionary types for Newtonsoft Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Apr 13, 2024
1 parent 5de599a commit c7e2e5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public DataContract GetDataContractForType(Type type)
jsonConverter: JsonConverterFunc);
}

if (JsonSerializerDataContractResolver.IsSupportedDictionary(type, out Type _, out Type valueType1))
{
return DataContract.ForDictionary(
underlyingType: type,
valueType: valueType1,
keys: null, // STJ doesn't currently support dictionaries with enum key types
jsonConverter: JsonConverterFunc);
}

if (JsonSerializerDataContractResolver.IsSupportedCollection(type, out Type itemType))
{
return DataContract.ForArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private string JsonConverterFunc(object value)
return JsonSerializer.Serialize(value, _serializerOptions);
}

public bool IsSupportedDictionary(Type type, out Type keyType, out Type valueType)
public static bool IsSupportedDictionary(Type type, out Type keyType, out Type valueType)
{
if (type.IsConstructedFrom(typeof(IDictionary<,>), out Type constructedType)
|| type.IsConstructedFrom(typeof(IReadOnlyDictionary<,>), out constructedType))
Expand Down

0 comments on commit c7e2e5f

Please sign in to comment.