{
"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]