I have an enum:
public enum Animal { Dog, Cat, BlackBear }
I need to send it to a third-party API. This API requires that the enum values I send be lower case and occasionally require underscores. In general, the names they require don't match the enum naming convention I use.
Using the example provided at https://gooddevbaddev.wordpress.com/2013/08/26/deserializing-c-enums-using-json-net/, I tried to use a custom JsonConverter:
public class AnimalConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var animal = (Animal)value; switch (animal) { case Animal.Dog: { writer.WriteValue("dog"); break; } case Animal.Cat: { writer.WriteValue("cat"); break; } case Animal.BlackBear: { writer.WriteValue("black_bear"); break; } } } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var enumString = (string)reader.Value; Animal? animal = null; switch (enumString) { case "cat": { animal = Animal.Cat; break; } case "dog": { animal = Animal.Dog; break; } case "black_bear": { animal = Animal.BlackBear; break; } } } public override bool CanConvert(Type objectType) { return objectType == typeof(string); } }
Back in the properties of a class, I put the attributes on the Animal as so:
[JsonProperty("animal")] [JsonConverter(typeof(AnimalConverter))] public Animal ZooAnimals { get; set; }
When I run the program though, it seems to completely ignore the JsonConverter and rather than seeing expected values like "black_bear" or "dog", I see "BlackBear" and "Dog". How can I get the JsonConverter to actually do the conversion from the name of the enum value to the string I specify to replace that value with?
Thanks!
All you have to do is create a static method annotated with @JsonCreator in your enum. This should accept a parameter (the enum value) and return the corresponding enum. This method overrides the default mapping of Enum name to a json attribute .
In C#, JSON serialization very often needs to deal with enum objects. By default, enums are serialized in their integer form. This often causes a lack of interoperability with consumer applications because they need prior knowledge of what those numbers actually mean.
We can convert an enum to string by calling the ToString() method of an Enum.
Specifies the settings on a JsonSerializer object. Newtonsoft.Json. JsonSerializerSettings. Namespace: Newtonsoft.Json.
You don't need to write your own converter. Json.NET's StringEnumConverter
will read the EnumMember
attribute. If you change your enum
to this, it will serialize from and to the values you want.
[JsonConverter(typeof(StringEnumConverter))] public enum Animals { [EnumMember(Value = "dog")] Dog, [EnumMember(Value = "cat")] Cat, [EnumMember(Value = "black_bear")] BlackBear }
(As a minor note, since Animals
isn't a flags enum, it should be singular: Animal
. You should consider changing it to this.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With