No matching definitions.

tur/json

stdlib/json.tur

JSON encode and decode with no external dependencies.

Since: Phase B1

defn

json/null

(json/null :int)

create a JSON null node.

A heap-allocated JSON null node.

(json/null)  ; => <null node>

Since: Phase B2

defn

json/bool

(json/bool [v :int] :int)

create a JSON boolean node.

v0 for false, non-zero for true

A heap-allocated JSON bool node.

(json/bool 1)  ; => <true node>

Since: Phase B2

defn

json/int

(json/int [v :int] :int)

create a JSON integer node.

vthe integer value

A heap-allocated JSON int node.

(json/int 42)  ; => <42 node>

Since: Phase B2

defn

json/float

(json/float [v :float] :int)

create a JSON float node.

vthe float value

A heap-allocated JSON float node.

(json/float 3.14)  ; => <3.14 node>

Since: Phase B2

defn

json/string

(json/string [s :cstr] :int)

create a JSON string node from a cstr.

sNUL-terminated cstr (copied internally)

A heap-allocated JSON string node owning a copy of s.

(json/string "hello")  ; => <"hello" node>

Since: Phase B2

defn

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

defn

json/array-push

(json/array-push [arr :int elem :int] :int)

append an element to a JSON array node.

arrJSON array node from json/array-new
elemJSON node to append

The same arr pointer (mutated in place).

(json/array-push arr (json/int 1))

Since: Phase B2

defn

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

defn

json/object-put

(json/object-put [obj :int key :cstr val :int] :int)

add or update a key-value pair in a JSON object.

objJSON object node
keyNUL-terminated key cstr (copied internally)
valJSON value node for the key

The same obj pointer (mutated in place).

(json/object-put obj "x" (json/int 1))

Since: Phase B2

defn

json/type

(json/type [node :int] :int)

return the type tag of a JSON node.

nodeJSON node

0=null 1=bool 2=int 3=float 4=string 5=array 6=object

(json/type (json/int 5))  ; => 2

Since: Phase B2

defn

json/get-bool

(json/get-bool [node :int] :bool)

extract the boolean value from a bool node.

nodeJSON bool node

true or false.

(json/get-bool (json/bool 1))  ; => true

Since: Phase B2

defn

json/get-int

(json/get-int [node :int] :int)

extract the integer value from an int node.

nodeJSON int node

The integer value as :int.

(json/get-int (json/int 42))  ; => 42

Since: Phase B2

defn

json/get-float

(json/get-float [node :int] :float)

extract the float value from a float node.

nodeJSON float node

The float value as :float.

(json/get-float (json/float 1.5))  ; => 1.5

Since: Phase B2

defn

json/get-string

(json/get-string [node :int] :cstr)

extract the string value from a string node.

nodeJSON string node

The string cstr (owned by the node; do not free).

(json/get-string (json/string "hi"))  ; => "hi"

Since: Phase B2

defn

json/array-len

(json/array-len [node :int] :int)

return the number of elements in a JSON array node.

nodeJSON array node

Number of elements as :int.

(json/array-len my-array)  ; => 3

Since: Phase B2

defn

json/array-get

(json/array-get [node :int i :int] :int)

get element i from a JSON array node.

nodeJSON array node
izero-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

defn

json/get

(json/get [node :int key :cstr] :int)

look up a key in a JSON object node.

nodeJSON object node
keyNUL-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

defn

json/get!

(json/get! [node :int key :cstr] :int)

look up a key in a JSON object, panicking if absent.

nodeJSON object node
keyNUL-terminated key cstr

The value node. Panics if the key is not found.

(json/get! my-obj "name")  ; => <node>

Since: Phase B2

defn

json/encode

(json/encode [node :int] :cstr)

serialize a JSON node tree to a NUL-terminated cstr.

noderoot JSON node

A heap-allocated NUL-terminated JSON string. The caller must free.

(json/encode (json/int 42))  ; => "42"

Since: Phase B2

defn

json/decode

(json/decode [s :cstr] :int)

parse a JSON string into a node tree.

sNUL-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

defn

json/free

(json/free [node :int] :void)

recursively free a JSON node tree.

noderoot JSON node to free (may be 0)
(json/free my-node)

Since: Phase B2