Choosing between two different types during deserialization

c#, json.net

Solution

The solution is to create a custom `JsonConverter` with the following method:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    object source;

    //[42, 23]
    if (reader.TokenType == JsonToken.StartArray)
    {
        reader.Read();
        var values = new List<object>();
        while (reader.TokenType != JsonToken.EndArray)
        {
            values.Add(reader.Value);
            reader.Read();
        }

        reader.Read();
        source = values.ToArray();
    }
    else if (reader.TokenType == JsonToken.StartObject)//{"subtrahend": 23, "minuend": 42}
    {
        reader.Read();
        var values = new Dictionary<string, object>();
        while (reader.TokenType != JsonToken.EndObject)
        {
            if (reader.TokenType != JsonToken.PropertyName)
                throw new FormatException("Expected a property name, got: " + reader.TokenType);
            string propertyName = reader.Value.ToString();
            reader.Read();

            values.Add(propertyName, reader.Value);
            reader.Read();
        }

        source = values;
    }
    else
        throw new FormatException("Expected start of object or start of array");

    reader.Read();
    return source;
}

And then tag the property with it:

[DataMember(Name = "params", IsRequired = false)]
[JsonConverter(typeof(ArgumentConverter))]
public object[] Parameters { get; set; }

Problem

I got an object which can have the following alternatives: ``` "params": [23, 42] "params": {"minuend": 42, "subtrahend": 23} ``` How can I make json.net automatically create a `object[]` property for the first one and a `Dictionary<string,object>` property for the second one? Example (stub) class: ``` [DataContract] public class JsonRcpRequest { [DataMember(Name = "params")] public object Parameters { get; set; } } ```

Original source