Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Enum-Dictionary-Keys (analogous to Newtonsoft) #2825

Merged
merged 8 commits into from
Apr 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,24 @@ public DataContract GetDataContractForType(Type type)

if (IsSupportedDictionary(type, out Type keyType, out Type valueType))
{
IEnumerable<string> keys = null;

if (keyType.IsEnum)
{
// This is a special case where we know the possible key values
var enumValuesAsJson = keyType.GetEnumValues()
.Cast<object>()
.Select(value => JsonConverterFunc(value));

keys = enumValuesAsJson.Any(json => json.StartsWith("\""))
? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty))
: keyType.GetEnumNames();
}

return DataContract.ForDictionary(
underlyingType: type,
valueType: valueType,
keys: null, // STJ doesn't currently support dictionaries with enum key types
keys: keys,
jsonConverter: JsonConverterFunc);
}

Expand Down