Documents
The example parses the people document in your selected format. It then reads Ada's age through the document nodes. Typed reads give you these values without serializing the document.
Example
Read Ada's age
people-text holds this document:
{
"#_ada": {
"name": "Ada",
"role": "engineer",
"age": 36
},
"#_bob": {
"name": "Bob",
"role": "engineer",
"age": 41,
"mentor": {
"#link": "#_ada"
}
},
"#_cy": {
"name": "Cy",
"role": "designer",
"age": 29
},
"#_tx": {
"author": "smoke",
"reason": "seed"
}
}
(with-open [people (sd/parse people-text :json)]
(let [ada (sd/member people (sd/root people) "#_ada")]
(sd/i64 people (sd/member people ada "age"))))36
Numbers
A document number is an i64 integer or a finite f64 value. The number type defines both.
- An integer read requires an integer node. A floating-point node can cause the read to fail.
- Choose the typed read that matches the node that you read.
Operations
| Operation | Returns | Summary |
|---|---|---|
| parse | document | parse(text, format) returns an owned document. |
| root | node | document.root() returns the root node ID. |
| count | count | document.count() returns the number of nodes. |
| read | node_view | document.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text. |
| read-many | node_views | document.read_many(start, capacity) reads consecutive node views. |
| member | node | document.member(id, key) returns the first matching object member ID. |
| children | node_views | document.children(id) iterates the node views of a node's children in order. |
| members | node_views | document.members(id) iterates the members of an object node in order, each with its key. |
| i64 | integer | Read an exact signed integer. |
| f64 | real | Read a finite floating-point number. |
| close | none | Release the document handle. |
parse
[text format][text format options]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
text | Text | Value | Required | Encoded input bytes in the selected format. TRON is binary. |
format | keyword | Value | Required | The codec to use for this operation. |
options | map | Value | {} | Binding options map. The :library key selects the loaded native library. Omission uses the binding's default library. |
Errors
| Status | When |
|---|---|
:invalid-json | JSON text is malformed |
:invalid-argument | the text is malformed, the format is unknown or a number is outside i64 or finite f64 |
:out-of-memory | native memory is exhausted |
Examples
Parse the shared DUST expression fixture
expression-text holds these bytes:
// One expression built once and evaluated against several inputs. The
// evaluate examples load this file, build `expression`, evaluate it with each
// of `inputs` in order and compare each result with `expected`.
expression [+ ?x 1]
inputs [{x 1} {x 41}]
expected [2 42](sd/parse expression-text :dust){
"$comment": "One expression built once and evaluated against several inputs. The\nevaluate examples load this file, build `expression`, evaluate it with each\nof `inputs` in order and compare each result with `expected`.",
"expression": [
"+",
"?x",
1
],
"inputs": [
{
"x": 1
},
{
"x": 41
}
],
"expected": [
2,
42
]
}Report a malformed byte at offset zero
malformed-text holds these bytes:
]
(sd/parse malformed-text :dust){
"code": "invalid_dust",
"message": "Invalid_Syntax at offset 0",
"path": null,
"field": null,
"entity": null,
"offset": 0,
"status": "invalid_argument"
}Report malformed DUST
malformed-text holds these bytes:
[
(sd/parse malformed-text :dust){
"code": "invalid_dust",
"message": "Unexpected_End at offset 2",
"path": null,
"field": null,
"entity": null,
"offset": 2,
"status": "invalid_argument"
}C exports: stardust_parse, stardust_document_error
root
[document]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
:closed | the document is closed |
Examples
document/root
input-text holds this document:
{
"x": 41
}
(with-open [source (sd/parse input-text :json)]
(sd/root source))0
C exports: stardust_document_root
count
[document]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
:closed | the document is closed |
Examples
document/count
input-text holds this document:
{
"x": 41
}
(with-open [source (sd/parse input-text :json)]
(sd/count source))2
C exports: stardust_document_count
read
[document id]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | Node | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
:invalid-argument | id is not a node of the document |
:closed | the document is closed |
Notes
Text contains string values. Read numbers with i64 or f64.
Examples
document/read
input-text holds this document:
{
"x": 41
}
(with-open [source (sd/parse input-text :json)]
(sd/read source (sd/root source))){
"kind": "object",
"boolean": false,
"parent": null,
"position": 0,
"first_child": 1,
"next_sibling": null,
"key": "",
"text": ""
}C exports: stardust_document_read_many
read-many
[document start capacity]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
start | Node | Value | Required | The first node ID of the consecutive read. |
capacity | Count | Value | Required | The maximum number of node views to read. For output slices, the call uses the caller's slice length. |
Errors
| Status | When |
|---|---|
:invalid-argument | start is past the last node or capacity is negative |
:closed | the document is closed |
Examples
document/read_many
input-text holds this document:
{
"x": 41
}
(with-open [source (sd/parse input-text :json)]
(first (sd/read-many source (sd/root source) 1))){
"kind": "object",
"boolean": false,
"parent": null,
"position": 0,
"first_child": 1,
"next_sibling": null,
"key": "",
"text": ""
}C exports: stardust_document_read_many
member
[document id key]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | Node | Value | Required | A node ID within the receiver document. |
key | Name | Value | Required | The object member name to find. |
Errors
| Status | When |
|---|---|
:not-found | the object has no member with the key |
:invalid-argument | id is not an object node of the document |
:closed | the document is closed |
Examples
document/member
input-text holds this document:
{
"x": 41
}
(with-open [source (sd/parse input-text :json)]
(sd/member source (sd/root source) "x"))1
C exports: stardust_document_member
children
[doc id]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | Node | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
:invalid-argument | id is not a node of the document |
:closed | the document is closed |
Notes
Keep the document open during iteration. A scalar has no children. A read failure is reported once and ends iteration.
Examples
document/children
seed-text holds this document:
{
"#_ada": {
"name": "Ada",
"role": "engineer",
"age": 36
},
"#_bob": {
"name": "Bob",
"role": "engineer",
"age": 41,
"mentor": {
"#link": "#_ada"
}
},
"#_cy": {
"name": "Cy",
"role": "designer",
"age": 29
},
"#_tx": {
"author": "smoke",
"reason": "seed"
}
}
(with-open [seed (sd/parse seed-text :json)]
(count (into [] (sd/children seed (sd/root seed)))))4
C exports: stardust_document_read_many
members
[document id]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | Node | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
:invalid-argument | id is not an object node of the document |
:closed | the document is closed |
Notes
Duplicate keys appear in document order. Keep the document open during iteration. A read failure is reported once and ends iteration.
Examples
document/members
seed-text holds this document:
{
"#_ada": {
"name": "Ada",
"role": "engineer",
"age": 36
},
"#_bob": {
"name": "Bob",
"role": "engineer",
"age": 41,
"mentor": {
"#link": "#_ada"
}
},
"#_cy": {
"name": "Cy",
"role": "designer",
"age": 29
},
"#_tx": {
"author": "smoke",
"reason": "seed"
}
}
(with-open [seed (sd/parse seed-text :json)]
(:key (reduce (fn [_ member] (reduced member)) nil (sd/members seed (sd/root seed))))){
"text": "#_ada"
}C exports: stardust_document_read_many
i64
[document id]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | Node | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
:invalid-argument | the node is not an integer number |
:closed | the document is closed |
Examples
document/i64
input-text holds this document:
{
"x": 41
}
(with-open [source (sd/parse input-text :json)]
(sd/i64 source (sd/member source (sd/root source) "x")))41
C exports: stardust_document_i64
f64
[document id]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | Node | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
:invalid-argument | the node is not a number or its value overflows f64 |
:closed | the document is closed |
Notes
Integer conversion can round values outside the exact f64 integer range.
Examples
document/f64
float-text holds this document:
1.0
(with-open [source (sd/parse float-text :json)]
(sd/f64 source (sd/root source)))1.0
C exports: stardust_document_f64
close
[handle]Consumes the receiver handle.
Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
handle | Document | Consumed owner | Required | The document owner to release. Do not use it after this call. |
Examples
document/close
expression-text holds this document:
[
"+",
"?x",
1
]
(with-open [source (sd/parse expression-text :json)]
(sd/serialize source :json)){
"text": "[\"+\",\"?x\",1]"
}C exports: stardust_document_release
node_view
This record contains the result of read and read_many. The key and text fields borrow the document until it closes.
| Field | Type | Unit | Absent | Meaning |
|---|---|---|---|---|
kind | Value-Kind | None | — | — |
boolean | Bool | None | — | True only for true. |
parent | Node | None | nil | — |
position | Count | Count | — | Index within the parent container. |
first_child | Node | None | nil | — |
next_sibling | Node | None | nil | — |
key | Text | None | — | Member key of an object member, else empty. |
text | Text | None | — | Value of a string, else empty. It can contain NUL. |
node_input
This record supplies a node for add and add_many. The number field holds a typed i64 or f64 value. Go uses I64/F64, Rust and Odin use Number, Clojure uses long or double, and Python uses int or float. This value sets the ABI number kind and the integer or real field.
| Field | Type | Absent | Meaning |
|---|---|---|---|
parent | Node | nil | — |
kind | Value-Kind | — | — |
boolean | Bool | — | Value of a boolean node. |
number | Number | An absent key | Value of a number node. |
key | Text | — | Member key when the parent is an object. |
text | Text | — | Value of a string node. |