20. JSON manipulation library

The JSON module implements JSON parser and serialization routines. See JSON <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

variant JsValue

Single JSON element.

Variants
  • _object : table<string; JsonValue ?> - JSON object

  • _array : array< JsonValue ?> - JSON array

  • _string : string - JSON string

  • _number : double - JSON number

  • _longint : int64 - extension, not part of JSON standard (represents long integer numbers)

  • _bool : bool - JSON boolean

  • _null : void? - JSON null

variant Token

JSON input stream token.

Variants
  • _string : string - string token

  • _number : double - number token

  • _longint : int64 - extension, not part of JSON standard (represents long integer numbers)

  • _bool : bool - boolean token

  • _null : void? - null token

  • _symbol : int - symbol token (one of []{}:,)

  • _error : string - error token

20.2. Structures

JsonValue

JSON value, wraps any JSON element.

Fields
  • value : JsValue - value of the JSON element

TokenAt

JSON parsing token. Contains token and its position.

Fields
  • value : Token - token value

  • line : int - token position in the input stream

  • row : int - token position in the input stream

20.3. Value conversion

JVNull() : JsonValue?()

Creates JsonValue representing null.

JV(v: string) : JsonValue?()

Creates JsonValue out of string value.

Arguments
  • v : string

JV(v: double) : JsonValue?()

Creates JsonValue out of double value.

Arguments
  • v : double

JV(v: bool) : JsonValue?()

Creates JsonValue out of boolean value.

Arguments
  • v : bool

JV(v: table<string, JsonValue?>) : JsonValue?()

Creates JsonValue out of object (table string->JsonValue?) value.

Arguments
JV(v: array<JsonValue?>) : JsonValue?()

Creates JsonValue out of array of JsonValue? value.

Arguments
JV(v: float) : JsonValue?()

Creates JsonValue out of float value.

Arguments
  • v : float

JV(v: int) : JsonValue?()

Creates JsonValue out of int value.

Arguments
  • v : int

JV(v: bitfield) : JsonValue?()

Creates JsonValue out of bitfield value.

Arguments
  • v : bitfield<>

JV(v: bitfield8:uint8<>) : JsonValue?()

Creates JsonValue out of bitfield8 value.

Arguments
  • v : bitfield : uint8<>

JV(v: bitfield16:uint16<>) : JsonValue?()

Creates JsonValue out of bitfield16 value.

Arguments
  • v : bitfield : uint16<>

JV(v: bitfield64:uint64<>) : JsonValue?()

Creates JsonValue out of bitfield64 value.

Arguments
  • v : bitfield : uint64<>

JV(val: int8) : JsonValue?()

Creates JsonValue out of int8 value.

Arguments
  • val : int8

JV(val: uint8) : JsonValue?()

Creates JsonValue out of uint8 value.

Arguments
  • val : uint8

JV(val: int16) : JsonValue?()

Creates JsonValue out of int16 value.

Arguments
  • val : int16

JV(val: uint16) : JsonValue?()

Creates JsonValue out of uint16 value.

Arguments
  • val : uint16

JV(val: uint) : JsonValue?()

Creates JsonValue out of uint value.

Arguments
  • val : uint

JV(val: int64) : JsonValue?()

Creates JsonValue out of int64 value.

Arguments
  • val : int64

JV(val: uint64) : JsonValue?()

Creates JsonValue out of uint64 value.

Arguments
  • val : uint64

20.4. Read and write

read_json(text: string implicit; error: string&) : JsonValue?()

reads JSON from the text string. if error is not empty, it contains the parsing error message.

Arguments
  • text : string implicit

  • error : string&

read_json(text: array<uint8>; error: string&) : JsonValue?()

reads JSON from the text array of uint8. if error is not empty, it contains the parsing error message.

Arguments
  • text : array<uint8>

  • error : string&

write_json(val: JsonValue?) : string()

returns JSON (textual) representation of JsonValue as a string.

Arguments
write_json(val: JsonValue?#) : string()

Overload accepting temporary type

Arguments

20.5. JSON properties

set_no_trailing_zeros(value: bool) : bool()

if value is true, then numbers are written without trailing zeros.

Arguments
  • value : bool

set_no_empty_arrays(value: bool) : bool()

if value is true, then empty arrays are not written at all

Arguments
  • value : bool

set_allow_duplicate_keys(value: bool) : bool()

if value is true, then duplicate keys are allowed in objects. the later key overwrites the earlier one.

Arguments
  • value : bool

20.6. Broken JSON

try_fixing_broken_json(bad: string) : string()

fixes broken json. so far supported 1. “string” + “string” string concatenation 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

Arguments
  • bad : string