20. JSON manipulation library¶
The JSON module implements JSON parser and serialization routines. See JHSON <www.json.org> for details.
All functions and symbols are in “json” module, use require to get access to it.
require daslib/json
20.1. Type aliases¶
-
JsValue is a variant type
¶
_object |
table<string;JsonValue?> |
_array |
array<JsonValue?> |
_string |
string |
_number |
double |
_bool |
bool |
_null |
void? |
Single JSON element.
-
Token is a variant type
¶
_string |
string |
_number |
double |
_bool |
bool |
_null |
void? |
_symbol |
int |
_error |
string |
JSON input stream token.
-
JsonValue
¶
JsonValue fields are
value |
|
JSON value, wraps any JSON element.
-
TokenAt
¶
TokenAt fields are
value |
|
line |
int |
row |
int |
JSON parsing token. Contains token and its position.
20.2. Value conversion¶
-
JV
(v: string const)¶
JV returns json::JsonValue ?
argument |
argument type |
---|---|
v |
string const |
Creates JsonValue out of value.
-
JV
(v: double const)
JV returns json::JsonValue ?
argument |
argument type |
---|---|
v |
double const |
Creates JsonValue out of value.
-
JV
(v: bool const)
JV returns json::JsonValue ?
argument |
argument type |
---|---|
v |
bool const |
Creates JsonValue out of value.
-
JVNull
()¶
JVNull returns json::JsonValue ?
Creates JsonValue representing null.
-
JV
(v: table<string;JsonValue?>)
JV returns json::JsonValue ?
argument |
argument type |
---|---|
v |
table<string; json::JsonValue ?> |
Creates JsonValue out of value.
-
JV
(v: array<JsonValue?>)
JV returns json::JsonValue ?
argument |
argument type |
---|---|
v |
array< json::JsonValue ?> |
Creates JsonValue out of value.
20.3. Read and write¶
-
read_json
(text: string const implicit; error: string&)¶
read_json returns json::JsonValue ?
argument |
argument type |
---|---|
text |
string const implicit |
error |
string& |
reads JSON from the text string. if error is not empty, it contains the parsing error message.
-
read_json
(text: array<uint8> const; error: string&)
read_json returns json::JsonValue ?
argument |
argument type |
---|---|
text |
array<uint8> const |
error |
string& |
reads JSON from the text string. if error is not empty, it contains the parsing error message.
-
write_json
(val: JsonValue? const)¶
write_json returns string
argument |
argument type |
---|---|
val |
json::JsonValue ? const |
Overload accepting temporary type Fine, as json doesn’t escape the function
-
write_json
(val: JsonValue? const#)
write_json returns string
argument |
argument type |
---|---|
val |
json::JsonValue ? const# |
Overload accepting temporary type Fine, as json doesn’t escape the function
20.4. JSON properties¶
-
set_no_trailing_zeros
(value: bool const)¶
set_no_trailing_zeros returns bool const
argument |
argument type |
---|---|
value |
bool const |
if value is true, then numbers are written without trailing zeros.
-
set_no_empty_arrays
(value: bool const)¶
set_no_empty_arrays returns bool const
argument |
argument type |
---|---|
value |
bool const |
if value is true, then empty arrays are not written at all
-
set_allow_duplicate_keys
(value: bool const)¶
set_allow_duplicate_keys returns bool const
argument |
argument type |
---|---|
value |
bool const |
if value is true, then duplicate keys are allowed in objects. the later key overwrites the earlier one. note - we use StringBuilderWriter for performance reasons here
20.5. Broken JSON¶
-
try_fixing_broken_json
(bad: string)¶
try_fixing_broken_json returns string
argument |
argument type |
---|---|
bad |
string |
fixes broken json. so far supported 1. “string” + “string” string concatination 2. “text “nested text” text” nested quotes 3. extra , at the end of object or array 4. /uXXXXXX sequences in the middle of white space write until beginning of string or end if eof we done write the first quote write until end of string or end of file if eof we done, we fix close quote and we are done skipping whitespace if eof we done, we fix close quote and we are done valid JSON things after string are :, }, ], or , if its one of those, we close the string and we are done now, if its a + - it could be a string concatenation if it is indeed new string, we go back to string writing, and add a separator ok, its not a string concatination, or a valid character if it was a whitespace after the quote, we assume its missing ‘,’ . we assume its nested quotes and we replace with ” if eof we done, we fix nested quote and and closing quote and we are done write the second replaced quote