Click here to Skip to main content
15,890,512 members
Articles / Programming Languages / C#
Tip/Trick

Complex Deserialization of Objects from JSON

Rate me:
Please Sign up or sign in to vote.
3.20/5 (4 votes)
15 Sep 2017CPOL1 min read 28.2K   2
Sometimes, we need to deserialize JSON into an object model. Here, I'll explain deserialization of objects belonging to a class hierarchy with a support of different formats using Newtonsoft Json.Net library.

Consider the following simple example. I'd like to deserialize from JSON objects of this type:

C#
public class DataModel
{
    [JsonProperty]
    public ValueModel[] Values { get; set; }
}

The only thing is that ValueModel is an abstract class, defining this hierarchy:

C#
public enum ValueType
{
    String,
    Integer
}

public abstract class ValueModel
{
    public abstract ValueType Type { get; }

    [JsonProperty]
    public string Id { get; set; }
}

public class StringValueModel : ValueModel
{
    [JsonProperty]
    public string Value { get; set; }

    public override ValueType Type => ValueType.String;
}

public class IntValueModel : ValueModel
{
    [JsonProperty]
    public int Value { get; set; }

    public override ValueType Type => ValueType.Integer;
}

I'd like to deserialize JSON like this:

JavaScript
{
    values: [
        {
            id: 'text',
            value: 'some comment'
        },
        {
            id: 'number',
            value: 4
        }
    ]
}

I want the result of this deserialization to have two objects in the Values array: the first of the StringValueModel type and the second of the IntValueModel type. How to achieve it?

First of all, we need to add into JSON discriminator field. Let's call it type:

JavaScript
{
    values: [
        {
            type: 'string',
            id: 'text',
            value: 'some comment'
        },
        {
            type: 'integer',
            id: 'number',
            value: 4
        }
    ]
}

The next step is to create custom JSON converter. It is a class, which inherits from JsonConverter class from Json.Net library:

C#
public class ValueModelJsonConverter : JsonConverter
{
    public override bool CanWrite => false;

    public override bool CanConvert(Type objectType)
    {
        return typeof(ValueModel).IsAssignableFrom(objectType);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotSupportedException("Custom converter should only be used while deserializing.");
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
        JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        // Load JObject from stream
        JObject jObject = JObject.Load(reader);
        if (jObject == null)
            return null;

        ValueType valueType;
        if (Enum.TryParse(jObject.Value<string>("type"), true, out valueType))
        {
            switch (valueType)
            {
                case ValueType.String:
                    var stringValueModel = new StringValueModel();
                    serializer.Populate(jObject.CreateReader(), stringValueModel);
                    return stringValueModel;
                case ValueType.Integer:
                    var intValueModel = new IntValueModel();
                    serializer.Populate(jObject.CreateReader(), intValueModel);
                    return intValueModel;
                default:
                    throw new ArgumentException($"Unknown value type '{valueType}'");
            }
        }

        throw new ArgumentException($"Unable to parse value object");
    }
}

In the ReadJson method of this class, we create JObject representing our value model from the instance of JsonReader. Then, we analyze the value of the type property to decide, which concrete object should be created (StringValueModel or IntValueModel). And finally, we populate properties of our created objects using serializer.Populate.

To use our converter, we should add it to JsonSerializerSettings:

C#
public class JsonParser
{
    private readonly JsonSerializerSettings _jsonSerializerSettings;

    public JsonParser()
    {
        _jsonSerializerSettings = new JsonSerializerSettings
        {
            Converters =
            {
                new StringEnumConverter {CamelCaseText = false},
                new ValueModelJsonConverter()
            },
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
    }

    public DataModel Parse(string expression)
    {
        return JsonConvert.DeserializeObject<DataModel>(expression, _jsonSerializerSettings);
    }
}

Here, we can use JsonParser to convert JSON strings into DataModel:

C#
var json = @"
{
    values: [
        {
            type: 'string',
            id: 'text',
            value: 'some comment'
        },
        {
            type: 'integer',
            id: 'number',
            value: 4
        }
    ]
}";
var parser = new JsonParser();

DataModel data = parser.Parse(json);

Now let me change requirements a little bit. Let's say, that Id property of ValueModel objects is not required. For example, if a user has not specified it, we'll generate it automatically somehow. In this case, it is sensible to allow simplified syntax of JSON:

JavaScript
{
    values: [
        'another text',
        3,
        {
            type: 'string',
            id: 'text',
            value: 'some comment'
        },
        {
            type: 'integer',
            id: 'number',
            value: 4
        }
    ]
}

If in values array we see a string, we should create an instance of StringValueModel. If we see an integer, we should create IntValueModel.

How can we do it? It requires a small change in the ReadJson method of our ValueModelJsonConverter class:

C#
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
    JsonSerializer serializer)
{
    if (reader.TokenType == JsonToken.Null)
        return null;

    if (reader.TokenType == JsonToken.String)
    {
        return new StringValueModel
        {
            Value = JToken.Load(reader).Value<string>()
        };
    }

    if (reader.TokenType == JsonToken.Integer)
    {
        return new IntValueModel
        {
            Value = JToken.Load(reader).Value<int>()
        };
    }

    // Load JObject from stream
    JObject jObject = JObject.Load(reader);
    if (jObject == null)
        return null;

    ValueType valueType;
    if (Enum.TryParse(jObject.Value<string>("type"), true, out valueType))
    {
        switch (valueType)
        {
            case ValueType.String:
                var stringValueModel = new StringValueModel();
                serializer.Populate(jObject.CreateReader(), stringValueModel);
                return stringValueModel;
            case ValueType.Integer:
                var intValueModel = new IntValueModel();
                serializer.Populate(jObject.CreateReader(), intValueModel);
                return intValueModel;
            default:
                throw new ArgumentException($"Unknown value type '{valueType}'");
        }
    }

    throw new ArgumentException($"Unable to parse value object");
}

Here, we analyze reader.TokenType property to understand if we have simple string or integer. Then we read this value using Value<T> method.

You can read more my articles on my blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Finstek
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAlready covered in more detail Pin
Graeme_Grant15-Sep-17 12:53
mvaGraeme_Grant15-Sep-17 12:53 
AnswerRe: Already covered in more detail Pin
Ivan Yakimov16-Sep-17 23:24
professionalIvan Yakimov16-Sep-17 23:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.