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

  • I'm really impressed with this plugin. It's basically everything I could have hoped for, I think. I'll be sure to give feedback if I run across anything.

  • Yann, yes sorry, was a bit fast on the suggestion, should wait till i get some sleep

    its because i"m just using a new technique (for me), combining the use of c2 array with pathsetting of json, i drive one or more paths with a c2 arrraylist, so loop is driven by the array curX, but like you said the currentkey is is always known in the code, anyway i shut up now, i agree with ScottA ... A+ plugin, everything is working smooth and my project is shaping up...

  • Hi there,

    Download links are right here:

      JSON v1.1

      (2014-04-17) JSON v1.0 (2014-03-21)

    I'm not entirely confident in how it was designed, so I want to leave it as a Work in Progress for a while. So please don't build important project with it yet, as I want to be able to move things around (create/delete condition/action/expressions) to release a clean plugin.

    Here is the basic documentation:

    1 - Design specificities

    All Conditions, Actions and Expressions accept at least an Origin and a Path parameter.

    The Origin can be either:

    - root: meaning from the base of the object

    - current: from the current path that is either automatically set by the foreach condition, or set using the Set Current Path action

    this allows you to be more concise and also some kind of recursion (to inspect an unknown JSON for instance)

    The path parameter is used to travel through level of depth inside your object (a bit like an URL).

    For example, if you have the following structure:

    {
       "Wizard": {
            "stats": {
                "hp":80,
                "mp":120,
                "str":2,
                "dex":2,
                "int":16
            },
           "spells": [
    >             "Fireball",
    >             "Poison",
    >             "Meteor",
    >             "Ice Storm"
    >         ]
        }
    }[/code:3r0vqt1q]
    You can access the hp of the Wizard property like this:[code:3r0vqt1q]JSON: Set 100 at root@"Wizard","stat","hp"[/code:3r0vqt1q]
    You would build this path the same way you add parameters to a function in the Function plugin
    
    A little important note, In an expression, you have to use 0 or 1 as first parameter. 0 stands for [b]root[/b] and 1 for [b]current[/b].[code:3r0vqt1q]JSON.Value(0,"Wizard","stat","hp")[/code:3r0vqt1q]Is an expression which returns the the hp of the wizard.
    
    This design allows you also to export from and import into any part of your object. For example, if you have different sources of JSON file, you could build an array or a dictionnary (Object) of those inside one JSON object.
    
    With that out of the way, let's list all we can do with the plugin.
    
    [b][h2]2 - ACE[/h2][/b]
    
    [h2]Conditions:[/h2]
    
    [b]Is object[/b] - v1.0
    Returns true if the value at the given path is an Object (type of value holding key/value pairs like a dictionnary)
    
    [b]Is array[/b] - v1.0
    Returns true if the value at the given path is an Array (type of value holding a simple list of numerically indexed values)
    
    [b]Is boolean[/b] - v1.0
    Returns true if the value at the given path is either true of false
    
    [b]Is number[/b] - v1.0
    Returns true if the value at the given path is a number
    
    [b]Is string[/b] - v1.0
    Returns true if the value at the given path is a string
    
    [b]Is null[/b] - v1.0
    Returns true if the value at the given path is null
    
    [b]Is undefined[/b] - v1.0
    Returns true if the value at the given path doesn't exist
    
    [b]Is Empty[/b] - v1.1  *New*
    Returns true if the value at the given path is empty.
    If the value is an object, empty means 0 members
    If the value is an array, empty means 0 elements
    Otherwise, empty means undefined (i.e. a boolean, a number, a string, or null will be considered not empty)
    
    [b]For each property[/b] - v1.0
    Loops through the value at the given path. The value should be an object or an array. 
    And order cannot be predicted.
    To loop through arrays you should use something like :[code:3r0vqt1q]System: repeat JSON.Size(0,"my","path")[/code:3r0vqt1q]
    go through your array using :[code:3r0vqt1q]JSON.Value(0,"my","path",loopindex)[/code:3r0vqt1q]
    
    [h2]Actions:[/h2]
    
    [b]Set New Object[/b] - v1.0
    Creates a new empty object (list of key/value pairs akin to the dictionary) at the given path
    
    [b]Set New Array[/b] - v1.0
    Creates a new empty array (list of numerically indexed values) at the given path
    
    [b]Set Value[/b] - v1.0
    Sets a string or number at the given path
    
    [b]Set Boolean[/b] - v1.0
    Sets true or false at the given path
    
    [b]Set null[/b] - v1.0
    Sets null at the given path
    
    [b]Delete[/b] - v1.0
    Deletes the property at the given path (future access will be undefined)
    
    [b]Clear[/b] - v1.1  *New*
    Clear the object/array at the given path, if the value at the given path is neither an object nor an array, it will be deleted (future access will be undefined)
    
    [b]LoadJSON[/b] - v1.0
    Loads any kind of JSON string. It will internally build a JSON object from it.
    
    [b]Set Current Path[/b] - v1.0
    Sets the current relative path of the JSON object. Allows you to use some shortcuts when writing path and to loop through properties recursively.
    Example:[code:3r0vqt1q]+ some condition
       -> JSON: Set Current Path to root@"my","path","to","an","array"
       + System: repeat JSON.Size(1)
          -> Text: append JSON.Value(1,loopindex) & newline[/code:3r0vqt1q]
    To list all the values of an array at "my","path","to","an","array
    Without using Set Current Path, it would look like this:
    [code:3r0vqt1q]+ some condition
       + System: repeat JSON.Size(0,"my","path","to","an","array")
          -> Text: append JSON.Value(1,"my","path","to","an","array",loopindex) & newline[/code:3r0vqt1q]
    
    [b]*LogData[/b] - v1.0
    That's the only thing without any path property, it allows you to log in the browser's consol:
     - the entire object
     - the current path
    
    [h2]Expressions:[/h2]
    
    [b]Length[/b] - v1.0 - deprecated in v1.1
    Returns the Length of the array at the given path. If there's no array at the given path, it returns 0 (maybe should return -1)
    
    [b]Size[/b] - v1.1  *New*
    Return the size of the array/object at the given path (-1 if not an array/object)
    Replace the deprecated Length expression
    
    [b]Value[/b]
    Returns the value at the given path. 
    If the value is:
     - a string, it returns the string
     - a number, it returns the number
     - true, it returns 1, false, it returns 0
     - an array, it returns the string "array"
     - an object, it returns the string "object"
    
    [b]ToJson[/b] - v1.0 - deprecated in v1.1
    Replaced by AsJson for coherence
    
    [b]AsJson[/b] - v1.1  *New*
    Returns a JSON of the data at the given path
    To export the entire object as JSON, do[code:3r0vqt1q]JSON.ToJson(0)[/code:3r0vqt1q]
    
    [b]TypeOf[/b] - v1.0
    Returns a string representing the type of the value at the given path:
     - "string" for a string
     - "number" for a number
     - "boolean" for true/false
     - "array" for an array
     - "object" for an object
     - "null" for a null value
     - "undefined" if there's nothing
    
    [b]CurrentKey[/b] - v1.0
    Returns the current key in a foreach loop. Outside a loop returns an empty string "".
    
    [b]CurrentValue[/b] - v1.1  *New*
    Returns the current value in a foreach loop. Outside a loop returns "undefined" (probably)
    
    [b][h2]3 - Use Cases:[/h2][/b]
    
    [h2]Class mecanism[/h2]
    [img="https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/proto.png"]
    gives you this kind of console output
    [img="https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/proto-console.png"]
    and now (v1.1) in the debugger:
    [img="https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/debugger.png"]
    [url]https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/demo/prototype.capx[/url]
    
    [h2]Inspecting an unknown JSON[/h2]
    
    [url]https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/demo/inspection.capx[/url]
    
    If you have other interesting use cases or any kind of idea to improve the plugin don't hesitate.
    To be honest, I'm more interested in making the current features more accessible/understandable than big new features, but I'll stay open minded (:. The main reason I go through this Alpha phase it to make sure the plugin won't be a pain to use or behave unexpectedly.
    
    Also I didn't implement yeah all the C2's save/loading and debugging system. I want to wait until we are sure the plugin is solid and stable.
    
    [h2][b]ChangeLog[/b][/h2]
    v1.1 - 2014-04-17
    [ul]- implement save/load/debugger
    - ToJson deprecated and replaced by AsJson for overall coherence
    - change the brackets syntax for a less confusing one (I hope) [code:3r0vqt1q]before: root["Wizard","stats","hp"]
    after:  root@"Wizard","stats","hp"[/code:3r0vqt1q]- 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[/ul]
    

    could u load animation object json or md5mech or simelar objects with this

  • istavang

    This plugin can load any json file that respects the json format described here: http://www.json.org/

    after that, what you do with the data is up to you =)

    (that's quite a quote you did there)

  • I'd like to work on Array (Data & Storage), set all needed values into it, save all data from this Array to JSON object of yours into Array type of node and save JSON file. The most important thing is, that I should be able to reverse this process (go from JSON file to Array data).

    My question is...

    How to set data of Array (Data & Storage) into a Array type of node in JSON in such a way so I'll have presented notation of data inside a single key: [[0,1], [2,3]] - normal notation for arrays in JSON format. This notation has this advantage that I would be able to modify data both in JSON object and Array, if needed some day.

    I know that I can save Array as a string using Array.AsJson method into JSON Object type of node but I'd like to do it in a way mentioned above. Is it possible?

    If I wrote sth in not clear way, I'll explain this one more time.

  • chrismaliszewski

    if you have an Array whose AsJson gives you

    {
        "c2array":true,
        "size":[3,2,2],
        "data":
        [
            [ 
                [10,32],[12,48]
            ],
            [
                [72,26],[80,32]
            ],
            [
                [-28,16],[-22,20]
            ]
        ]
    }[/code:3djezfzp]
    What would you want to transform it into?
    
    (or maybe use your own example, mine uses just random numbers)
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • My bad... I forgot about using JSON.LoadJSON method to set data from Array.AsJSON... I created peace of code and everything works. Thank you very much!

  • for me, i want to achieve is, in my json data, to be something like

    {"mykey": "value1:, "mykey2": [2darray]}.

    Here is the cumbersome format i'm getting using construct dict and array

    {"c2dictionary":true,"data":{"name":"Project1","value":"{\"c2array\":true,\"size\":[10,2,1],\"data\":[[[\"cons 1\"],[\"2\"]],[[\"cons 2\"],[\"4\"]],[[\"cons 3\"],[\"5\"]],[[\"cons 4\"],[\"2\"]],[[\"\"],[\"\"]],[[\"\"],[\"\"]],[[\"\"],[\"\"]],[[\"\"],[\"\"]],[[\"\"],[\"\"]],[[\"\"],[\"\"]]]}"}}

    I've been trying out somethings by trying to follow the examples, but when i set the key or value to a defined variable in my program or content of some array, it doesnt reflect, it only reflects when i add some raw string or integer(not a variable with data stored in it). I also fear for the challenge i might encounter when trying to read the data back from my my backend.

    Kindly help. Thanks

    chrismaliszewski

    if you have an Array whose AsJson gives you

    {
        "c2array":true,
        "size":[3,2,2],
        "data":
        [
    >         [ 
    >             [10,32],[12,48]
            ],
            [
    >             [72,26],[80,32]
            ],
            [
    >             [-28,16],[-22,20]
            ]
        ]
    }[/code:f84pylqi]
    What would you want to transform it into?
    
    (or maybe use your own example, mine uses just random numbers)
    
  • Dasat

    hmmm

    but when i set the key or value to a defined variable in my program or content of some array, it doesnt reflect

    Not sure I understand what you are trying to say.

    + System: on start of layout
         Local text myKey = "someKey"
         Local text myValue = "someValue"
         -> JSON: new Object at root@
         -> JSON: set myValue at root@myKey[/code:2s2jgc8y]
    Doesn't work for you?
  • no it doesn't.

    But like i said what i basically want to achieve is to store "2 arrays as a value for a particular key" while i still have another key with just a string. all this values are set as the program is run, not mannually, but it does;t work and i don''t even know how to achieve storing 2 arrays as a value.(reading 2d array as json in construct is not really straighforward)

    I dont also get how to populate arrays using this plugin. I mean when you add a new array and you add values like you do for the object, it just replaces values so, its only one value in the array added

    Dasat

    hmmm

    > but when i set the key or value to a defined variable in my program or content of some array, it doesnt reflect

    >

    Not sure I understand what you are trying to say.

    + System: on start of layout
         Local text myKey = "someKey"
         Local text myValue = "someValue"
         -> JSON: new Object at root@
         -> JSON: set myValue at root@myKey[/code:2s2hevo1]
    Doesn't work for you?
    
  • This plugin looks interesting. Will have to check it out xD. Thx~

  • Dasat

    is this working for you?

    arithmeticTables.capx

  • Yann, Thanks a lot, the array illustration made using the plugin very easy and i've been able to implement that in my work. Moving on to the next stage now. Thanks once again.

    Dasat

    is this working for you?

    https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/demo/arithmeticTables.capx

  • Dasat

    Glad to help sir

  • shes a miss!!

    Dasat

    Glad to help sir

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