Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize json based on fields in .Net (C#)

I'm writing an app that gets a Json list of objects like this:

[
   {
       "ObjectType": "apple",
       "ObjectSize": 35,
       "ObjectCost": 4,
       "ObjectTaste": "good",
       "ObjectColor": "golden"
   },
   {
       "ObjectType": "books",
       "ObjectSize": 53,
       "ObjectCost": 7,
       "Pages": 100
   },
   {
       "ObjectType": "melon",
       "ObjectSize": 35,
       "ObjectTaste": "good",
       "ObjectCost": 5
   },
   {
       "ObjectType": "apple",
       "ObjectSize": 29,
       "ObjectCost": 8,
       "ObjectTaste": "almost good",
       "ObjectColor": "red"
   }
  ]

I want to make a base class ItemToSell (size,cost), and derive Apple, Melon and Book from it, then make the deserialization based on the "ObjectType" field to whatever class it fits. I want it to build a list of ItemToSell objects, every object being Apple, Melon or Book.

How could I do this in .Net?

Thanks in advance :)

EDIT: I know how to deserialize it in a Big class with all the fields it can ever contain, like: Base(ObjectType,ObjectSize,ObjectCost,ObjectColor,Pages). But I want it to distinguish between classes by the ObjectType so I won't have any usefulness fields like Pages field for every book item or ObjectTaste for every book.

like image 280
Cristian Dan Avatar asked May 09 '14 14:05

Cristian Dan


3 Answers

Some time ago I had the same problem.

You'll can use Json.NET, but if you don't have control over the json document (as in: 'it has been serialized by some other framework') you'll need to create a custom JsonConverter like this:

class MyItemConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(ItemToSell).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject obj = JObject.Load(reader);
        string discriminator = (string)obj["ObjectType"];

        ItemToSell item;
        switch (discriminator)
        {
            case "apple":
                item = new Apple();
                break;
            case "books":
                item = new Books();
                break;
            case "melon":
                item = new Melon();
                break;
            default:
                throw new NotImplementedException();
        }

        serializer.Populate(obj.CreateReader(), item);

        return item;

    }


    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {

    }
}

Then you'll need to add it to the converters of the JsonSerializerSettings like so:

JsonSerializerSettings settings = new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Objects,

};
settings.Converters.Add(new MyItemConverter());
var items = JsonConvert.DeserializeObject<List<ItemToSell>>(response, settings);
like image 133
Dries Marckmann Avatar answered Sep 20 '22 00:09

Dries Marckmann


You can use a CustomCreationConverter. This lets you hook into the deserialization process.

public abstract class Base
{
    public string Type { get; set; }
}

class Foo : Base
{
    public string FooProperty { get; set; }
}

class Bar : Base
{
    public string BarProperty { get; set; }
}

class CustomSerializableConverter : CustomCreationConverter<Base>
{
    public override Base Create(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jObject = JObject.Load(reader);

        var type = (string)jObject.Property("Type");
        Base target;
        switch (type)
        {
            case "Foo":
                target = new Foo();
                break;
            case "Bar":
                target = new Bar();
                break;
            default:
                throw new InvalidOperationException();
        }
        serializer.Populate(jObject.CreateReader(), target);
        return target;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var json = "[{Type:\"Foo\",FooProperty:\"A\"},{Type:\"Bar\",BarProperty:\"B\"}]";
        List<Base> bases = JsonConvert.DeserializeObject<List<Base>>(json, new CustomSerializableConverter());
    }
}
like image 30
Tejas Sharma Avatar answered Sep 18 '22 00:09

Tejas Sharma


This is not an answer but in C# 6.0 you will be able to do this:

  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using Newtonsoft.Json.Linq;
  [TestMethod]
  public void JsonWithDollarOperatorStringIndexers()
  {

      // Additional data types eliminated for elucidation
      string jsonText = @"
        {
          'Byte':  {
            'Keyword':  'byte',
            'DotNetClassName':  'Byte',
            'Description':  'Unsigned integer',
            'Width':  '8',
            'Range':  '0 to 255'
                    },
          'Boolean':  {
            'Keyword':  'bool',
            'DotNetClassName':  'Boolean',
            'Description':  'Logical Boolean type',
            'Width':  '8',
            'Range':  'True or false.'
                      },
        }";
      JObject jObject = JObject.Parse(jsonText);
      Assert.AreEqual("bool", jObject.$Boolean.$Keyword);
    }

Taken from here.

like image 24
Bura Chuhadar Avatar answered Sep 20 '22 00:09

Bura Chuhadar