Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft JSON - How to use the JsonConverter.ReadJson method to convert types when deserializing JSON

I need help understanding how to use the the JsonConverter.ReadJson method to convert a value of any number of types (string, boolean, Date, int, array, object) to a specific custom type.

For example, I have the following;

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {   
       //where reader.Value could be a string, boolean, Date, int, array, object
       //and in this example the value of reader.Value is a string
        return new MyCustomType(reader.Value);
    }

But this gives error;

Compilation error (line 115, col 36): Argument 1: cannot convert from 'object' to 'string'

I'm a bit green with C#, just need help making this work.

like image 553
ClaraU Avatar asked Jul 26 '16 09:07

ClaraU


People also ask

What is JsonConverter attribute?

The JsonConverterAttribute specifies which JsonConverter is used to convert an object. The attribute can be placed on a class or a member. When placed on a class, the JsonConverter specified by the attribute will be the default way of serializing that class.

What is Jsonconvert DeserializeObject?

DeserializeObject<T>(String,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to a .

What is JsonSerializerSettings?

Specifies the settings on a JsonSerializer object.


1 Answers

Finally worked it out;

public override object ReadJson(
    JsonReader reader,
    Type objectType, 
    object existingValue, 
    JsonSerializer serializer)
{
    MyCustomType myCustomType = new MyCustomType();//for null values        

    if (reader.TokenType != JsonToken.Null)
    {           
        if (reader.TokenType == JsonToken.StartArray)
        {
            JToken token = JToken.Load(reader); 
            List<string> items = token.ToObject<List<string>>();  
            myCustomType = new MyCustomType(items);
        }
        else
        {
            JValue jValue = new JValue(reader.Value);
            switch (reader.TokenType)
            {
                case JsonToken.String:
                    myCustomType = new MyCustomType((string)jValue);
                    break;
                case JsonToken.Date:
                    myCustomType = new MyCustomType((DateTime)jValue);
                    break;
                case JsonToken.Boolean:
                    myCustomType = new MyCustomType((bool)jValue);
                    break;
                case JsonToken.Integer:
                    int i = (int)jValue;
                    myCustomType = new MyCustomType(i);
                    break;
                default:
                    Console.WriteLine("Default case");
                    Console.WriteLine(reader.TokenType.ToString());
                    break;
            }
        }
    }      
    return myCustomType;
}

Not sure if this is the best possible solution, but it does the job.

like image 147
ClaraU Avatar answered Sep 21 '22 05:09

ClaraU