Marco Franssen Marco Franssen - 5 months ago 43
JSON Question

Howto decorate JSON.NET StringEnumConverter

I'm consuming an api which returns string values like this.

some-enum-value


I try to put these values in an enum, since the default StringEnumConverter doesn't do the job I try to decorate this Converter with some additional logic. How can I make sure the values are correctly deserialized?

Following code is my tryout to get this job done. However the line
reader = new JsonTextReader(new StringReader(cleaned));
is breaking the whole thing since the base.ReadJson cant recognize the string as JSON.

Is there a better way of doing this without having to implement all the excisting logic in StringEnumConverter? How to fix my approach?

public class BkStringEnumConverter : StringEnumConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
var enumString = reader.Value.ToString();
if (enumString.Contains("-"))
{
var cleaned = enumString.Split('-').Select(FirstToUpper).Aggregate((a, b) => a + b);
reader = new JsonTextReader(new StringReader(cleaned));
}
}
return base.ReadJson(reader, objectType, existingValue, serializer);
}

private static string FirstToUpper(string input)
{
var firstLetter = input.ToCharArray().First().ToString().ToUpper();
return string.IsNullOrEmpty(input)
? input
: firstLetter + string.Join("", input.ToCharArray().Skip(1));
}
}

Answer

I solved the issue by adding EnumMember attributes on my enum values. The Json.NET default StringEnumConverter perfectly deals with these attributes.

Example:

public enum MyEnum
{
    [EnumMember("some-enum-value")]
    SomeEnumValue,
    Value,
    [EnumMember("some-other-value")]
    SomeOtherValue
}

Please note that you only have to specify the attributes in case of dashes or other special chars you can't use in your enum. The uppercase lowercase is dealt with by the StringEnumConverter. So if the service returns a value like someenumvalue you should use it like this in the enum Someenumvalue. If you prefer SomeEnumValue you should use the EnumMember attribute. In case the service returns it like this someEnumValue you can just use it like this SomeEnumValue (It works out of the box when you use the CamelCaseText property).

You can easily specify your converters and other settings in the JsonSerializerSettings.

Here is an example of the settings I use myself.

new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    Converters = new List<JsonConverter> { new StringEnumConverter { CamelCaseText = true } },
    NullValueHandling = NullValueHandling.Ignore
};