4

I got a json Object that i want to deserialize to its .Net type without casting it.

I think i read somewhere in the doc that you can pass an attribute into the json to tells to the deserializer the .Net object type that it can try to cast.

I can't find the where i read this.

I want to avoid use of

var myNewObject = JsonConvert.DeserializeObject<MyClass>(json);

To get something like this

MyClass myNewObject = JsonConvert.DeserializeObject(json);

I got my json object from an HttpRequest and want to instantiate the appropriate class from this nested object. Currently deserialization into an known item work good but need something more flexible without the need to manage all known Object from a parsing method.

4
  • 2
    why would you want to exclude the Generic? what benefit do you gain from this? Commented Dec 15, 2015 at 9:04
  • Are you saying you want the class to be dynamic? The MyClass bit is confusing Commented Dec 15, 2015 at 9:05
  • Why don't you want to pass a generic argument? Commented Dec 15, 2015 at 9:07
  • I think that the job can be done by an generic handled process too, but what i was looking for is the @croxy answer. Commented Dec 15, 2015 at 11:23

3 Answers 3

6

You can save the object type in your json string like this.

The settings you have to hand over the converter

public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
   TypeNameHandling = TypeNameHandling.Objects
};

How to serialize with the given settings:

var json = JsonConvert.SerializeObject(data, Settings);

This is what your json string looks like:

{
   "$type":"YourNamespaceOfTheClass",
   "YourPropertyInTheClass":valueOfProperty
}

How to deserialize with the given settings:

var object = JsonConvert.DeserializeObject(json, Settings);

Now your json string contains not only the serialized object, but also the type of your serialized object. So you don't have to worry about the right type when deserializing your json string.

Sign up to request clarification or add additional context in comments.

3 Comments

It's exactly what i was looking for. Thanks
and if i want to deserialize an object that it's inside another jsonObject. Ex: i got an jsonObject like : {"$type":"ServerResponse","message":"text","error":"error","result":[{"$type":"fooClass","message":"message"}]}
Will something like this work for you: dynamic myObject = JsonConvert.DeserializeObject(json, Settings); an then var insideObject = myObject.result ? Or have it to be more flexible? Because at the moment I can't think of another way doing this.
2

You can do the following:

dynamic myNewObject = JsonConvert.DeserializeObject(json);

which will return a dynamic object which you can work with.

Console.WriteLine(myNewObject.data[0].description);

Obviously, it will fail if your JSON doesn't contain data array having objects with description property.

Comments

0

You can do something like this:

var result = JsonConvert.DeserializeObject<dynamic>(json);

so you can deserialize any object. Cast dynamic says that you're deserializing an anynymous object with any type which will be known on runtime. It trully works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.