The EntitySpaces Community

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

JSON & EntitySpaces, I am posting a working solution for those interested in using both!

Last post 05-16-2008, 4:39 PM by Mike.Griffin. 5 replies.
Sort Posts: Previous Next
  •  03-24-2008, 12:16 PM 8536

    JSON & EntitySpaces, I am posting a working solution for those interested in using both!

    The code below serves as a working sample for those that want to serialize an ES object into a json format. It passes a datatable to do the job. Enjoy!

    Code:
    public class JsonSerialization
        {
            public string Serialize(DataTable dt)
            {
                StringBuilder msg = new StringBuilder();
                msg.Append("sqlFieldVals: {");
    
                foreach (DataRow dr in dt.Rows)
                {
                    int count = 0;
                    foreach (DataColumn column in dt.Columns)
                    {
                        count += 1;
                        string columnName = column.ColumnName;  
                        msg.Append(columnName + ":'");
                        string value = dr[column].ToString();
                        msg.Append(value);
                        if (count != dt.Columns.Count)
                        {
                            msg.Append("',");
                        }
                        else
                        {
                            msg.Append("'");
                        }                    
                    }
                    
                }
    
                msg.Append("}");
    
                return msg.ToString();
            }
        }
     

    You should use it like this:

    Code:
    DataTable dt = customer.Query.LoadDataTable();
                    
            JsonSerialization json = new JsonSerialization();
            packet = json.Serialize(dt);
  •  03-25-2008, 8:21 PM 8556 in reply to 8536

    Re: JSON & EntitySpaces, I am posting a working solution for those interested in using both!

    Thank you for posting this. As much traffic as there has been on this subject I am suprised nobody has replied, perhaps you might reply on related threads that mention JSON?

    EntitySpaces | Twitter | BLOG
  •  05-16-2008, 9:42 AM 9332 in reply to 8536

    Re: JSON & EntitySpaces, I am posting a working solution for those interested in using both!

    This is superb stuff! I used the custom base class option, created a public method and put your code in:

    Code:
    public string Json()
    {
    
    // your code
    
    }

    now i just have to:

    Code:
    CustomersCollection col = new CustomersCollection();
    col.LoadAll();
    string json = col.Json();

    and my jquery stuff is something like:

    function LoadData()
        {
            $.getJSON("ajaxData.ashx",function(data)
            {            
                    $('#lblId').html(data.CustomerCollection.Customer[0].CustomerID);
                    $('#lblName').html(data.CustomerCollection.Customer[0].Name);
               
            });
        } 

     magic!


     Note: in the code listed by paschoal1 i used the body of the method and used the Table property. so i replace 'dt' with 'Table'.


    Sean Rock
  •  05-16-2008, 1:45 PM 9335 in reply to 9332

    Re: JSON & EntitySpaces, I am posting a working solution for those interested in using both!

    Maybe we should just build this right into EntitySpaces ?

    EntitySpaces | Twitter | BLOG
  •  05-16-2008, 2:30 PM 9336 in reply to 9335

    Re: JSON & EntitySpaces, I am posting a working solution for those interested in using both!

    Hey Mike, that would be great. however, i was thinking about making this available to some of my previous applications where rebuilding the dal (es project) isn't an option, but adding some ajax/jquery magic is. I was contemplating inheriting from the Newtonsoft.Json.JsonConverter base class and creating an EntitySpaces converter because i can see i will be using this alot more! unless someone else beats me to it. :)


    Sean Rock
  •  05-16-2008, 4:39 PM 9342 in reply to 9336

    Re: JSON & EntitySpaces, I am posting a working solution for those interested in using both!

    It's all your, let me know if you have any suggestion, there's a lot of cool stuff going on right now, good work. Good thing this was posted by Paschoal1, good work guys.

    EntitySpaces | Twitter | BLOG
View as RSS news feed in XML