The Problem
Let's say I have an enum representing something or other:
public enum ResultState
{
Found,
Deleted,
NotFound
}
In my serialized json, I'd like these values to be serialized as "found"
, "gone"
or "not_found"
respectively. (Note: this is not camelCase, but rather a totally custom string!)
I'm using JSON.NET
The Story So Far
I've got everything working almost right - enums are globally converted to strings via the StringEnumConverter
, however I can't for the life of me see how to achieve something similar to the above.
My initial thoughts were to apply the JsonProperty(...)
attribute to the relevant enum values, however this doesn't seem to work!
Potential Solution?
The only way I can think of getting this work is to write my own JsonConverter
inheriting from StringEnumConverter
, but with some additional magic to handle a new JsonName
attribute I'd create.
As you might imagine, I don't relish the idea of this.
I was wondering if you wonderful people could suggest a simpler alternative?