I'm currently using Json.net to consume json in my app. The API I use send me a specific string format for enum for example:
For an enum of type TemperatureType with values fahrenheit, Celcius
The json value is:
{"result":["TemperatureType_fahrenheit","TemperatureType_Celcius"]}
I would like to use a converter to directly manage it to get
an IList<TemperatureType> but also working for other enum types.
Does anybody have an idea ?
I've try to use a custom JsonConverter :
if (reader.TokenType == JsonToken.String && reader.Value != null)
{
string value = reader.Value.ToString();
var splitValues = value.Split('_');
if (splitValues.Length == 2)
{
var type = Type.GetType(splitValues[0]);
return Enum.Parse(type, splitValues[1]);
}
}
The problem is the GetType Property because I haven't got parameters indicating the desired type and no namespace
The enum type is the objectType argument to ReadJson. However, a few points:
[Flag] enumerations. Json.NET writes these as comma-separated lists of values.StringEnumConverter.AllowIntegerValues == true and throws an exception otherwise.Here is a subclass of StringEnumConverter that handles these cases by calling the base class then adding or removing the type prefix when appropriate:
public class TypePrefixEnumConverter : StringEnumConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool isNullable = (Nullable.GetUnderlyingType(objectType) != null);
Type enumType = (Nullable.GetUnderlyingType(objectType) ?? objectType);
if (!enumType.IsEnum)
throw new JsonSerializationException(string.Format("type {0} is not a enum type", enumType.FullName));
var prefix = enumType.Name + "_";
if (reader.TokenType == JsonToken.Null)
{
if (!isNullable)
throw new JsonSerializationException();
return null;
}
// Strip the prefix from the enum components (if any).
var token = JToken.Load(reader);
if (token.Type == JTokenType.String)
{
token = (JValue)string.Join(", ", token.ToString().Split(',').Select(s => s.Trim()).Select(s => s.StartsWith(prefix) ? s.Substring(prefix.Length) : s).ToArray());
}
using (var subReader = token.CreateReader())
{
while (subReader.TokenType == JsonToken.None)
subReader.Read();
return base.ReadJson(subReader, objectType, existingValue, serializer); // Use base class to convert
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var array = new JArray();
using (var tempWriter = array.CreateWriter())
base.WriteJson(tempWriter, value, serializer);
var token = array.Single();
if (token.Type == JTokenType.String && value != null)
{
var enumType = value.GetType();
var prefix = enumType.Name + "_";
token = (JValue)string.Join(", ", token.ToString().Split(',').Select(s => s.Trim()).Select(s => (!char.IsNumber(s[0]) && s[0] != '-') ? prefix + s : s).ToArray());
}
token.WriteTo(writer);
}
}
Then, you can use it wherever you could use StringEnumConverter, for instance:
var settings = new JsonSerializerSettings { Converters = new JsonConverter[] { new TypePrefixEnumConverter() } };
var json = JsonConvert.SerializeObject(myClass, Formatting.Indented, settings);
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