tur/json
JSON encode and decode with no external dependencies.
Since: Phase B1
json/null
(json/null :int)
create a JSON null node.
A heap-allocated JSON null node.
(json/null) ; => <null node>
Since: Phase B2
json/bool
(json/bool [v :int] :int)
create a JSON boolean node.
| v | 0 for false, non-zero for true |
A heap-allocated JSON bool node.
(json/bool 1) ; => <true node>
Since: Phase B2
json/int
(json/int [v :int] :int)
create a JSON integer node.
| v | the integer value |
A heap-allocated JSON int node.
(json/int 42) ; => <42 node>
Since: Phase B2
json/float
(json/float [v :float] :int)
create a JSON float node.
| v | the float value |
A heap-allocated JSON float node.
(json/float 3.14) ; => <3.14 node>
Since: Phase B2
json/string
(json/string [s :cstr] :int)
create a JSON string node from a cstr.
| s | NUL-terminated cstr (copied internally) |
A heap-allocated JSON string node owning a copy of s.
(json/string "hello") ; => <"hello" node>
Since: Phase B2
json/array-new
(json/array-new :int)
create an empty JSON array node.
A heap-allocated JSON array node backed by a growable vec.
(json/array-new) ; => <[] node>
Since: Phase B2
json/array-push
(json/array-push [arr :int elem :int] :int)
append an element to a JSON array node.
| arr | JSON array node from json/array-new | |
| elem | JSON node to append |
The same arr pointer (mutated in place).
(json/array-push arr (json/int 1))
Since: Phase B2
json/object-new
(json/object-new :int)
create an empty JSON object node.
A heap-allocated JSON object node with no entries.
(json/object-new) ; => <{} node>
Since: Phase B2
json/object-put
(json/object-put [obj :int key :cstr val :int] :int)
add or update a key-value pair in a JSON object.
| obj | JSON object node | |
| key | NUL-terminated key cstr (copied internally) | |
| val | JSON value node for the key |
The same obj pointer (mutated in place).
(json/object-put obj "x" (json/int 1))
Since: Phase B2
json/type
(json/type [node :int] :int)
return the type tag of a JSON node.
| node | JSON node |
0=null 1=bool 2=int 3=float 4=string 5=array 6=object
(json/type (json/int 5)) ; => 2
Since: Phase B2
json/get-bool
(json/get-bool [node :int] :bool)
extract the boolean value from a bool node.
| node | JSON bool node |
true or false.
(json/get-bool (json/bool 1)) ; => true
Since: Phase B2
json/get-int
(json/get-int [node :int] :int)
extract the integer value from an int node.
| node | JSON int node |
The integer value as :int.
(json/get-int (json/int 42)) ; => 42
Since: Phase B2
json/get-float
(json/get-float [node :int] :float)
extract the float value from a float node.
| node | JSON float node |
The float value as :float.
(json/get-float (json/float 1.5)) ; => 1.5
Since: Phase B2
json/get-string
(json/get-string [node :int] :cstr)
extract the string value from a string node.
| node | JSON string node |
The string cstr (owned by the node; do not free).
(json/get-string (json/string "hi")) ; => "hi"
Since: Phase B2
json/array-len
(json/array-len [node :int] :int)
return the number of elements in a JSON array node.
| node | JSON array node |
Number of elements as :int.
(json/array-len my-array) ; => 3
Since: Phase B2
json/array-get
(json/array-get [node :int i :int] :int)
get element i from a JSON array node.
| node | JSON array node | |
| i | zero-based index |
The JSON node at index i, or 0 if out of bounds.
(json/array-get my-array 0) ; => first element node
Since: Phase B2
json/get
(json/get [node :int key :cstr] :int)
look up a key in a JSON object node.
| node | JSON object node | |
| key | NUL-terminated key cstr |
A some option (:int) containing the value node, or none (0) if the key is absent.
(json/get my-obj "name") ; => some(<node>)
Since: Phase B2
json/get!
(json/get! [node :int key :cstr] :int)
look up a key in a JSON object, panicking if absent.
| node | JSON object node | |
| key | NUL-terminated key cstr |
The value node. Panics if the key is not found.
(json/get! my-obj "name") ; => <node>
Since: Phase B2
json/encode
(json/encode [node :int] :cstr)
serialize a JSON node tree to a NUL-terminated cstr.
| node | root JSON node |
A heap-allocated NUL-terminated JSON string. The caller must free.
(json/encode (json/int 42)) ; => "42"
Since: Phase B2
json/decode
(json/decode [s :cstr] :int)
parse a JSON string into a node tree.
| s | NUL-terminated JSON cstr to parse |
A heap-allocated JSON node tree on success, or 0 on parse error. Free with json/free when done.
(json/decode "{\"x\":1}") ; => <object node>
Since: Phase B2
json/free
(json/free [node :int] :void)
recursively free a JSON node tree.
| node | root JSON node to free (may be 0) |
(json/free my-node)
Since: Phase B2