Please use skript-json over this if you can
Required Addons: SkQuery
Tested Versions: PaperSpigot 1.12.2, Skript 2.2-dev36, SkQuery 3.6.0-Lime
Parse JSON in Skript with minimal addons. A slow, inefficient, trash parser.
Usage:
Don't mess with functions named as 'JSON___f___()'
Use functions named as 'JSON_f()'
Parses JSON from {_json} and outputs it to {%{_bufferAddress}%::*}
Returns error, if any.
Remember to clear the buffer if any error is given, as the function itself will not delete the parsed data before the error occured.
Some things to remember:
Example Code:
Output:
View attachment 5106
Recursively serializes (stringifies) JSON from {_jsonAddress} and returns it.
Some things to remember:
Output:
View attachment 5107
Required Addons: SkQuery
Tested Versions: PaperSpigot 1.12.2, Skript 2.2-dev36, SkQuery 3.6.0-Lime
Parse JSON in Skript with minimal addons. A slow, inefficient, trash parser.
Usage:
Don't mess with functions named as 'JSON___f___()'
Use functions named as 'JSON_f()'
code_language.skript:
function JSON_parse(json: string, bufferAddress: string) :: string
Parses JSON from {_json} and outputs it to {%{_bufferAddress}%::*}
Returns error, if any.
Remember to clear the buffer if any error is given, as the function itself will not delete the parsed data before the error occured.
Some things to remember:
- JSON 'null' is parsed as Skript 'NaN'
- The type of {var::*} ("array" or "object") is stored in {var}, i.e.
if JSON '{"hello": [2, 5]}' is parsed to variable "var", then
{var} = "object"
{var::hello} = "array"
{var::hello::1} = 2
{var::hello::2} = 5
- Special characters cannot be parsed in Skript.
'\n' and '\t' is acceptable and will be correctly parsed.
All other characters (e.g. '\b', '\f', '\u00BB') will be converted to "<U+HHHH>" where H is the hex code for the symbol.
- Skript list variable indexes are case-insensitive, so JSON keys can be overwritten. E.g.
'{"Hello": 1, "hello": 2}'
This might yield either {var::hello} = 1 or {var::hello} = 2 - Skript list variable indexes have some restriction as to what characters can be used. For example, '*' asterik can only be used to denote a list variable '{var::*}', but can't be used as '{literal asterik *}', so JSON keys containing invalid variable keys have undefined results.
Example Code:
code_language.skript:
on load:
set {_json} to "{""key1"":""a string"", ""key2!!"": 5}"
set {_error} to JSON_parse({_json}, "-res")
if {_error} is set:
broadcast "oh no! an error happened:%nl%%{_error}%"
delete {-res} and {-res::*}
exit trigger
broadcast "Parsed JSON!"
broadcast "&a%{-res}%"
loop {-res::*}:
broadcast " &b%loop-index% = %loop-value%"
delete {-res} and {-res::*}
Output:
View attachment 5106
code_language.skript:
function JSON_serialize(jsonAddress: string) :: string
Some things to remember:
- Serializing might break JSON if it involves empty objects/arrays. Empty objects/arrays will simply be serialized to the string "object" and "array" respectively, i.e.
Parsing '[{}, []]' then serializing it will yield '["object", "array"]'
Why?
Skript has no way to represent empty containers. Currently, container differentiated by using strings "object" and "array" and then detecting if the address corresponds to a list variable. - This function is recursive. If the JSON is nested too deep, Skript may print "excessive or infinite repetitions" or similar to console and stop this function.
- If the type of container is not specified in the given address it is taken as object by default. E.g.
{var::1} = "hello"
{var::2} = false
Now, you might say this is an array because it has number indexes. But, nothing differentiates that from an object which simply has the strings "1" and "2" as its indexes. As an object can house any valid variable index in skript as its key, an object is chosen by default. To serialize as an array, you need to set
{var} = "array"
code_language.skript:
on load:
set {-var} to "array"
set {-var::1} to 5
set {-var::2} to NaN value
set {-var::3} to "hiii"
set {-var::4} to "object"
set {-var::4::eyy} to "oii!"
# Here, var must not be local as JSON_serialize needs to read from it
broadcast JSON_serialize("-var")
Output:
View attachment 5107