[CLOSED] Ex.net.json with nhibernate proxies.

  1. #1

    [CLOSED] Ex.net.json with nhibernate proxies.

    Is there any way to tell ex.net.json serializer how deep he should serialize POCO item with or without relationships?

    For example i got tables:
    Company->job->employess
    and want to serialize only
    Company->job
    Last edited by geoffrey.mcgill; Mar 23, 2012 at 3:32 PM. Reason: [CLOSED]
  2. #2
    Hi,

    The simplest way can be mark the Employees property of the Job class with the
    [Newtonsoft.Json.JsonIgnore]
    attribute.

    Example
    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <%@ Import Namespace="Newtonsoft.Json" %>
    
    <script runat="server">
        class Employee
        {
            public string Name { get; set; }
        }
    
        class Job
        {
            public string Name { get; set; }
    
            [JsonIgnore]
            public List<Employee> Employees { get; set; }
        }
    
        class Company
        {
            public string Name { get; set; }
            public List<Job> Jobs { get; set; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Employee employee1 = new Employee() { Name = "Employee 1" };
            Employee employee2 = new Employee() { Name = "Employee 2" };
    
            Job job1 = new Job() 
            { 
                Name = "Job 1",
                Employees = new List<Employee>() { employee1, employee2 }
            };
            
            Job job2 = new Job() 
            { 
                Name = "Job 2",
                Employees = new List<Employee>() { employee1, employee2 }
            };
    
            Company company = new Company()
            {
                Name = "Company 1",
                Jobs = new List<Job>() { job1, job2 }
            };
    
            X.Msg.Alert("Company", JSON.Serialize(company)).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>
    But it's not an option if you will need to serialize the Job class including the Employees property.

    If so, the single way is implementing your own Newtonsoft.Json.JsonConverter.

    The following example should help you to start.

    Example
    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <%@ Import Namespace="Newtonsoft.Json" %>
    
    <script runat="server">
        class Employee
        {
            public string Name { get; set; }
        }
    
        class Job
        {
            public string Name { get; set; }
            public List<Employee> Employees { get; set; }
        }
    
        class Company
        {
            public string Name { get; set; }
            public List<Job> Jobs { get; set; }
        }
    
        class CompanyJsonConverter : Newtonsoft.Json.JsonConverter
        {
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                Company company = value as Company;
    
                // you should implement your custom logic here
                
                writer.WriteRawValue(JSON.Serialize(company));
            }
    
            public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                throw new NotImplementedException();
            }
    
            public override bool CanConvert(Type objectType)
            {
                return typeof(Company).IsAssignableFrom(objectType);
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Employee employee1 = new Employee() { Name = "Employee 1" };
            Employee employee2 = new Employee() { Name = "Employee 2" };
    
            Job job1 = new Job() 
            { 
                Name = "Job 1",
                Employees = new List<Employee>() { employee1, employee2 }
            };
            
            Job job2 = new Job() 
            { 
                Name = "Job 2",
                Employees = new List<Employee>() { employee1, employee2 }
            };
    
            Company company = new Company()
            {
                Name = "Company 1",
                Jobs = new List<Job>() { job1, job2 }
            };
    
            X.Msg.Alert("Company", JSON.Serialize(company, new List<JsonConverter>() { new CompanyJsonConverter() })).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>
    There is a lot of custom implementations of JsonConverter in Ext.NET:
    <Ext.Net sources root>\Ext.Net\Utility\JsonConverters\...
  3. #3
    But if i got
    context.Response.Write(JSON.Serialize(new Paging<object>(rangeData, data.Count)));
    and rangeData is List<object> and data is List<object> so how to implement ?
    Last edited by Daniil; Mar 23, 2012 at 2:36 PM. Reason: Please use [CODE] tags
  4. #4
    Again - implementing your own JsonConverter.

    Though it might be much easy to cut the unwanted things from a string after serialization.

    I mean something like this:
    string json = JSON.Serialize(new Paging<object>(rangeData, data.Count));
    json = CutUnwantedThings(json);
    context.Response.Write(json);
    Though it's a rough solution.

    Also did you consider a possibility to use the JsonIgnore attribute?
  5. #5
    Quote Originally Posted by Daniil View Post
    Again - implementing your own JsonConverter.

    Though it might be much easy to cut the unwanted things from a string after serialization.

    I mean something like this:
    string json = JSON.Serialize(new Paging<object>(rangeData, data.Count));
    json = CutUnwantedThings(json);
    context.Response.Write(json);
    Okay JsonIgnore helps alot but i want to do that more flex thx for answer anyways.
    Last edited by geoffrey.mcgill; Mar 23, 2012 at 3:32 PM.

Similar Threads

  1. Bind datasource from Nhibernate
    By thesti in forum 1.x Help
    Replies: 0
    Last Post: Apr 08, 2012, 10:44 AM
  2. Ex.net.json with nhibernate mapping.
    By endiss in forum 1.x Help
    Replies: 1
    Last Post: Mar 23, 2012, 2:05 PM
  3. Replies: 4
    Last Post: Feb 11, 2010, 4:51 PM
  4. Anyone use NHibernate?
    By Timothy in forum Open Discussions
    Replies: 1
    Last Post: Jun 14, 2009, 5:45 AM
  5. [CLOSED] Gridview datasource with nhibernate
    By jarremw in forum 1.x Help
    Replies: 20
    Last Post: Dec 24, 2008, 2:46 PM

Tags for this Thread

Posting Permissions