stdjson - anssihalmeaho/funl GitHub Wiki

stdjson

Implements encoding and decoding of JSON as defined in RFC 7159.

Mapping between FunL and JSON data

Mapping values between JSON and FunL values is following:

JSON FunL value
Number, like 0.5, 2.0 float
Number, like 2 int
String string
Boolean (true/false) bool (true/false)
Array list
Object map
null opaque type: stdjson.json-null

null (opaque type)

Opaque type json-null represent JSON null value. Value stdjson.null can be used as JSON null as there's no other similar data in FunL for that.

Functions

encode

Encodes FunL values to JSON.

type: function

Format:

call(stdjson.encode <value>) -> <list: is-ok error-description data-bytearray>

Return value: list

Returned list contains

  1. bool: true if encode succeeded, false otherwise
  2. error description (string), '' if succeeded
  3. target data as bytearray (stdbytes opaque type)

Example:

data = map(
   'key-1' 100
   'key-2' list('something' map('a' 'b') true)
   'key-3' stdjson.null
   'key-4' 0.5
   'key-5' false
)

ok err-desc bytes = call(stdjson.encode data):
if( ok 
  call(stdbytes.string bytes)
  sprintf('error: %s' err-desc)
)

-> '{"key-2": ["something", {"a": "b"}, true], "key-5": false, "key-4": 0.5, "key-1": 100, "key-3": null}'

decode

Decodes JSON data to FunL value.

type: function

Format:

call(stdjson.decode <data-bytearray>) -> <list: is-ok error-description value>

Return value: list

Returned list contains

  1. bool: true if decode succeeded, false otherwise
  2. error description (string), '' if succeeded
  3. decoded FunL value

Example:

json-data = '{"key1": [10, null, {"key2": "abc"}], "key-2": false}'
call(stdjson.decode call(stdbytes.str-to-bytes json-data))

-> list(true, '', map('key1' : list(10, opaque(json-null), map('key2' : 'abc')), 'key-2' : false))
⚠️ **GitHub.com Fallback** ⚠️