Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# JSON serialization
Hi everybody,
 
I am trying to deserialize a json string which I have serialized before with the
Newtonsoft Json library.
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() 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 28-Sep-12 5:36am
Edited 28-Sep-12 9:31am
Nueman8.5K
v3
Comments
BillWoodruff at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

How is this for an answer: Clicky[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 BillWoodruff 260
3 Shweta N Mishra 244
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100