Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.net - field is either string or List<string>

I have a situation where the JSON returned from a REST-service returns a list of Movie-objects, all specced out with a ton of information. A couple of fields in that REST-service result changes depending on the information available.

An example: A Movie always has some screen captures (images), actors and directors. Depending on the movie in question, there might be one or more images, one or more actors and one or more directors. Sample JSON for a couple of cases:

{
    "title": "Movie title",
    "images": [
        "http://www.url.com/img_0.jpg",
        "http://www.url.com/img_1.jpg",
        "http://www.url.com/img_2.jpg",
        "http://www.url.com/img_3.jpg",
        "http://www.url.com/img_4.jpg"
    ],
    "actors": [
        "Steven Berkoff",
        "Nikolaj Coster-Waldau",
        "Julie Cox"
    ],
    "directors": "Simon Aeby"
},
{
    "title": "Another movie",
    "images": "http://www.url.com/img_1.jpg",
    "actors": "actor 1"
    "directors": [
        "Justin Bieber",
        "Justin Timberlake"
    ]
}

The question is, using JSON.net, how can I create a converter that deals with this problem? I've been scouring the internet, but still haven't found a solution.

Another spin on the same question: If a field is either a List of strings or a simple string, how do I make JSON.NET create a List either way (and if just a simple string, create a list with one member)

EDIT: This REST-service is out of my control

like image 262
Kris Selbekk Avatar asked Apr 12 '12 10:04

Kris Selbekk


People also ask

How to deserialize JSON string?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

How do you handle both a single item and an array for the same property using JSON net?

The ToObjectCollectionSafe<TResult>() method can handle that for you. This is usable for Single Result vs Array using JSON.net and handle both a single item and an array for the same property and can convert an array to a single object.

How to set JsonProperty name in c#?

To set the name of individual properties, use the [JsonPropertyName] attribute. The property name set by this attribute: Applies in both directions, for serialization and deserialization.


1 Answers

Ok, I did it for fun, but don't think is useful or the best way, anyway...

Declaring the "dynamic" attributes as object and then create methods to obtain the properties as something like ImagesAsList or ImagesAsString. I did it with Extension Methods.....

var movies = JsonConvert.DeserializeObject<List<Movie>>(str);

Class

class Movie
{

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("images")]
    public object Images { get; set; }

    [JsonProperty("actors")]
    public object Actor { get; set; }

    [JsonProperty("directors")]
    public object Directors { get; set; }
}

Extension Methods

static class MovieExtension
{
    public static List<string> ImagesAsList(this Movie m)
    {
        var jArray = (m.Images as JArray);
        if (jArray == null) return null;

        return jArray.Select(x => x.ToString()).ToList();
    }

    public static string ImagesAsString(this Movie m)
    {
        return m.Images as string;
    }

}

EDIT

After reading @yamen comments I did some changes like:

var settings = new JsonSerializerSettings();
settings.Converters.Add(new MoviesConverter());

var movies = JsonConvert.DeserializeObject<List<Movie>>(str, settings);

Class

class Movie
{

    [JsonProperty("title")]
    public List<string> Title { get; set; }

    [JsonProperty("images")]
    public List<string> Images { get; set; }

    [JsonProperty("actors")]
    public List<string> Actor { get; set; }

    [JsonProperty("directors")]
    public List<string> Directors { get; set; }
}

Converter

class MoviesConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(string)) || (objectType == typeof(List<string>)) ;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.StartArray)
        {
            var l = new List<string>();
            reader.Read();
            while (reader.TokenType != JsonToken.EndArray)
            {
                l.Add(reader.Value as string);

                reader.Read();
            }
            return l;
        }
        else
        {
            return new List<string> { reader.Value as string };
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        //ToDo here we can decide to write the json as 
        //if only has one attribute output as string if it has more output as list
    }
}
like image 109
jjchiw Avatar answered Oct 07 '22 01:10

jjchiw