7.2. Boost package for JSON

The JSON_BOOST module extends JSON support with operator overloads for convenient field access (?[]), null-coalescing (??), and automatic struct-to-JSON conversion macros (from_JsValue, to_JsValue).

See also JSON manipulation library for core JSON parsing and writing. See tutorial_json for a hands-on tutorial.

All functions and symbols are in “json_boost” module, use require to get access to it.

require daslib/json_boost

Example:

require daslib/json_boost

    [export]
    def main() {
        let data = "\{ \"name\": \"Alice\", \"age\": 30 \}"
        var error = ""
        var js <- read_json(data, error)
        if (error == "") {
            let name = js?.name ?? "?"
            print("name = {name}\n")
            let age = js?.age ?? -1
            print("age = {age}\n")
        }
        unsafe {
            delete js
        }
    }
    // output:
    // name = Alice
    // age = 30

7.2.1. Structures

JsonFieldState

Per-field serialization options for JSON struct conversion.

Fields:
  • argName : string - name of the field in JSON

  • enumAsInt : bool - whether to parse enum as integer

  • unescape : bool - whether to unescape strings

  • embed : bool - whether to embed the field

  • optional : bool - whether the field is optional

7.2.2. Reader macros

json

This macro implements embedding of the JSON object into the program:

var jsv = %json~
{
  "name": "main_window",
  "value": 500,
  "size": [1,2,3]
} %%

7.2.3. Variant macros

better_json

This macro is used to implement is json_value and as json_value runtime checks. It essentially substitutes value as name with value.value as name and value is name with value.value is name.

7.2.4. Value conversion

7.2.4.1. JV

JV(val1: auto; val2: auto; val3: auto; val4: auto; val5: auto; val6: auto; val7: auto; val8: auto; val9: auto): JsonValue?

Creates array of nine JsonValues.

Arguments:
  • val1 : auto

  • val2 : auto

  • val3 : auto

  • val4 : auto

  • val5 : auto

  • val6 : auto

  • val7 : auto

  • val8 : auto

  • val9 : auto

JV(val1: auto; val2: auto; val3: auto; val4: auto): JsonValue?
JV(val1: auto; val2: auto; val3: auto; val4: auto; val5: auto; val6: auto; val7: auto): JsonValue?
JV(val1: auto; val2: auto; val3: auto; val4: auto; val5: auto; val6: auto; val7: auto; val8: auto; val9: auto; val10: auto): JsonValue?
JV(val1: auto; val2: auto; val3: auto; val4: auto; val5: auto; val6: auto): JsonValue?
JV(val1: auto; val2: auto; val3: auto; val4: auto; val5: auto): JsonValue?
JV(val1: auto; val2: auto): JsonValue?
JV(val1: auto; val2: auto; val3: auto; val4: auto; val5: auto; val6: auto; val7: auto; val8: auto): JsonValue?
JV(val1: auto; val2: auto; val3: auto): JsonValue?
JV(v: auto(VecT)): auto
JV(value: auto(TT)): JsonValue?

7.2.4.2. from_JV

from_JV(v: JsonValue const?; ent: uint16; defV: uint16 = uint16(0)): auto

Parse a JSON value and return the corresponding native value.

