[CLOSED] Using JSON.Serialize() and JSON.Deserialize()

  1. #1

    [CLOSED] Using JSON.Serialize() and JSON.Deserialize()

    Hi,

    Could you give me some pointers on the proper usage of JSON serialization methods? Both Serialize and Desirialize work OK for me by default as long as I don't have a hierarchy of derived classes. However, that's where the challenges start. It seems as though JSON.Deserialize() requires a properly created list of Newtonsoft JsonConverter objects or an instance of IContractResolver in order to be able to handle inherited classes.

    I've looked at the Newtonsoft documentation and examples to help me get on track at
    http://james.newtonking.com/projects...meHandling.htm
    http://james.newtonking.com/projects...nConverter.htm
    http://james.newtonking.com/projects...ctResolver.htm

    Any hint or directions would be highly appreciated. Please advise if more info is required.
    Last edited by Daniil; Apr 30, 2013 at 3:31 PM. Reason: [CLOSED]
  2. #2
    Hi Vadym,

    Quote Originally Posted by vadym.f View Post
    However, that's where the challenges start. It seems as though JSON.Deserialize() requires a properly created list of Newtonsoft JsonConverter objects or an instance of IContractResolver in order to be able to handle inherited classes.
    To be able to handle inherited classes? Sorry, I don't not quite follow you. Could you, please, clarify?

    Are you asking how to create custom converters?
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi Vadym,



    To be able to handle inherited classes? Sorry, I don't not quite follow you. Could you, please, clarify?

    Are you asking how to create custom converters?
    I think so. Do you have any example using JSON.Serialize() and Deserialize() similar to this?
    http://james.newtonking.com/projects...meHandling.htm
  4. #4
    Well, there is no an overloaded version of the Serialize method with this signature.
    string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    });
    The Json class is a set of wrappers for Json.NET.
    http://svn.ext.net/premium/branches/...t/Util/JSON.cs

    It defines the wrappers which are convenient to use within Ext.NET and, certainly, in a developer's application. But it doesn't reflects all of possible overloaded version of the JsonConvert.SerializeObject method.

    You could use the JsonConvert.SerializeObject method directly if needed. If you do need (or just want) use the JSON.Serialize method, you might need to implement a custom JsonConverter. There is a lot of custom JsonConverters used in Ext.NET. They are in this folder.
    http://svn.ext.net/premium/branches/...sonConverters/
  5. #5
    By the way, the JSON class has been improved in Ext.NET v2. There are the overloaded methods which takes a JsonSerializeSettings instance.

    Also there are the GloballSettings и RequestSettings properties to manage serialization options in a global manner.
  6. #6
    It seems that Newtonsoft's SerializeObject() method isn't very robust when handling a JSON object built on the client side. I manually build a JSON object and have to assign the $type property in the form of $type="FullyQualifiedTypeName, AssemblyName" for all derived classes, which SerializeObject expects to be present. However, if this property isn't the first in the object state, it's somehow ignored and only parent class properties are serialized. I'm not sure how to make the $type property appear first in the client object state. I do realize this may not be directly related to Ext.Net but any hint would be appreciated.

    The code below works only if the $type property comes first in the JSON property stack.
    string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    });
  7. #7
    Quote Originally Posted by vadym.f View Post
    It seems that Newtonsoft's SerializeObject() method isn't very robust when handling a JSON object built on the client side. I manually build a JSON object and have to assign the $type property in the form of $type="FullyQualifiedTypeName, AssemblyName" for all derived classes, which SerializeObject expects to be present. However, if this property isn't the first in the object state, it's somehow ignored and only parent class properties are serialized. I'm not sure how to make the $type property appear first in the client object state. I do realize this may not be directly related to Ext.Net but any hint would be appreciated.

    The code below works only if the $type property comes first in the JSON property stack.
    string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    });
    It might be best to post Json.NET specific questions on the Json.NET related forum. Or, post a simplified .aspx code sample that demonstrates the whole scenario.
    Geoffrey McGill
    Founder
  8. #8
    Found a robust solution at http://stackoverflow.com/questions/8...a-list-of-base. Please mark this thread as closed.

Similar Threads

  1. JSON.Serialize issue
    By fragrance22 in forum 2.x Help
    Replies: 0
    Last Post: Jan 24, 2013, 5:40 AM
  2. JSON.Serialize issue
    By fragrance22 in forum 1.x Help
    Replies: 0
    Last Post: Jan 22, 2013, 12:58 PM
  3. [CLOSED] Ext.NET v1.3 JSON Serialize Problem
    By softmachine2011 in forum 1.x Legacy Premium Help
    Replies: 14
    Last Post: Mar 07, 2012, 1:08 PM
  4. [CLOSED] How to Json.Serialize a DataTable
    By vali1993 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 28, 2011, 4:21 PM
  5. [1.0] - JSON Serialize DateTime Request
    By drkoh in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Jul 26, 2010, 1:56 PM

Tags for this Thread

Posting Permissions