[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.
  • Hello there,

    I just stumbled upon this plugin and it seems to be amazing for my project!

    But, as I'm not currently able to try it, I was wondering if there is a way to get a random item from a structure.

    For example:

    {
       "item 0" : {
          "feature 0" : "value",
          "feature 1" : "value"
       },
       "item 1" : {
          "feature 0" : "value",
          "feature 1" : "value"
       }
    }
    [/code:2v1ddcts]
    is it possible to get a random item with something like:
    [code:2v1ddcts]JSON.Value(0, floor(random(0, 1)))[/code:2v1ddcts]
    
    In other words: are nodes' indexes numbered too or should I replace "item 0" and "item 1" with 0 and 1 and call it a day?
  • Copons , instead of making the root an object, make it an array, this supports indixes or you could index the objects itemnames in a seperate array

  • vtrix ah nice!

    For some reasons the online JSON editor I was using gave me errors all over the place if I tried to make arrays of objects, so I just assumed it wasn't a thing in JSON, but reading your answer I read some JSON syntax references and it is possible and actually really easy:

    [
        {"key1" : "val1"},
        {"key2" : "val2"}
    ][/code:2944uvjv]Thank you very much!
  • vtrix , shinkan (mostly highlighting you since you were the most active (: )

    New version: JSON v1.1

    The first post documentation has been updated to reflect this change

    ChangeLog

    v1.1 - 2014-04-17

      - implement save/load/debugger
      • ToJson deprecated and replaced by AsJson for overall coherence
      • change the brackets syntax for a less confusing one (I hope) before: root["Wizard","stats","hp"]
      after: root@"Wizard","stats","hp"[/code:2i4wuwdv]- change Length for Size and handle object/array return -1 for anything else
      • isEmpty condition and Clear action for array and object
      • add a CurrentValue property
      • in foreach loop reset the current path to the path given at the begining on each iteration, this way we can mess with this current path to our heart content within the loop
      • more reliable logData

    Have fun testing

    If all go well, I'll probably move it to completed plugin.

  • Try Construct 3

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

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

  • Hello,

    This looks really neat. Can we expect some sort of example capx ones the plugin will be released "clean"? Thanks

  • megatronx

    In the first post there are already 2 example capx.

    https://dl.dropboxusercontent.com/u/235 ... ction.capx

    https://dl.dropboxusercontent.com/u/235 ... otype.capx

    Oh sorry, I've missed them! >.>

  • Yann , damn, just when i wanted to get some sleep, i see this.. great! who needs sleep! testing....

  • Yann , first impression, looks great, love the debugger and log info, and the new way of displaying with "@" feels a bit lighter,

    but just wanted to quickly say, that something as loopindex is missing, and could be usefull for not making a counter, or what i just did,

    for an array int(json.currentkey) , is that a design decision?

  • vtrix

    Well... The problem is a mix between javascript ambiguity, striving for simplicity (nice way to say laziness) and giving as much control as possible

    In javascript, you usually loop through array elements like this:

    for (var i = 0; i < myArray.length; i++) {
          // do things with myArray[i]
    }[/code:3sbiy7t4]
    and you loop through objects members like this:[code:3sbiy7t4]for (var m in myObject) {
          // do things with myObject[m]
    }[/code:3sbiy7t4]
    
    Thing is, in javascript arrays are also some kind of objects. So looping using a for-in works.
    However there are few differences.
      - With the first solution you have an index that increments. So expected ordering is ensured and you only explore the range between 0 and size-1
      - With the second you treat array indexes as object properties, so if you did weird things like adding a value to an array using a non positive integer (like -1) or a key as an index (c2 doesn't prevent that), you would, at least, be able to loop through it using For each property. However, if I'm not wrong the JSON exporting will just export the array part, so overall adding properties to an array should be discouraged)
      - If you skip array indexes, like for example set a value at index 10 on an less than 9-elements array, the first loop will allow you to loop through all the indexes from 0 to 10 (the inbetweens will return undefined and the JSON export will be padded with null), whereas if you use the second loop (for... in) you'll only loop through existing indexes (skipping the inbetweens)
    
    So my idea was to leave as much possibilities as the user might want (so internally choosed "for .. in") while avoiding confusion (there's only one type of loop)
    This way:
    [ul]
    	[li]If you want to loop through arrays like an object (for... in), use For each property[/li]
    	[li]otherwise, use a for/repeat loop like this:[code:3sbiy7t4]+ System: repeat JSON.Size(0,"path","to","array")[/li]
    [/ul]   -> Browser: log JSON.Value(0,"path","to","array",loopindex)[/code:3sbiy7t4]which is equivalent to for(var i = 0; i < arr.length, i++) {...}
    
    As for the necessity to cast currentKey to int... it's because I return currentKey as a string, I should probably return it as an "any" type (meaning string or number).
  • Yann

    i'm having trouble deleting an index of array

    delete root@"array","0" or 0 both just set the arrayvalue to null

    the path "key" however is gone, but the size is still counting the null as an entry

    edit: i think i gonna use strictly object from now on and use only c2 arrays for listing things

  • vtrix

    thanks nice finding, I was doing

    delete array[0];[/code:1o8zrg4h]
    but this doesn't change the size of the array so javascript set it to undefined (which is transformed to null on export)
    I have to do [code:1o8zrg4h]array.splice(0,1);[/code:1o8zrg4h]
  • Yann , just wanted to mention, i have some instances where i could use the json.key like it was before, json.key(1), i know it was me who suggested using currentkey, but the way its now, you cant use it outside of a loop and the key(1) option isnt there anymore,, if you set the path to current and then use currentkey it doesn't return it.

    i mean, currentkey, or currentvalue, would always return the the key / value at currentpath whether you're in a loop or not, so in sense its a shortcut to key(1) or value(1), but the parameter kind should also be an option

    so in the end you have currentkey, currentvalue, or key(0,"..") key(1) , value(0,"..") value(1) with parameter usage

    why i suggested it, is because i think it will be easier to understand for somebody new to the plugin, you just set the currentpath, and then use the currentkey/currentvalue, for advance use you have parameter options.

  • vtrix

    First there's never been any JSON.Key(...) expression, with or without parameters.

    Getting the key at a specific path doesn't mean much thing, because the key would be the last part of the path you are setting.

    Key(0,"my","path") should return "path". Key(0) shouldn't return anything (although it's "root" in javascript, but this implementation detail needs to be hidden from the user)

    Mabye the only use I could think of is Key(1) that would return the last component of the CurrentPath, but you should already know it in your code somehow.

    The only place where you could be unable to know the CurrentPath is in the foreach loop, that's why CurrentKey is set here.

    Otherwise, Value(0,"my","path") is already returning the value at the given path, and Value(1,"my","path") as well but relatively to CurrentPath.

    The difference between v1.0 and v1.1 is just the addition of CurrentValue which is an alias to Value(1).

    The other change I made was to reset the CurrentPath to what it should be at the begining of each loop iteration, and reset it to what it was before the foreach loop when you exit it.

    I was thinking about providing an alias like ValueFromRoot("my","path") and ValueFromCurrent("my","path") to make things more clear (maybe someone has a better idea)

    So, to summarize, I fail to see where a `Key` expression would be usefull, and the `Value` expression already works as expected, isn't it?

    And as far as CurrentKey and CurrentValue goes I prefer them to be simple. They are more or less the equivalent of the Dictionary counterpart.

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