# json

The file builtins\json.e (not an autoinclude) contains a simple json parser/printer/converter.
Use parse_json() to convert a string representation of a JSON object (see http://json.org for a formal definition, such as it is) to an internal form suitable for further processing.
Details of the internal form can be found on the parse_json page.
Use print_json() to convert an internal form into a string representation of a JSON object.
Some examples can be found in demo/rosetta/JSON.exw.
Note this does not enforce json string contents:
\-prefixes are not enforced (esp /)
\uHHHH by pure happy coincidence matches Phix native format, no more.
Unicode as utf8 is considered sufficient; no utf-16 surrogate pair stuff.
Technically these routines are fully supported by pwa/p2js, however that may be of little practical concern since the likes of libcurl and sockets are not.

### Performance note/disclaimer

No particular effort has been made regarding performance. (Keeping things dirt-simple is deemed a better goal.)
I would strongly expect the actual conversion to be almost completely IO bound, however for very large json objects that require significant processing, there may be a case for indexing the internal form - but that should be reasonably straightforward and is deemed outside the responsibility of these routines. It would also be fair to say that the trim_res argument is lazily implemented, and it might be better not to add any whitespace in the first place, instead of trimming it away at the last moment.

### Compatibility note

Euphoria cannot distinguish between {} and "" and consequently may accept some things (specifically the first of those) as valid that Phix does not. Phix will reject {12} as invalid, because it is not a string, whereas Euphoria will treat it as "\x0C". Otherwise this component should run on both Phix and Euphoria.
The following constants are defined in builtins\json.e (see parse_json for more details):

    global constant JSON_OBJECT = -1,
                    JSON_ARRAY = -2,
                    JSON_KEYWORD = -3,
                    JSON_INVALID = -4