Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi everybody,

I am trying to deserialize a json string which I have serialized before with the
Newtonsoft Json library.
C#
using (StreamReader sr = new StreamReader(s))
{
   json = sr.ReadToEnd();
}
object o = JsonConvert.DeserializeObject(json);
return o;

After the deserialisation the var o contains a JObject, but I want the source
object which I have serialized. How can I get the real object back.
I know there is a JsonConvert.DeserializeObject<t>() function, but I do not know what type of object it is, because I am serializing a lot of different objects.
When I serialize my objects I store the type of the object in the json string,
so how can I tell the serializer to use the stored type for deserializing.

Thanks in advance
Manu
Posted
Updated 28-Sep-12 8:31am
v3
Comments
BillWoodruff 28-Sep-12 20:00pm    
I am sure Marcus' solution will answer your question, but, if you are struggling with NewtonSoft Json, may I recommend you take a look at Mehdi Gholam's fantastic json facility here on CP: http://www.codeproject.com/Articles/159450/fastJSON ... includes detailed performance comparisons of his work, fastJson, and NewtonSoft's Json. His facility is much faster, and offers more options to deserialize complex .NET objects.

How is this for an answer: Clicky[^]
 
Share this answer
 
C#
JsonSerializerSettings set = new JsonSerializerSettings();
set.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
set.TypeNameHandling = TypeNameHandling.All;
set.Formatting = Newtonsoft.Json.Formatting.None;


When using this settings for serializing and deserializing
it works, now i get the real object and not a JObject.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900