Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deserialize a derived class with json.net

I'm currently getting back the following json from a service that I do not own:

{
    "auc" : 320658953,
    "item" : 31294,
    "owner" : "Amacid",
    "ownerRealm" : "EarthenRing",
    "bid" : 289493,
    "buyout" : 371150,
    "quantity" : 1,
    "timeLeft" : "LONG",
    "rand" : 0,
    "seed" : 0,
    "context" : 0
}, {
    "auc" : 321175921,
    "item" : 82800,
    "owner" : "Drakonys",
    "ownerRealm" : "EarthenRing",
    "bid" : 7000384,
    "buyout" : 7507980,
    "quantity" : 1,
    "timeLeft" : "VERY_LONG",
    "rand" : 0,
    "seed" : 161297536,
    "context" : 0,
    "petSpeciesId" : 293,
    "petBreedId" : 9,
    "petLevel" : 1,
    "petQualityId" : 3
}, 

And I'm trying to deserialize it into proper CLR objects. It's obvious that these object hold a an object hierarchy on the server side; something like Item and PetItem : Item where PetItem has the extra 4 properties at the end there.

How do I handle this? I'm using Json.net to deserialize most of the other results from other services that are more consistent, but I'm looking for a strategy to handle situations like this. I've seen some solutions on how to have Json.net deserialize everything into an implied base class, but I'm not sure how to handle this upcasting type scenario

like image 954
Sinaesthetic Avatar asked Mar 21 '15 21:03

Sinaesthetic


People also ask

How do I deserialize an object to JSON?

NET objects (deserialize) 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.

Which JSON method is used to deserialize the data?

Net object to JSON string use the Serialize method. It's possible to deserialize JSON string to . Net object using Deserialize<T> or DeserializeObject methods.

What is the difference between serialize and deserialize JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How do you serialize and deserialize?

For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.


2 Answers

Here is yours example: http://dotnetbyexample.blogspot.ru/2012/02/json-deserialization-with-jsonnet-class.html?m=1

You just have to use overloaded method

JsonConvert.DeserializeObject<List<Item>>
  (r.EventArgs.Result, params Newtonsoft.Json.JsonConverter[] converters)

And write your converters

like image 129
Evgeny Korolyuk Avatar answered Nov 04 '22 13:11

Evgeny Korolyuk


I don't see any alternative besides creating the class hierarchy in c# and then have some sort factory that checks for the presence of signature keys on the json side, creating the right c# object. petSpeciesId is probably a good candidate for distinguishing between Item and PetItem.

Ask yourself whether you really need to do this. Maybe a sparse class with unused properties will suit your purposes. Or just use System.Json.JsonObject to get an unordered key/value collection.

@Evgeny 's link to the blog post is a correct approach, but it does seem like an incredible amount of work! There's probably a simpler variation, but at bottom that's the work that needs to be done.

like image 28
Robert Moskal Avatar answered Nov 04 '22 14:11

Robert Moskal