Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

c# - Json.net Custom enum converter

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The enum type is the objectType argument to ReadJson. However, a few points:

  1. You need to handle nullable enum types.
  2. You need to handle [Flag] enumerations. Json.NET writes these as comma-separated lists of values.
  3. You need to handle the case of an enum with an invalid value. Json.NET writes these as numeric values when 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...