Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Serializing Data for the Web Interface with JSON

  1. #1
    Join Date
    Jul 2010
    Posts
    28

    Default Serializing Data for the Web Interface with JSON

    So instead of continuing to derail another post, I decided to start a new one.

    How would one go about serializing data inside Girder for a web interface to take advantage of?

  2. #2
    Join Date
    Jun 2008
    Posts
    1,026

    Default

    there is a lua json library http://json.luaforge.net/

  3. #3
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,550

    Default

    It's included with Girder.
    Ron
    No support through PM

  4. #4
    Join Date
    Jun 2008
    Posts
    1,026

    Default

    i guess i should have known that.

    there is a new release on the website and indicates some performance improvements.

  5. #5
    Join Date
    Jul 2010
    Posts
    28

    Default

    Very cool.

    So to use it, inside Girder I would do something like:

    Code:
    myJSONString = json.encode( {'foo', 'bar'} )
    Then, from the interface:

    Code:
    $.get("ajaxreq.lhtml?Data=myJSONString",null, function(data){
    		// do something with the data
    });
    Is that basically how it works?

  6. #6
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,550

    Default

    Yep.

    Code:
    local json = require('json.json')
    local x = json.encode( { 'asdf','asdf' } )
    webserver:print(x)
    Ron
    No support through PM

  7. #7
    Join Date
    Oct 2005
    Posts
    307

    Default

    Attached is my updated version of the ajaxreq.lhtml file. Now if the variable is a table, the entire table is returned as a json encoded string. Then with a simple:
    newARRAY = jQuery.parseJSON(data);
    I have the whole table available as an array on the client side.

    For me and my current project, this reduced my ajax requests from 27 to 2 per client per second. JSON encoding is also making it easy to compare the previous string to the new one, reducing client side processing if nothing has changed.

    I suppose I could get down to one request if I ajaxreq.lhtml was changed to process multiple tables and/or variables per request...
    Attached Files Attached Files
    Last edited by shaun5; October 4th, 2010 at 06:04 PM.

  8. #8
    Join Date
    Jul 2010
    Posts
    28

    Default

    Dude! That is too cool.. will definitely put this into v1.1 on my project.

    Except I don't see the file attachment. Is it just me?

  9. #9
    Join Date
    Oct 2005
    Posts
    307

    Default

    It failed to upload because .lhtml isn't an allowed file extension. I renamed it .txt and uploaded it...

    For the inclusion of multiple variables, I think a request with a comma delimited variable list will work (since commas aren't part of valid lua names). Returning comma delimited json strings might create a few problems, but I don't see a better way???

  10. #10
    Join Date
    Oct 2005
    Posts
    307

    Default

    I don't think modifying the ajaxreq.lhtml file was the way to go. Instead of changing ajaxreq.lhtml to do both json and non-json, I made a new one for returning a single json encoded data. It also processes multiple variables and tables...

    $.get("ajaxreqjson.lhtml?Data=myVARIABLE,myTABLE", null, function(data){
    myOBJ = jQuery.parseJSON(data);
    something = myOBJ['myVARIABLE'];
    something else = myOBJ['myTABLE']['key'];
    });

    The code isn't very elegant, but seems to work....

    The correct file is now attached...
    Attached Files Attached Files
    Last edited by shaun5; October 5th, 2010 at 07:05 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •