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:

json333 B
{
  "#_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"
  }
}
clojure156 B
(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"))))
Result / 2 B
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

OperationReturnsSummary
parsedocumentparse(text, format) returns an owned document.
rootnodedocument.root() returns the root node ID.
countcountdocument.count() returns the number of nodes.
readnode_viewdocument.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text.
read-manynode_viewsdocument.read_many(start, capacity) reads consecutive node views.
membernodedocument.member(id, key) returns the first matching object member ID.
childrennode_viewsdocument.children(id) iterates the node views of a node's children in order.
membersnode_viewsdocument.members(id) iterates the members of an object node in order, each with its key.
i64integerRead an exact signed integer.
f64realRead a finite floating-point number.
closenoneRelease the document handle.

parse

clojure13 B
[text format]
clojure21 B
[text format options]

Parameters

ParameterTypeOwnershipDefaultMeaning
textTextValueRequiredEncoded input bytes in the selected format. TRON is binary.
formatkeywordValueRequiredThe codec to use for this operation.
optionsmapValue{}Binding options map. The :library key selects the loaded native library. Omission uses the binding's default library.

Errors

StatusWhen
:invalid-jsonJSON text is malformed
:invalid-argumentthe text is malformed, the format is unknown or a number is outside i64 or finite f64
:out-of-memorynative memory is exhausted

Examples

Parse the shared DUST expression fixture

expression-text holds these bytes:

text278 B
// 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]
clojure32 B
(sd/parse expression-text :dust)
Result / 381 B
{
  "$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:

text1 B
]
clojure31 B
(sd/parse malformed-text :dust)
Result / 169 B
{
  "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:

text1 B
[
clojure31 B
(sd/parse malformed-text :dust)
Result / 169 B
{
  "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

clojure10 B
[document]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:closedthe document is closed

Examples

document/root

input-text holds this document:

json14 B
{
  "x": 41
}
clojure67 B
(with-open [source (sd/parse input-text :json)]
  (sd/root source))
Result / 1 B
0

C exports: stardust_document_root

count

clojure10 B
[document]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:closedthe document is closed

Examples

document/count

input-text holds this document:

json14 B
{
  "x": 41
}
clojure68 B
(with-open [source (sd/parse input-text :json)]
  (sd/count source))
Result / 1 B
2

C exports: stardust_document_count

read

clojure13 B
[document id]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idNodeValueRequiredA node ID within the receiver document.

Errors

StatusWhen
:invalid-argumentid is not a node of the document
:closedthe document is closed

Notes

Text contains string values. Read numbers with i64 or f64.

Examples

document/read

input-text holds this document:

json14 B
{
  "x": 41
}
clojure84 B
(with-open [source (sd/parse input-text :json)]
  (sd/read source (sd/root source)))
Result / 148 B
{
  "kind": "object",
  "boolean": false,
  "parent": null,
  "position": 0,
  "first_child": 1,
  "next_sibling": null,
  "key": "",
  "text": ""
}

C exports: stardust_document_read_many

read-many

clojure25 B
[document start capacity]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
startNodeValueRequiredThe first node ID of the consecutive read.
capacityCountValueRequiredThe maximum number of node views to read. For output slices, the call uses the caller's slice length.

Errors

StatusWhen
:invalid-argumentstart is past the last node or capacity is negative
:closedthe document is closed

Examples

document/read_many

input-text holds this document:

json14 B
{
  "x": 41
}
clojure99 B
(with-open [source (sd/parse input-text :json)]
  (first (sd/read-many source (sd/root source) 1)))
Result / 148 B
{
  "kind": "object",
  "boolean": false,
  "parent": null,
  "position": 0,
  "first_child": 1,
  "next_sibling": null,
  "key": "",
  "text": ""
}

C exports: stardust_document_read_many

member

clojure17 B
[document id key]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idNodeValueRequiredA node ID within the receiver document.
keyNameValueRequiredThe object member name to find.

Errors

StatusWhen
:not-foundthe object has no member with the key
:invalid-argumentid is not an object node of the document
:closedthe document is closed

Examples

document/member

input-text holds this document:

json14 B
{
  "x": 41
}
clojure90 B
(with-open [source (sd/parse input-text :json)]
  (sd/member source (sd/root source) "x"))
Result / 1 B
1

C exports: stardust_document_member

children

clojure8 B
[doc id]

Parameters

ParameterTypeOwnershipDefaultMeaning
docDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idNodeValueRequiredA node ID within the receiver document.

Errors

StatusWhen
:invalid-argumentid is not a node of the document
:closedthe 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:

json333 B
{
  "#_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"
  }
}
clojure99 B
(with-open [seed (sd/parse seed-text :json)]
  (count (into [] (sd/children seed (sd/root seed)))))
Result / 1 B
4

C exports: stardust_document_read_many

members

clojure13 B
[document id]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idNodeValueRequiredA node ID within the receiver document.

Errors

StatusWhen
:invalid-argumentid is not an object node of the document
:closedthe 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:

json333 B
{
  "#_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"
  }
}
clojure133 B
(with-open [seed (sd/parse seed-text :json)]
  (:key (reduce (fn [_ member] (reduced member)) nil (sd/members seed (sd/root seed)))))
Result / 21 B
{
  "text": "#_ada"
}

C exports: stardust_document_read_many

i64

clojure13 B
[document id]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idNodeValueRequiredA node ID within the receiver document.

Errors

StatusWhen
:invalid-argumentthe node is not an integer number
:closedthe document is closed

Examples

document/i64

input-text holds this document:

json14 B
{
  "x": 41
}
clojure106 B
(with-open [source (sd/parse input-text :json)]
  (sd/i64 source (sd/member source (sd/root source) "x")))
Result / 2 B
41

C exports: stardust_document_i64

f64

clojure13 B
[document id]

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idNodeValueRequiredA node ID within the receiver document.

Errors

StatusWhen
:invalid-argumentthe node is not a number or its value overflows f64
:closedthe document is closed

Notes

Integer conversion can round values outside the exact f64 integer range.

Examples

document/f64

float-text holds this document:

json4 B
1.0
clojure83 B
(with-open [source (sd/parse float-text :json)]
  (sd/f64 source (sd/root source)))
Result / 3 B
1.0

C exports: stardust_document_f64

close

clojure8 B
[handle]

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
handleDocumentConsumed ownerRequiredThe document owner to release. Do not use it after this call.

Examples

document/close

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]
clojure83 B
(with-open [source (sd/parse expression-text :json)]
  (sd/serialize source :json))
Result / 32 B
{
  "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.

FieldTypeUnitAbsentMeaning
kindValue-KindNone——
booleanBoolNone—True only for true.
parentNodeNonenil—
positionCountCount—Index within the parent container.
first_childNodeNonenil—
next_siblingNodeNonenil—
keyTextNone—Member key of an object member, else empty.
textTextNone—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.

FieldTypeAbsentMeaning
parentNodenil—
kindValue-Kind——
booleanBool—Value of a boolean node.
numberNumberAn absent keyValue of a number node.
keyText—Member key when the parent is an object.
textText—Value of a string node.