The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

JSON serialization

Last post 03-23-2008, 7:02 AM by paschoal1. 11 replies.
Sort Posts: Previous Next
  •  03-04-2008, 2:25 AM 8253

    JSON serialization

    Hi fellows, as you can see in my other posts, I am getting really involved in serialization subjects. Now I am trying to serialize an ES object to Json format. Has anyone ever tried to do it?

    I have tried JayRock, as well as JSON.NET. I only got to serialize using Jayrock, but only if it had no foreign key to any table. Ive read in some posts around here it has to do with the hierarquical structure...anyhow, would there be any better builtin way in ES, or maybe in .net framework, to acomplish this task?

    Thanks a lot.

  •  03-04-2008, 6:16 AM 8255 in reply to 8253

    Re: JSON serialization

    You might try using our ProxyStubs, they serialize XML very nicely?

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  03-05-2008, 3:22 PM 8276 in reply to 8255

    Re: JSON serialization

    Well Mike, its not exactly XML I am after, I rather need it to be in Json format.

    However, I went on and tried your solution (mostly out of curiosity on how it works), I generated the stub classes as demonstrated in http://www.entityspaces.net/blog/2007/11/10/EntitySpacesPureWCFClientSideProxies.aspx. Below goes my code, and the outuput (the packet content)...

    Code:
    Cliente cliente = new Cliente();
            if (cliente.LoadByPrimaryKey(idCliente))
            {
                ClienteProxyStub clienteProxyStub = new ClienteProxyStub();
                XmlSerializer sf = new XmlSerializer(typeof(ClienteProxyStub));
                StringWriter sw = new StringWriter();
                sf.Serialize(sw, clienteProxyStub);
                packet = sw.ToString();
            }

     

    Code:
    
    <ClienteProxyStub xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    
    <IdCliente xsi:nil="true" />
    
    <esRowState>Added</esRowState>
    
    </ClienteProxyStub>
    
     

    So if I am not mistaken, shoudnt it spit out my table data? I guess I missed something...

    Anyhow, as I stated before, even after creating a fine XML, I dont quite see this as a solution for my Json format issue...it was good, though, to get a little expertise on how stub classes work...

  •  03-05-2008, 5:24 PM 8277 in reply to 8276

    Re: JSON serialization

    You were supposed to pass "cliente" into your ClienteProxyStub() constructor, that will do it.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  03-06-2008, 2:37 AM 8278 in reply to 8277

    Re: JSON serialization

    Yes, I noticed that in your code...however, when I tried this, the compiler raised me an error saying there is no method overload that accepts 1 parameter!

     

  •  03-06-2008, 2:20 PM 8290 in reply to 8278

    Re: JSON serialization

    I am not that worried at the stub classes at the moment (though it would be nice if I could get it working too :) ) ...my only concern is how I could serialize an object into json format...

  •  03-06-2008, 4:43 PM 8294 in reply to 8290

    Re: JSON serialization

    1) Regarding the proxy/stub issue: It sounds like you have included the lightweight client side ProxyStub classes in your server side project. The output from that template would go in a separate client side project. The server side needs the full ProxyStubs. They are created when you run the Generated Master, and check "Generate the Proxy/Stub" on the Proxy/Stub tab.

    2) Why use our ProxyStubs?: Even if you use Json.NET, the EntitySpaces ProxyStubs are designed to be serialized. If you try to serialize the full objects, you would probably have to manually add a bunch of JsonIgnore attributes every time you regenerated.

    3) Regarding how to get them into Json format: We are not familiar with that library. A quick skim of these sites suggests that you would use JsonSerializer in place of XmlSerializer. The exact syntax should be in their docs, blogs, forums, etc.

    http://www.json.org/
    http://www.codeplex.com/Json
    http://james.newtonking.com/projects/json-net.aspx
    Json.NET - Simplifying .NET <-> JavaScript communication


    David Neal Parsons
    www.entityspaces.net
  •  03-20-2008, 6:03 AM 8480 in reply to 8294

    Re: JSON serialization

    OK, I regenerated the classes. Now I can pass the object in the constructor.

    However, I now get the following error when trying to serialize the object:

    A circular reference was detected while serializing an object of type 'Tracker.BO.Cliente'

    I tried generating the classes with no hierarquical, but still didnt help...I didnt check the WCF option, does it make any difference? 

    Below goes my code, I appreciate any help on this, Ive been struggling for days in this subject:

    Code:
    public string GeraJson()
        {
            string packet = "";
    
            Cliente cliente = new Cliente();
            if (cliente.LoadByPrimaryKey(idCliente))
            {   
                ClienteProxyStub clienteProxyStub = new ClienteProxyStub(cliente);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                
                string msg = "[" + serializer.Serialize(clienteProxyStub) + "]";
    
                return msg;
            }
    
            return packet;                
        }
     

     

     

     

  •  03-20-2008, 6:16 AM 8482 in reply to 8480

    Re: JSON serialization

    According to this post (http://community.entityspaces.net/forums/post/5503.aspx), in fact there seems to exist some sort of "incompatibility" between JavaScriptSerializer and ES stub classes. Does it make sense ? If so, do you intend to fix it someday?

    Still, if that is true, what would you recommend to work around this issue?

  •  03-22-2008, 7:43 AM 8516 in reply to 8482

    Re: JSON serialization

    the only reason why I tend to use JavaScriptSerializer  is that it seems smooth in termos of generating json format...at least gets close to it!

    that said, would be wonderful if I could get ES to work fine with it...

  •  03-22-2008, 2:00 PM 8517 in reply to 8516

    Re: JSON serialization

    Based on the fact that Javascript serialization of DataTables gets the same exception, I'm going to suggest the same solution - a custom JavaScriptConverter. I haven't really looked at how one might implement it.

    http://siderite.blogspot.com/2007/08/invalidoperationexception-circular.html
    http://www.dennydotnet.com/post/2007/09/A-DataTable-Serializer-for-ASPNET-AJAX.aspx


    David Neal Parsons
    www.entityspaces.net
  •  03-23-2008, 7:02 AM 8523 in reply to 8517

    Re: JSON serialization

    Thanks, I appreciate this help.

    Will let you know as soon as I get this implemented and up running...

View as RSS news feed in XML