A very detailed explanation on how to handle this case is available at “Using a Custom JsonConverter to fix bad JSON results”.
To summarize, you can extend the default JSON.NET converter doing
-
Annotate the property with the issue
[JsonConverter(typeof(SingleValueArrayConverter<OrderItem>))] public List<OrderItem> items;
-
Extend the converter to return a list of your desired type even for a single object
public class SingleValueArrayConverter<T> : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { object retVal = new Object(); if (reader.TokenType == JsonToken.StartObject) { T instance = (T)serializer.Deserialize(reader, typeof(T)); retVal = new List<T>() { instance }; } else if (reader.TokenType == JsonToken.StartArray) { retVal = serializer.Deserialize(reader, objectType); } return retVal; } public override bool CanConvert(Type objectType) { return true; } }
As mentioned in the article this extension is not completely general but it works if you are fine with getting a list.