ASL
|
Functions to encode/decode data as JSON.
These functions use class Var to represent JSON values. JSON parsing supports C/C++ style comments.
Write and encode functions support an additional argument to control style. By default encode()
uses a compact format (no newlines or whitespace) and write()
uses an indented style (PRETTY
). This can be changed with this argument.
The SIMPLE
and NICE
(same but multiline, indented) values will write real numbers having a slightly reduced precision to avoid numbers like 12.25000001
or 2.09999999
(will look like 12.25
and 2.1
). By default numbers are written so that they are recovered exactly when parsing.
The same data
object can be built in one statement, in C++11 compilers:
Or in older compilers:
#include <JSON.h>
Public Types | |
enum | Mode { NONE = 0 , PRETTY = 1 , SIMPLE = 2 , COMPACT = 4 , JSON = 8 , EXACT = 16 , SHORTF = 32 , NICE = 3 } |
Options for Json::encode and Json::write. More... | |
Static Public Member Functions | |
static Var | read (const String &file) |
Reads and decodes data from a file in JSON format. | |
static bool | write (const Var &v, const String &file, Mode mode=PRETTY) |
Writes a var to a file in JSON format. | |
static Var | decode (const String &json) |
Decodes the JSON-encoded string into a Var that will contain all the structure. More... | |
static String | encode (const Var &v, Mode mode=NONE) |
Encodes the given Var into a JSON-format representation. More... | |
enum Mode |
Options for Json::encode and Json::write.
Decodes the JSON-encoded string into a Var that will contain all the structure.
It is similar to JavaScript's JSON.parse()
. If there are format parsing errors, the result will be a Var::NONE
typed variable.
Encodes the given Var into a JSON-format representation.
It is similar to JavaScript's JSON.stringify()
.