stdjson - anssihalmeaho/funl GitHub Wiki
Implements encoding and decoding of JSON as defined in RFC 7159.
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 |
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.
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
- bool: true if encode succeeded, false otherwise
- error description (string), '' if succeeded
- 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}'
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
- bool: true if decode succeeded, false otherwise
- error description (string), '' if succeeded
- 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))