Ernesto Rodriguez
Ernesto Rodriguez

Reputation: 267

Deserialize Json arrays and simple object

I´m deserializing a JSON response and there is a object that could be an array or a simple object, for example:

{  
   "ValueAdds":{  
      "@size":3,
      "ValueAdd":[  
         {  
            "description":"desc 1"
         },
         {  
            "description":"desc 1"
         }
      ]
   }
}

and the other case:

{
  "ValueAdds": {
    "@size": "1",
    "ValueAdd": {
      "description": "Internet inalámbrico gratuito"
    }
  }
}

The entities:

public ValueAddsWrap 
{
     public ValueAddBase ValueAdds { set; get; }
}

public class ValueAddBase
{
    public ValueAdd[] ValueAdd { set; get; }
}

public class ValueAdd
{
    public string description { set; get; }
}

I´m getting an exception when I receive a simple object. How could I design the entities in order to deserialize an array and a simple object?

I´m using Newtonsoft:

T resp_object = JsonConvert.DeserializeObject<T>(json);

and the exception:

    An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'rce.bookings.business.expedia.Responses.ValueAdd[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Upvotes: 1

Views: 188

Answers (2)

Chris Trombley
Chris Trombley

Reputation: 2322

With a JsonConverter:

public class ValueAddsConverter : 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)
    {
        var valueAdds = new ValueAdds();
        var jo = (JObject)JObject.Load(reader)["ValueAdds"];

        valueAdds.Size = Int32.Parse((string)jo["@size"]);

        if (valueAdds.Size > 1)
        {
            valueAdds.ValueAdd = jo["ValueAdd"].Children().Select(x => x.ToObject<ValueAdd>());
        }
        else
        {
            valueAdds.ValueAdd = new List<ValueAdd>{jo["ValueAdd"].ToObject<ValueAdd>()};
        }

        return valueAdds;
    }

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

public class ValueAdd
{
    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }
}

[JsonConverter(typeof (ValueAddsConverter))]
public class ValueAdds
{
    public int Size { get; set; }

    public IEnumerable<ValueAdd> ValueAdd { get; set; }
}

Upvotes: 2

Craig W.
Craig W.

Reputation: 18155

You really can't do what I think you're asking. In the first case you've got a collection inside the object you're getting back.

public class ValueAdd
{
    public string description { get; set; }
}

public class ValueAdds
{
    public int size { get; set; }
    public List<ValueAdd> ValueAdd { get; set; }
}

public class RootObject
{
    public ValueAdds ValueAdds { get; set; }
}

In the second case you've got a single instance of an object.

public class ValueAdd
{
    public string description { get; set; }
}

public class ValueAdds
{
    public string size { get; set; }
    public ValueAdd ValueAdd { get; set; }
}

public class RootObject
{
    public ValueAdds ValueAdds { get; set; }
}

You might be able to get away with using a dynamic for the ValueAdd member, but that presents a whole bunch of annoying problems of its own as you'll still have to figure out whether it contains a collection or a single instance.

Realistically I see you have two options:

Option #1: Figure out if you've got the version with the collection or the single instance and deserialize into the appropriate type.

Option #2: Can you contact the author of the API and have them send back a consistent JSON structure? That's a lousy API if it changes the structure based on whether there are one or more than one ValueAdd objects.

Upvotes: 3

Related Questions