Arguments:
from_JV(v: JsonValue const?; ent: uint8; defV: uint8 = uint8(0)): auto
from_JV(v: JsonValue const?; ent: int8; defV: int8 = int8(0)): auto
from_JV(v: JsonValue const?; ent: int16; defV: int16 = int16(0)): auto
from_JV(v: JsonValue const?; ent: bitfield; defV: bitfield = bitfield()): auto
from_JV(v: JsonValue const?; ent: uint; defV: uint = 0x0): auto
from_JV(v: JsonValue const?; ent: int; defV: int = 0): auto
from_JV(v: JsonValue const?; ent: bitfield16:uint16<>; defV: bitfield16 = bitfield16()): auto
from_JV(v: JsonValue const?; ent: auto(VecT); defV: VecT = VecT()): auto
from_JV(v: JsonValue const?; ent: bitfield64:uint64<>; defV: bitfield64 = bitfield64()): auto
from_JV(v: JsonValue const?; anything: auto(TT)): auto
from_JV(v: JsonValue const?; anything: table<auto(KT), auto(VT)>): auto
from_JV(v: JsonValue const?; ent: int64; defV: int64 = 0): auto
from_JV(v: JsonValue const?; ent: float; defV: float = 0f): auto
from_JV(v: JsonValue const?; ent: string; defV: string = ""): auto
from_JV(v: JsonValue const?; ent: auto(EnumT); defV: EnumT = EnumT()): EnumT
from_JV(v: JsonValue const?; ent: bool; defV: bool = false): auto
from_JV(v: JsonValue const?; ent: double; defV: double = 0lf): auto
from_JV(v: JsonValue const?; ent: uint64; defV: uint64 = 0x0): auto
from_JV(v: JsonValue const?; ent: bitfield8:uint8<>; defV: bitfield8 = bitfield8()): auto

7.2.5. Element access operators

JsonValue const? ==const?.(a: JsonValue const? ==const; key: string): JsonValue?

Returns the value of the key in the JSON object, if it exists.

Arguments:

7.2.5.1. JsonValue const? ==const?[]

JsonValue const? ==const?[](a: JsonValue const? ==const; key: string): JsonValue?

Returns the value of the key in the JSON object, if it exists.

Arguments:
JsonValue const? ==const?[](a: JsonValue const? ==const; idx: int): JsonValue?

JsonValue? ==const?.(a: JsonValue? ==const; key: string): JsonValue?

Returns the value of the key in the JSON object, if it exists.

Arguments:

7.2.5.2. JsonValue? ==const?[]

JsonValue? ==const?[](a: JsonValue? ==const; idx: int): JsonValue?

Returns the value of the index in the JSON array, if it exists.

Arguments:
JsonValue? ==const?[](a: JsonValue? ==const; key: string): JsonValue?

7.2.6. Null coalescing operators

7.2.6.1. JsonValue const???

JsonValue const???(a: JsonValue const?; val: float): float

Returns the value of the JSON object, if it exists, otherwise returns the default value.

Arguments:
JsonValue const???(a: JsonValue const?; val: double): double
JsonValue const???(a: JsonValue const?; val: int16): int16
JsonValue const???(a: JsonValue const?; val: int8): int8
JsonValue const???(a: JsonValue const?; val: uint8): uint8
JsonValue const???(a: JsonValue const?; val: int64): int64
JsonValue const???(a: JsonValue const?; val: uint16): uint16
JsonValue const???(a: JsonValue const?; val: int): int
JsonValue const???(a: JsonValue const?; val: bool): bool
JsonValue const???(a: JsonValue const?; val: uint64): uint64
JsonValue const???(a: JsonValue const?; val: uint): uint
JsonValue const???(a: JsonValue const?; val: string): string

7.2.7. Value extraction

JsonValue const? ==const?.`value(a: JsonValue const? ==const): variant<_object:table<string;JsonValue?>;_array:array<JsonValue?>;_string:string;_number:double;_longint:int64;_bool:bool;_null:void?> const?

Returns the value of the JSON object, if it exists.

Arguments:
JsonValue? ==const?.`value(a: JsonValue? ==const): variant<_object:table<string;JsonValue?>;_array:array<JsonValue?>;_string:string;_number:double;_longint:int64;_bool:bool;_null:void?>?

Returns the value of the JSON object, if it exists.

Arguments:

7.2.8. Annotation parsing

parse_json_annotation(name: string; annotation: array<tuple<name:string;data:variant<tBool:bool;tInt:int;tUInt:uint;tInt64:int64;tUInt64:uint64;tFloat:float;tDouble:double;tString:string;nothing:any>>>): JsonFieldState

Parse JSON field annotations and return the corresponding JsonFieldState.

Arguments:
  • name : string

  • annotation : array<tuple<name:string;data: RttiValue>>