简体   繁体   中英

Is it possible to deserialize JSON object to custom type, passed as string?

Let say, I have a table in my database that stores Type of the object as string, and object's serialized data as JSON. The table can hold many different objects.

I want to recreate the objects depending on the stored type, something like this:

string typeFromDatabase = "SomeType";
string serializedData = ...some Json data here...;

var typeToCast = ...some reflection methods here...;

var myDeserializedObject = JsonConvert.DeserializeObject<typeToCast>(serializedData);

Is something like this possible?

Thanks in advance.

EDIT:

I think I was misunderstood - I know, I can use something like this:

var x = JsonConvert.DeserializeObject(sr1, typeof(SerizlizationTest1));

BUT in the above case, "x" is still recognized as "Object" and not as "SerizlizationTest1" - and that is what I really want.

Now, that I think about it... I guess this is not possible, and pretty useless ;] I guess that's what "dynamic" object is for.

Thanks anyway, and sorry for pretty useless question.

Best regards.

using this overload ,

var yourObject = JsonConvert.DeserializeObject(
       serializedData,
       Type.GetType(typeFromDatabase));

However, this is only going to work if the type name is assembly qualified. See here .


If the type name is not assembly qualified, then you have a different problem. You'll have to do something like iterate through all loaded assemblies for matches. What should happen if you find no, or multiple, matches?


Following your edit, you seem to be asking, how can I cast to a type named in a string. Well, I'm sure this is possible with reflection but, it won't help you write any useful code.

If you want to access something on the object, you'll need to "know" its type, based on the type string, once you know that, you might as well cast to that type. In fact, you'd probably have been better of using another overload of DeserializeObject in the first place.

You could use dynamic to stab blindly at the object, trying all possible combinations of what you want to do and catching the failures but that seems unnecessary and inefficient.

Yeah, sure it's possible, just use another overload :

var myDeserizlizedObject = JsonConvert.DeserizlizeObject(serizlizedData, typeToCast);

to retrieve the type there is no need for some "reflection methods", use simple:

var typeToCast = Type.GetType(typeFromDatabase);

be sure that name is full type name like Namespace.ClassName .

Concerning your last update. How could you possibly benefit from having specific type (I mean further usage of new object)? It is indeed looks absurd in strongly typed language like C#:

// lets assume that var is magically becomes your typeToCast type
var myDeserizlizedObject = JsonConvert.DeserizlizeObject<typeToCast>(serizlizedData);

// imagine myDeserizlizedObject is a Car
myDeserizlizedObject.StartupEngine();
myDeserizlizedObject.Move();

// but what if it's a Bag ?
// the above code makes no sense then

It's either they have a common parent and you can process them in uniform manner by casting, or it's a switch there you process them individually by type.

Try the following code out, it should work for you.

using System.Web.Script.Serialization;

JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize(Your JSon String);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM