[plugin] JSON (import/export/generate/edit/inspect/...)

0 favourites
From the Asset Store
The I18N (Translation) is a Construct plugin created to translate text in game.
  • rexrainbow

    When I'll find a way to have cyclic objects and being able to export/import them as JSON.

    For that I investigated how dojo does it

    http://dojotoolkit.org/reference-guide/1.10/dojox/json/ref.html

    And I'd like to extract only that specific algorithm to use in the plugin (not having to carry the entire dojo library)

    For now I managed to have a Save/Load Reference actions so you can basically put objects inside objects by reference instead of copy. (even from different object-type of the JSON plugin)

    So you can make a cyclic linked list for example

    But if you export as JSON and import it back, you'll lose the cyclic aspect, so it breaks all the save/load features.

    Even if I manage that, I'm not sure the save/load will always work since I won't be able to save as JSON the possible link between two different objects.

    So yeah, I hit a wall and I had other things to do so the dev stopped for awhile.

    However, I already used the plugin in a few of my projects and it's behaving nicely. Though it adds some layer of complexity.

    Though bear in mind that there's two bugs hanging in the current version:

    - bug fix: array elements weren't properly deleted using the delete function
    [ul]
    	[li]bug fix: error when using clear and delete with current path.[/code:1qdildcw][/li]
    [/ul]If I used some subversion I would have just made a release with those fixes but now they are merged with my half-implemented reference thing so...
  • It will be useful if there has a JSON behavior , since family does not has container feature, only has behavior.

  • rexrainbow

    Maybe a container behavior? so you could link any datastructure to families? I'd like to avoid forking my plugin into a behavior, since it's a pain to maintain two almost similar code base.

  • Yann

    I had put this request to Ashley. As you know, he is very very busy, so that the only one solution is making a behavior version.

  • So I'm trying to use this with json data that's been put into an array, and its reporting back this.

    [c2array] is a boolean : True

    [size] is an array of length: 3

    +---[0] is a number: 1

    +---[1] is a number: 1

    +---[2] is a number: 1

    [data] is an array of length: 1

    +---[0] is an array of length: 1

    +---+---[0] is an array of length: 1

    +---+---+---[0] is an object

    +---+---+---+---[Username] is a string : "Justin"

    +---+---+---+---[Password] is a string : "Test"

    +---+---+---+---[objectId] is a string : "NYulexXQuC"

    +---+---+---+---[createdAt] is a string : "2014-09-15T17:44:35.614Z"

    +---+---+---+---[updatedAt] is a string : "2014-09-15T17:44:35.614Z"

    But i can't figure out how to access the values of the data eg; username, password etc.

    eg: JSON.Value(0,"Username") is undefined.

  • justifun

    Of course it's undefined

    If it were in javascript you would probably do something like

    myJSONData["data"][0][0][0]["Username"]

    So in the plugin it would translate as

    JSON.Value(0,"data",0,0,0,"Username")

    Ya need to tell the thing where to put the data from.

  • Thanks

    Is the syntax similar if you are reading it from an array object instead of using your plugin?

  • Nope, the array object is just using indices and you can only have 3 dimension

    something like Array.At(3,5,1) you can't have strings as "index"

  • I think I found a bug.

    It seems it's not possible to do the following:

    clear root@

    set "Hello World" at root@"someText"

    set 10 at root@"location","latitude"

    set 20 at root@"location","longitude"

    This will cause a warning and the JSON object will be {"someText":"Hello World"}. After a long time of reading your plugin I think I found the error (or at least a fix for my problem). In the method on Line 208: "instanceProto.setValueFromPath".

    Change this:

    if(i < path_.length-1) {
    	obj = obj[path_[i]];
    } else {
    	obj[path_[i]] = value; // silently create a new property if doesn't exist yet
    }[/code:1j5vqyqm]
    
    To this:
    [code:1j5vqyqm]if(i < path_.length-1) {
    	if(!(path_[i] in obj))
    		obj[path_[i]] = {};
    
    	obj = obj[path_[i]];
    } else {
    	obj[path_[i]] = value; // silently create a new property if doesn't exist yet
    }[/code:1j5vqyqm]
  • Whyser

    Yup, it's not a bug, it's done on purpose, you should first manually create an object before filling it with property/value pairs.

    I prefer it not to be done silently

    Following your idea, I should check if an argument is a string or a number because

    set "Hello World" at root@"0"[/code:2205ryy3]it should automagically do[code:2205ryy3]var root = {};
    root["0"] = "Hello World";[/code:2205ryy3]
    
    Whereas[code:2205ryy3]set "Hello World" at root@0[/code:2205ryy3]it should automagically do[code:2205ryy3]var root = [];
    root[0] = "Hello World";[/code:2205ryy3]
    
    And I think that's messy and error prone.
    
    So basically your code should look like:
    [code:2205ryy3]Clear root@
    Set New Object at root@   // so the clear isn't necessary
    Set "Hello World" at root@"someText"
    Set New Object at root@"location"
    Set 10 at root@"location","latitude"
    Set 20 at root@"location","longitude"[/code:2205ryy3]
    
    it's equivalent to the javascript:
    [code:2205ryy3]root = {};
    root["someText"] = "Hello World";
    root["location"] = {};
    root["location"]["latitude"] = 10;
    root["location"]["longitude"] = 20;[/code:2205ryy3]
    
    so yeah bottom line, the json plugin shouldn't automagically create objects for you.
    if I'm not wrong, if you try inserting a property/value and the object doesn't exist, you should get the following warning in the console:[code:2205ryy3]invalid path: root@someText[/code:2205ryy3]or[code:2205ryy3]invalid path: root@location[/code:2205ryy3]
    
    This "invalid path" warning allowed me to debug some wrong code many times and I think silencing it would be a mistake. Yes you have to write more actions, but at least you stay in control of your datastructure.
    And events using the JSON plugins can get quite messy, 'cause construct2 doesn't have a lot of possibilities UI-wise. (I wish you could use the bracket notation)
    
    Hope I make sense.
    
    In any case you can rip apart my plugin to your heart's content :D  after all it's still in "beta" and if you find good ideas I'd be glad to add them.
    
    But keep in mind I have a more advanced version on my computer since more than a year, but I haven't released it because it's incomplete. I wanted to be able to have references to other object to avoid having to copy things and be able to create fun things like cyclic datastructures.
    But it's breaking the JSON serialization so you wouldn't be able to save/load the object's state properly.
    I found something from the dojo framework that might be a solution (they have a JSON.stringify that support references) but I didn't have time to rip it apart to just get the usefull bits (I wouldn't want to include the whole dojo library for just a plugin)
    (that's the current state of things concerning this plugin :D)
  • Hi,

    A useful feature for my purposes would be to set the path to one "level' back.

    So, if the path is set as follows:

    JSON: Set Current Path to root@"my","path","to","an","array"

    setCurrentPath_one_back

    would set it as so:

    "my","path","to","an"

    this is very useful for when you loop though complex structures and need to "automatically" back up one step, before going down several path steps again.

    Dan

  • Thank you Yann for your response! I agree about your reasoning, but I think the benefits might overweight the losses. For a complex JSON-object where we have to go down even more hierarchy's it quickly gets messy and probably even more error prone when doing it in Construct.

    As a side-note, I actually tested what you described (created new object at every "level"), and had it worked I wouldn't have even bothered to look for this "bug". But I will try again and see if I maybe made some error the first time. Will get back to you! EDIT: It seems I had missed that there were an Action called "New Object" which was necessary for it to work as intended (which however isn't necessary when creating first-level objects).

    This is a lot more code (and probably more error prone as more objects gets added)

    Clear root@
    Set New Object at root@   // so the clear isn't necessary
    Set "Hello World" at root@"someText"
    Set New Object at root@"location"
    Set New Object at root@"location","latitude"
    Set New Object at root@"location","latitude","someCoordObject"
    Set 100 at root@"location","latitude","someCoordObject","Coord1"
    Set 110 at root@"location","latitude","someCoordObject","Coord2"
    Set 120 at root@"location","latitude","someCoordObject","Coord3"[/code:33o8mxuh]
    
    Then this:
    [code:33o8mxuh]Clear root@
    Set New Object at root@   // so the clear isn't necessary
    Set "Hello World" at root@"someText"
    Set 100 at root@"location","latitude","someCoordObject","Coord1"
    Set 110 at root@"location","latitude","someCoordObject","Coord2"
    Set 120 at root@"location","latitude","someCoordObject","Coord3"[/code:33o8mxuh]
    
    Imagine when we have multiple objects that have multiple hierarchy's the actions required will expontential (figure of speech )!
  • Whyser

    well, remember you can also do:

    Load JSON " {
      ""someText"":""Hello World"",
      ""location"": {
          ""latitude"":"&mylatitude&"
          ""longitude"":"&mylongitude&"
       }
    }" at rootyet@[/code:3twjem51]
    And trust me, I recently made a manhattan distance algorithm for strategy game prototype using this JSON plugin, and having the plugin spitting out warnings was a big help. I had forgotten some checks and looping through empty data.
    Without those warning the data would have been wrongly, automatically filled and I would have had very hard to fix bugs.
    You have to like "[url=http://stackoverflow.com/questions/2807241/what-does-the-expression-fail-early-mean-and-when-would-you-want-to-do-so#answer-2807375]failing early[/url]" (:
    
    @grossd
    that's an idea, I'll think about it thanks =)
  • Yann

    I agree with you kinda, but I think it might depend on the use-case! My scenario is that I create those fields only to call "AsJSON(0)" directly after in an AJAX call, so that i can send JSON-formatted data to a server.

    Doing what you suggested (Load JSON) is very error prone and messy! If that's what i wanted to do, I would just write the JSON-object-string myself.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yann,

    In the meantime I created my own C2 based "library" function to push and pop a set the path using an Array/Stack. It works pretty well ...

    so, whenever i descend I push the current last path element into a stack, and when i return from the decent i pop the stack, after each push and pop I set the path looping through the current elements in the stack (array).

    So, if you could implement a simple "pop" that would be great.

Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)