ASL
Json Struct Reference

Detailed Description

Functions to encode/decode data as JSON.

These functions use class Var to represent JSON values. JSON parsing supports C/C++ style comments.

Var data = Json::read("data.json"); // read and parse JSON from a file
Var data = Json::decode("{\"a\":\"abc\", \"b\":[1.5, 3]}"); // decode JSON from a string
data["c"] = true; // add a property
String json = Json::encode(data); // encode to string
Json::write(data, "data.json"); // write to file
static Var read(const String &file)
Reads and decodes data from 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.
static String encode(const Var &v, Mode mode=NONE)
Encodes the given Var into a JSON-format representation.
static bool write(const Var &v, const String &file, Mode mode=PRETTY)
Writes a var to a file in JSON format.

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.

Json::write(data, "data.json", Json::NICE);
@ NICE
Same as PRETTY and SIMPLE.
Definition: JSON.h:75

The same data object can be built in one statement, in C++11 compilers:

Var data {
{"a", "abc"},
{"b", {1.5, 3.0}},
{"c", true}
};

Or in older compilers:

Var data = Var("a", "abc")
("b", array<Var>(1.5, 3.0))
("c", true);

#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...
 

Member Enumeration Documentation

◆ Mode

enum Mode

Options for Json::encode and Json::write.

Enumerator
NONE 

Compact format in a single line.

PRETTY 

Format with newlines and indentations.

SIMPLE 

Format real numbers with reduced precision.

SHORTF 

Format doubles as short as floats.

NICE 

Same as PRETTY and SIMPLE.

Member Function Documentation

◆ decode()

static Var decode ( const String json)
static

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.

◆ encode()

static String encode ( const Var v,
Mode  mode = NONE 
)
static

Encodes the given Var into a JSON-format representation.

It is similar to JavaScript's JSON.stringify().


The documentation for this struct was generated from the following file: