Json.Net Is converting on its own before using my JsonConverter

三世轮回 提交于 2019-12-24 05:59:59

问题


In my WPF code, I'm using Newtonsoft.Json to deserialize json into my models. First, I receive a Json string ('json') which I then parse into 'message'. (The object I want to deserialize is wrapped in a "data" field in the json string).

Activity message = JObject.Parse(json)["data"].ToObject<Activity>();

My Activity class uses several [JsonProperty] attributes to generate its fields. One of them is an enum called 'ActivityType'.

[JsonProperty("type")]
[JsonConverter(typeof(ActivityTypeConverter))]
public ActivityType Type { get; set; }

public enum ActivityType {
    EmailOpen,
    LinkClick,
    Salesforce,
    Unsupported
};

public class ActivityTypeConverter : JsonConverter {

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var rawString = existingValue.ToString().ToLower();
        if (rawString.Contains("click"))
            return ActivityType.LinkClick;
        else if (rawString.Contains("salesforce"))
            return ActivityType.Salesforce;
        else if (rawString.Contains("email_open"))
            return ActivityType.EmailOpen;
        else
        {
            Console.WriteLine("unsupported " + rawString);
            return ActivityType.Unsupported;
        }
    }

    public override bool CanConvert(Type objectType)
    {
        return !objectType.Equals(typeof(ActivityType));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

What's bizarre and frustrating is that json objects which I know have "type":"email_open" are being deserialized as ActivityType.Unsupported, even though my converter should be deserializing them as EmailOpen.

Debugging has shown what the problem is: the json field "type" is automatically deserializing "email_open" as EmailOpen and then it is sent through my converter. (It breaks then because my conditional checks for an underscore, while EmailOpen.ToString() doesn't have one.)


So my question then is: Why is it converting without my converter and how do I stop it? I just want it to only use my converter

回答1:


I think your converter is being called -- it's just not working. The problem is that, rather than reading the new value from the JsonReader reader, you are using the value from the existingValue. But this second value is the pre-existing property value in the class being deserialized, not the value being read.

You need to load the value from the reader along the lines of Json.NET's StringEnumConverter. Here's a version that does that and also handles standard values of your enum by subclassing StringEnumConverter and passing the value read from the file to the base class for further processing:

public class ActivityTypeConverter : StringEnumConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        bool isNullable = (Nullable.GetUnderlyingType(objectType) != null);
        Type type = (Nullable.GetUnderlyingType(objectType) ?? objectType);

        if (reader.TokenType == JsonToken.Null)
        {
            if (!isNullable)
                throw new JsonSerializationException();
            return null;
        }

        var token = JToken.Load(reader);
        if (token.Type == JTokenType.String)
        {
            var rawString = ((string)token).ToLower();
            if (rawString.Contains("click"))
                return ActivityType.LinkClick;
            else if (rawString.Contains("salesforce"))
                return ActivityType.Salesforce;
            else if (rawString.Contains("email_open"))
                return ActivityType.EmailOpen;
        }

        using (var subReader = token.CreateReader())
        {
            while (subReader.TokenType == JsonToken.None)
                subReader.Read();
            try
            {
                return base.ReadJson(subReader, objectType, existingValue, serializer); // Use base class to convert
            }
            catch (Exception ex)
            {
                return ActivityType.Unsupported;
            }
        }
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(ActivityType);
    }
}


来源:https://stackoverflow.com/questions/32017695/json-net-is-converting-on-its-own-before-using-my-jsonconverter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!