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"
}
}
stardust_document *people = NULL;
uint32_t root = 0, ada = 0, age = 0;
int64_t result = 0;
uint32_t status = stardust_parse(people_text, STARDUST_JSON, &people);
if (status == STARDUST_OK) status = stardust_document_root(people, &root);
if (status == STARDUST_OK) status = stardust_document_member(people, root, (stardust_bytes){(const uint8_t *)"#_ada", 5}, &ada);
if (status == STARDUST_OK) status = stardust_document_member(people, ada, (stardust_bytes){(const uint8_t *)"age", 3}, &age);
if (status == STARDUST_OK) status = stardust_document_i64(people, age, &result);
stardust_document_release(people);
if (status != STARDUST_OK) return status;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 |
|---|---|---|
| stardust_parse | Status. document through output | parse(text, format) returns an owned document. |
| stardust_document_root | Status. node through output | document.root() returns the root node ID. |
| stardust_document_count | Status. count through output | document.count() returns the number of nodes. |
| read | Unavailable | document.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text. |
| stardust_document_read_many | Status. node_views through output | document.read_many(start, capacity) reads consecutive node views. |
| stardust_document_member | Status. node through output | document.member(id, key) returns the first matching object member ID. |
| children | Unavailable | document.children(id) iterates the node views of a node's children in order. |
| members | Unavailable | document.members(id) iterates the members of an object node in order, each with its key. |
| stardust_document_i64 | Status. integer through output | Read an exact signed integer. |
| stardust_document_f64 | Status. real through output | Read a finite floating-point number. |
| stardust_document_release | void | Release the document handle. |
stardust_parse
uint32_t stardust_parse(stardust_bytes source, uint32_t format, stardust_document **output);uint32_t stardust_document_error(const stardust_document *doc, stardust_diagnostic_view *output);Parameters
stardust_parse
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
source | stardust_bytes | Value | Required | Value passed to the call. |
format | uint32_t | Value | Required | The codec to use for this operation. |
output | stardust_document ** | Owned output | Required | Receives a library-owned handle. Release it after use. |
stardust_document_error
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
output | stardust_diagnostic_view * | Caller-owned output | Required | Receives a record. Its pointer fields follow the record's ownership contract. |
Errors
| Status | When |
|---|---|
STARDUST_INVALID_JSON | JSON text is malformed |
STARDUST_INVALID_ARGUMENT | the text is malformed, the format is unknown or a number is outside i64 or finite f64 |
STARDUST_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]stardust_document *result = NULL;
uint32_t status = stardust_parse(expression_text, STARDUST_DUST, &result);
if (status != STARDUST_OK) {stardust_document_release(result); return status;}{
"$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:
]
stardust_document *failed = NULL;
uint32_t status = stardust_parse(malformed_text, STARDUST_DUST, &failed);
stardust_diagnostic_view result = {0};
stardust_document_error(failed, &result);{
"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:
[
stardust_document *failed = NULL;
uint32_t status = stardust_parse(malformed_text, STARDUST_DUST, &failed);
stardust_diagnostic_view result = {0};
stardust_document_error(failed, &result);{
"code": "invalid_dust",
"message": "Unexpected_End at offset 2",
"path": null,
"field": null,
"entity": null,
"offset": 2,
"status": "invalid_argument"
}Also calls: stardust_document_error
stardust_document_root
uint32_t stardust_document_root(const stardust_document *doc, uint32_t *node);Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
node | uint32_t * | Caller-owned output | Required | Receives the scalar result. |
Errors
| Status | When |
|---|---|
STARDUST_INVALID_ARGUMENT | a required input or output pointer is NULL, or an input owner is not ready |
Examples
document/root
input_text holds this document:
{
"x": 41
}
stardust_document *source = NULL;
uint32_t root = 0;
uint32_t status = stardust_parse(input_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_root(source, &root);
stardust_document_release(source);
if (status != STARDUST_OK) return status;
int64_t result = root;0
stardust_document_count
uint32_t stardust_document_count(const stardust_document *doc, int64_t *count);Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
count | int64_t * | Caller-owned output | Required | Receives the scalar result. |
Errors
| Status | When |
|---|---|
STARDUST_INVALID_ARGUMENT | a required input or output pointer is NULL, or an input owner is not ready |
Examples
document/count
input_text holds this document:
{
"x": 41
}
stardust_document *source = NULL;
int64_t result = 0;
uint32_t status = stardust_parse(input_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_count(source, &result);
stardust_document_release(source);
if (status != STARDUST_OK) return status;2
read
Call stardust_document_read_many with capacity 1.
Also calls: stardust_document_read_many
stardust_document_read_many
uint32_t stardust_document_read_many(const stardust_document *doc, uint32_t start, int64_t capacity, stardust_node_view *output, int64_t *written);Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
start | uint32_t | Value | Required | The first node ID of the consecutive read. |
capacity | int64_t | Value | Required | The maximum number of node views to read. For output slices, the call uses the caller's slice length. |
output | stardust_node_view * | Caller-owned array | NULL | Writes at most capacity elements and reports their count in written. |
written | int64_t * | Caller-owned output | Required | Receives the scalar result. |
Errors
| Status | When |
|---|---|
STARDUST_INVALID_ARGUMENT | start is past the last node or capacity is negative |
Examples
document/read_many
input_text holds this document:
{
"x": 41
}
stardust_document *source = NULL;
uint32_t root = 0;
stardust_node_view view = {0};
int64_t written = 0;
uint32_t status = stardust_parse(input_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_root(source, &root);
if (status == STARDUST_OK) status = stardust_document_read_many(source, root, 1, &view, &written);
stardust_document_release(source);
if (status != STARDUST_OK) return status;
// The root object has no key or text, so the view borrows nothing.
stardust_node_view result = view;{
"kind": "object",
"boolean": false,
"parent": null,
"position": 0,
"first_child": 1,
"next_sibling": null,
"key": "",
"text": ""
}document/read_many
input_text holds this document:
{
"x": 41
}
stardust_document *source = NULL;
uint32_t root = 0;
stardust_node_view view = {0};
int64_t written = 0;
uint32_t status = stardust_parse(input_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_root(source, &root);
if (status == STARDUST_OK) status = stardust_document_read_many(source, root, 1, &view, &written);
stardust_document_release(source);
if (status != STARDUST_OK) return status;
// The root object has no key or text, so the view borrows nothing.
stardust_node_view result = view;{
"kind": "object",
"boolean": false,
"parent": null,
"position": 0,
"first_child": 1,
"next_sibling": null,
"key": "",
"text": ""
}stardust_document_member
uint32_t stardust_document_member(const stardust_document *doc, uint32_t object, stardust_bytes name, uint32_t *node);Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
object | uint32_t | Value | Required | Value passed to the call. |
name | stardust_bytes | Value | Required | Value passed to the call. |
node | uint32_t * | Caller-owned output | Required | Receives the scalar result. |
Errors
| Status | When |
|---|---|
STARDUST_NOT_FOUND | the object has no member with the key |
STARDUST_INVALID_ARGUMENT | id is not an object node of the document |
Examples
document/member
input_text holds this document:
{
"x": 41
}
stardust_document *source = NULL;
uint32_t root = 0, node = 0;
uint32_t status = stardust_parse(input_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_root(source, &root);
if (status == STARDUST_OK) status = stardust_document_member(source, root, (stardust_bytes){(const uint8_t *)"x", 1}, &node);
stardust_document_release(source);
if (status != STARDUST_OK) return status;
int64_t result = node;1
children
Read the first child, then follow next_sibling with stardust_document_read_many.
Also calls: stardust_document_read_many
members
Follow the children of an object node; each node view carries its key.
Also calls: stardust_document_read_many
stardust_document_i64
uint32_t stardust_document_i64(const stardust_document *doc, uint32_t node, int64_t *output);Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
node | uint32_t | Value | Required | Value passed to the call. |
output | int64_t * | Caller-owned output | Required | Receives the scalar result. |
Errors
| Status | When |
|---|---|
STARDUST_INVALID_ARGUMENT | the node is not an integer number |
Examples
document/i64
input_text holds this document:
{
"x": 41
}
stardust_document *source = NULL;
uint32_t root = 0, node = 0;
int64_t result = 0;
uint32_t status = stardust_parse(input_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_root(source, &root);
if (status == STARDUST_OK) status = stardust_document_member(source, root, (stardust_bytes){(const uint8_t *)"x", 1}, &node);
if (status == STARDUST_OK) status = stardust_document_i64(source, node, &result);
stardust_document_release(source);
if (status != STARDUST_OK) return status;41
stardust_document_f64
uint32_t stardust_document_f64(const stardust_document *doc, uint32_t node, double *output);Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | const stardust_document * | Borrowed handle | Required | Keep the handle alive until the call returns. |
node | uint32_t | Value | Required | Value passed to the call. |
output | double * | Caller-owned output | Required | Receives the scalar result. |
Errors
| Status | When |
|---|---|
STARDUST_INVALID_ARGUMENT | the node is not a number or its value overflows f64 |
Notes
Integer conversion can round values outside the exact f64 integer range.
Examples
document/f64
number_text holds this document:
1.0
stardust_document *source = NULL;
uint32_t root = 0;
double result = 0;
uint32_t status = stardust_parse(number_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_document_root(source, &root);
if (status == STARDUST_OK) status = stardust_document_f64(source, root, &result);
stardust_document_release(source);
if (status != STARDUST_OK) return status;1.0
stardust_document_release
void stardust_document_release(stardust_document *doc);Consumes the receiver handle.
Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | stardust_document * | Consumed handle | NULL | Ends ownership. Do not use the handle after this call. |
Examples
document/close
expression_text holds this document:
[
"+",
"?x",
1
]
stardust_document *source = NULL;
stardust_text *result = NULL;
uint32_t status = stardust_parse(expression_text, STARDUST_JSON, &source);
if (status == STARDUST_OK) status = stardust_serialize(source, STARDUST_JSON, 0, &result);
stardust_document_release(source);
if (status != STARDUST_OK) {stardust_text_release(result); return status;}{
"text": "[\"+\",\"?x\",1]"
}stardust_node_view
This record contains the result of read and read_many. The key and text fields borrow the document until it closes.
| Field | C type | Unit | Range | Absent | Meaning |
|---|---|---|---|---|---|
kind | uint32_t | None | Unsigned u32 | Always present | — |
boolean | uint32_t | None | Unsigned u32 | Always present | True only for true. |
parent | uint32_t | None | Unsigned u32 | STARDUST_DOCUMENT_NO_NODE | STARDUST_DOCUMENT_NO_NODE shows the root. |
position | uint32_t | Count | Unsigned u32 | Always present | Index within the parent container. |
first_child | uint32_t | None | Unsigned u32 | STARDUST_DOCUMENT_NO_NODE | STARDUST_DOCUMENT_NO_NODE shows that there is no child. |
next_sibling | uint32_t | None | Unsigned u32 | STARDUST_DOCUMENT_NO_NODE | STARDUST_DOCUMENT_NO_NODE shows that there is no next sibling. |
key | stardust_bytes | None | C type range | Always present | Member key of an object member, else empty. |
text | stardust_bytes | None | C type range | Always present | Value of a string, else empty. It can contain NUL. |
stardust_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 | C type | Range | Default | Absent | Meaning |
|---|---|---|---|---|---|
parent | uint32_t | Unsigned u32 | Required: node ID or STARDUST_DOCUMENT_NO_NODE | STARDUST_DOCUMENT_NO_NODE | STARDUST_DOCUMENT_NO_NODE adds the root. |
kind | uint32_t | Unsigned u32 | Zero | Always present | — |
boolean | uint32_t | Unsigned u32 | Zero | Always present | Value of a boolean node. |
number | uint32_t | Unsigned u32 | STARDUST_NUMBER_INTEGER (0) | Always present | Selects integer or real for a Number node. Zero selects Integer. |
key | stardust_bytes | C type range | Zero | Always present | Member key when the parent is an object. |
text | stardust_bytes | C type range | Zero | Always present | Value of a string node. |
integer | int64_t | Signed i64 | Zero | Always present | Contains the value when number is STARDUST_NUMBER_INTEGER. Otherwise, set integer to zero. |
real | double | C type range | Zero | Always present | Contains the finite value when number is STARDUST_NUMBER_FLOAT. Otherwise, set real to zero. |