An easy way to save objects in JSON?

Hello,

I would like to save an arbitrary object (i.e. one of my own making) to file, and was wondering if it was possible to utilise Daz's JSON library?

Are any of its methods exposed via DazScript?

I reckon I could use libraries like this [https://github.com/douglascrockford/JSON-js] direct in DazScript with little or no tweaking but thought I would check first.

Comments

  • edited December 1969

    By pure dumb luck, I have discovered the answer while looking at the debugger.

    It is yes. There is an object in global called JSON.

    The following demonstrates how to use it should anyone else be interested...

    
    //Take an object (o1) and return a string that is it expressed in JSON
    //var o_as_json = JSON.stringify(o1)
    //Take a JSON string and return an object with its members and values
    //var o = JSON.parse(o_as_json);
    
    //For example....
    
    function showProperties(o)
    {
     for(var name in o)
     {
       print(name + " : " + o[name]);
     }
    }
    
    function thing()
    {
     this.x = "xval";
     this.y = "yval";
    }
     
    var o1 = new thing();
    
    var o_as_json = JSON.stringify(o1)
    
    print(o_as_json);
    
    var o2 = JSON.parse(o_as_json);
    
    showProperties(o2);
Sign In or Register to comment.