JSON Serialize lost properties

  1. #1

    JSON Serialize lost properties

    Hi all,

    I have an problem when serialize an object. JSON.Serialize<Type>(obj) return an incomplete object.
    Example:
    public class Class1
    {
              public Class2 Obj2;
    
              public Class3 Obj3;
    }
    
    public class Class2
    {
              public string Name;
    }
    
    public class Class3
    {
              public string Name;
    
              public Class4 Obj4;
    }
    
    public class Class4
    {
              public string Name;
    }
    
    var lst = new List<Class1>();
    
    lst.Add(new Class1());
    
    string str = JSON.Serialize(lst);
    In this example i lost Obj2 and Obj3.

    Thx in advanced.
  2. #2
    Hi,

    I see all properties.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        public class Class1
        {
            public Class2 Obj2;
            public Class3 Obj3;
        }
     
        public class Class2
        {
            public string Name;
        }
     
        public class Class3
        {
            public string Name;
            public Class4 Obj4;
        }
     
        public class Class4
        {
            public string Name;
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            var lst = new List<Class1>();
    
            lst.Add(new Class1()
                {
                    Obj2 = new Class2()
                    {
                        Name = "Class2"
                    },
                    Obj3 = new Class3()
                    {
                        Name = "Class3",
                        Obj4 = new Class4()
                        {
                            Name = "Class4"
                        }
                    }
                });
    
            string str = JSON.Serialize(lst);
    
            X.Msg.Alert("JSON", str).Show();
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
    </body>
    </html>
  3. #3
    Yes, I believe the "Class#" properties in the original sample are not Serialized by Json.NET because they have not been instantiated/created or set. The values do not exist.

    I think there's a way to work around this in Json.NET by using the DefaultValue Attribute, although you might have to manually call the Json.NET methods, instead of using the JSON.Serialize helpers provided by Ext.NET.
    Geoffrey McGill
    Founder
  4. #4
    Thx Daniill and geoffrey.

    The problem is:

    Class# is not serialized becouse the attributes [DataContract] and [DataMember] is missed .

    Solution:

    [DataContract]
        public class Class1
        {
            [DataMember]
            public Class2 Obj2;
            [DataMember]
            public Class3 Obj3;
        }
        [DataContract]
        public class Class2
        {
            [DataMember]
            public string Name;
        }
        [DataContract]
        public class Class3
        {
            [DataMember]
            public string Name;
            [DataMember]
            public Class4 Obj4;
        }
        [DataContract]
        public class Class4
        {
            [DataMember]
            public string Name;
        }
    Thx all again. I hope this help anyone too.
  5. #5
    Thanks for the sharing your solution. It might really help someone on the forums in the future.
  6. #6
    It did just help me, thanks!

Similar Threads

  1. [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
  2. Replies: 0
    Last Post: Dec 02, 2011, 12:23 AM
  3. [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
  4. [CLOSED] [1.0] lost properties in new versions of source code
    By edigital in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Oct 13, 2010, 4:35 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

Posting Permissions