key:value Json serialization

key:value Json serialization

kjmkjm Posts: 19Questions: 0Answers: 0
edited November 2009 in General
Hi,

firstly thanks for what appears to be a great plugin.

I have come up against a few issues.

The one I have at the moment is that all Json serilizers I know of (Json.NET, JavaScriptConverter Class, + others) all generate the Json in key:value format but this is not something that the datatable seems to be able to use.

Is there a workaround for this or have I missed something or are you planning to incorporate this or...?

this is what is output from most Json serialization

[code]
{"sEcho":1,"iTotalRecords":10,"iTotalDisplayRecords":10,"aaData":[[{"Job":"Plumber","Name":"John"}],[{"Job":"Plumber2","Name":"John2"}]]}
[/code]

Replies

  • kjmkjm Posts: 19Questions: 0Answers: 0
    edited November 2009
    solution I've implemented to this is (simplistic for readability);

    [code]
    public interface ITableDataSource
    {
    IList GetTableData();
    }
    [/code]

    [code]
    public class Person : ITableDataSource
    {
    public string Job { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }

    public IList GetTableData()
    {
    return new List()
    {
    this.Id,
    this.Job,
    this.Name
    };
    }
    }
    [/code]



    [code]
    //possible client code

    Person person = new Person();
    Person person2 = new Person();

    person.Job = "some occupation";
    person.Name = "some name";
    person.Id = "1";

    person2.Job = "person 2 job";
    person2.Name = "person 2 name";
    person2.Id = "2";

    List persons = new List();

    persons.Add(person);
    persons.Add(person2);

    var o = new
    {
    sEcho = 1,
    iTotalRecords = 10,
    iTotalDisplayRecords = 10,
    aaData = new List()
    };

    foreach (ITableDataSource ds in persons)
    {
    o.aaData.Add(ds.GetTableData());
    }


    return Json(o);

    [/code]

    hope this helps somebody
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Kurt,

    Nice one - thanks very much for posting your solution! I'm sure this will provide useful to others. Perhaps a future version of DataTables should allow for the key:value pair, but part of the reason I didn't do this originally was that it just adds unnecessary bandwidth requirements, since you are effectively passing redundant data around. Is there a reason you didn't want to just to an 'echo' loop, like I did in my PHP example?

    Thanks,
    Allan
  • soberspsobersp Posts: 28Questions: 0Answers: 0
    I have implemented by taking into account sColumns and Server Side Processing.
    This helps in many ways... If you have a Huge Object Graph and Composition, it introduces Dependecy.

    Instead, create a Simple Json Serializer - Generic, which takes sColums(Comma Sapareted String passed by Datatable) and the List of Object you want to generate JSON , - uses Reflection(Incase of Java) to return back the Values(Json), order same as sColumns Expects...
    Works like a Charm... No Need to have API's which take time to parse the whole Object and give back key:values which DataTable wont Accept

    Thanks
  • kjmkjm Posts: 19Questions: 0Answers: 0
    Hi Allan,

    The reason I can not build up the data exactly as I want it is I am using services to retrieve my data not from a database so I have to use their structure.

    I think you are right that passing the key along everytime would be a waste of bandwidth
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi kjm,

    In that case, what I think you'll need to do is a bit of "post-processing" on the client-side to convert the data from the format you are getting from the server, into something that DataTables is expecting. In server-side processing this can be done using fnServerData. Using Ajax or whatever, it can be done just before you pass in aaData (i.e. construct aaData).

    Regards,
    Allan
  • sgentilesgentile Posts: 3Questions: 0Answers: 0
    Allan, could you support both? Both are valid. Let the developer decide if he wants to 'waste bandwidth'.

    Basically it says 'it needs to be valid json' - and it is. But it really 'it needs to be valid json without key/pair values'.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Reply added to http://datatables.net/forums/comments.php?DiscussionID=1990&page=1#Item_2
    Allan
This discussion has been closed.