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

Post your work in progress addons and get feedback

Post » Fri Apr 25, 2014 2:23 pm

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.
B
6
S
1
Posts: 8
Reputation: 350

Post » Fri Apr 25, 2014 11:17 pm

@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...
ImageImage
B
69
S
21
G
7
Posts: 827
Reputation: 10,017

Post » Thu Jun 26, 2014 3:47 pm

Yann wrote:Hi there,

Download links are right here:

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:
Code: Select all
{
   "Wizard": {
        "stats": {
            "hp":80,
            "mp":120,
            "str":2,
            "dex":2,
            "int":16
        },
       "spells": [
            "Fireball",
            "Poison",
            "Meteor",
            "Ice Storm"
        ]
    }
}

You can access the hp of the Wizard property like this:
Code: Select all
JSON: Set 100 at root@"Wizard","stat","hp"

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 root and 1 for current.
Code: Select all
JSON.Value(0,"Wizard","stat","hp")
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.

2 - ACE

Conditions:

Is object - v1.0
Returns true if the value at the given path is an Object (type of value holding key/value pairs like a dictionnary)

Is array - 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)

Is boolean - v1.0
Returns true if the value at the given path is either true of false

Is number - v1.0
Returns true if the value at the given path is a number

Is string - v1.0
Returns true if the value at the given path is a string

Is null - v1.0
Returns true if the value at the given path is null

Is undefined - v1.0
Returns true if the value at the given path doesn't exist

Is Empty - 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)

For each property - 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: Select all
System: repeat JSON.Size(0,"my","path")

go through your array using :
Code: Select all
JSON.Value(0,"my","path",loopindex)



Actions:

Set New Object - v1.0
Creates a new empty object (list of key/value pairs akin to the dictionary) at the given path

Set New Array - v1.0
Creates a new empty array (list of numerically indexed values) at the given path

Set Value - v1.0
Sets a string or number at the given path

Set Boolean - v1.0
Sets true or false at the given path

Set null - v1.0
Sets null at the given path

Delete - v1.0
Deletes the property at the given path (future access will be undefined)

Clear - 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)

LoadJSON - v1.0
Loads any kind of JSON string. It will internally build a JSON object from it.

Set Current Path - 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: Select all
+ 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

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: Select all
+ some condition
   + System: repeat JSON.Size(0,"my","path","to","an","array")
      -> Text: append JSON.Value(1,"my","path","to","an","array",loopindex) & newline


*LogData - 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


Expressions:

Length - 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)

Size - 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

Value
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"

ToJson - v1.0 - deprecated in v1.1
Replaced by AsJson for coherence

AsJson - v1.1 *New*
Returns a JSON of the data at the given path
To export the entire object as JSON, do
Code: Select all
JSON.ToJson(0)


TypeOf - 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

CurrentKey - v1.0
Returns the current key in a foreach loop. Outside a loop returns an empty string "".

CurrentValue - v1.1 *New*
Returns the current value in a foreach loop. Outside a loop returns "undefined" (probably)


3 - Use Cases:

Class mecanism
Image
gives you this kind of console output
Image
and now (v1.1) in the debugger:
Image
https://dl.dropboxusercontent.com/u/23551572/C2/plugins/json/demo/prototype.capx

Inspecting an unknown JSON

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

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.


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)
    Code: Select all
    before: root["Wizard","stats","hp"]
    after:  root@"Wizard","stats","hp"
    - 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


could u load animation object json or md5mech or simelar objects with this
B
13
S
5
G
4
Posts: 176
Reputation: 4,419

Post » Thu Jun 26, 2014 6:23 pm

@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)
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Jun 30, 2014 6:46 pm

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.
B
5
S
1
Posts: 7
Reputation: 289

Post » Mon Jun 30, 2014 8:34 pm

@chrismaliszewski
if you have an Array whose AsJson gives you
Code: Select all
{
    "c2array":true,
    "size":[3,2,2],
    "data":
    [
        [
            [10,32],[12,48]
        ],
        [
            [72,26],[80,32]
        ],
        [
            [-28,16],[-22,20]
        ]
    ]
}

What would you want to transform it into?

(or maybe use your own example, mine uses just random numbers)
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Tue Jul 01, 2014 7:15 am

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!
B
5
S
1
Posts: 7
Reputation: 289

Post » Sat Jul 19, 2014 8:39 pm

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
Yann wrote:@chrismaliszewski
if you have an Array whose AsJson gives you
Code: Select all
{
    "c2array":true,
    "size":[3,2,2],
    "data":
    [
        [
            [10,32],[12,48]
        ],
        [
            [72,26],[80,32]
        ],
        [
            [-28,16],[-22,20]
        ]
    ]
}

What would you want to transform it into?

(or maybe use your own example, mine uses just random numbers)
B
15
S
4
Posts: 138
Reputation: 2,003

Post » Sat Jul 19, 2014 11:56 pm

@Dasat
hmmm
Dasat wrote: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.
Code: Select all
+ System: on start of layout
     Local text myKey = "someKey"
     Local text myValue = "someValue"
     -> JSON: new Object at root@
     -> JSON: set myValue at [email protected]

Doesn't work for you?
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Sun Jul 20, 2014 12:02 am

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

Yann wrote:@Dasat
hmmm
Dasat wrote: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.
Code: Select all
+ System: on start of layout
     Local text myKey = "someKey"
     Local text myValue = "someValue"
     -> JSON: new Object at root@
     -> JSON: set myValue at [email protected]

Doesn't work for you?
Last edited by Dasat on Sun Jul 20, 2014 5:31 pm, edited 1 time in total.
B
15
S
4
Posts: 138
Reputation: 2,003

PreviousNext

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests

cron