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"
  }
}
python196 B
async with (await stardust.parse(people_text, "json")) as people:
    ada = (await people.member((await people.root()), "#_ada"))
    result = (await people.i64((await people.member(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.
Document.rootnodedocument.root() returns the root node ID.
Document.countcountdocument.count() returns the number of nodes.
Document.readnode_viewdocument.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text.
Document.read_manynode_viewsdocument.read_many(start, capacity) reads consecutive node views.
Document.membernodedocument.member(id, key) returns the first matching object member ID.
Document.childrennode_viewsdocument.children(id) iterates the node views of a node's children in order.
Document.membersnode_viewsdocument.members(id) iterates the members of an object node in order, each with its key.
Document.i64integerRead an exact signed integer.
Document.f64realRead a finite floating-point number.
Document.closenoneRelease the document handle.

parse

python84 B
def parse(text: str | bytes, format: str, *, library: str | None = None) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
textstr | bytesValueRequiredEncoded input bytes in the selected format. TRON is binary.
formatstrValueRequiredThe codec to use for this operation.
librarystr | NoneValueNoneLoaded native library used by this call.

Errors

StatusWhen
"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:

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]
python56 B
result = (await stardust.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
]
python139 B
try:
    async with (await stardust.parse(malformed_text, "dust")):
        pass
except stardust.StardustError as error:
    result = error
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
[
python139 B
try:
    async with (await stardust.parse(malformed_text, "dust")):
        pass
except stardust.StardustError as error:
    result = error
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

Document.root

python21 B
def root(self) -> int

Parameters

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

Errors

StatusWhen
"closed"the document is closed

Examples

document/root

input_text holds this document:

json14 B
{
  "x": 41
}
python97 B
async with (await stardust.parse(input_text, "json")) as input:
    result = (await input.root())
Result / 1 B
0

C exports: stardust_document_root

Document.count

python22 B
def count(self) -> int

Parameters

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

Errors

StatusWhen
"closed"the document is closed

Examples

document/count

input_text holds this document:

json14 B
{
  "x": 41
}
python98 B
async with (await stardust.parse(input_text, "json")) as input:
    result = (await input.count())
Result / 1 B
2

C exports: stardust_document_count

Document.read

python35 B
def read(self, id: int) -> NodeView

Parameters

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

Errors

StatusWhen
"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:

json14 B
{
  "x": 41
}
python117 B
async with (await stardust.parse(input_text, "json")) as input:
    result = (await input.read((await input.root())))
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

Document.read_many

python64 B
def read_many(self, start: int, capacity: int) -> list[NodeView]

Parameters

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

Errors

StatusWhen
"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:

json14 B
{
  "x": 41
}
python142 B
async with (await stardust.parse(input_text, "json")) as input:
    views = (await input.read_many((await input.root()), 1))
result = views[0]
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

Document.member

python42 B
def member(self, id: int, key: str) -> int

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.
keystrValueRequiredThe object member name to find.

Errors

StatusWhen
"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:

json14 B
{
  "x": 41
}
python124 B
async with (await stardust.parse(input_text, "json")) as input:
    result = (await input.member((await input.root()), "x"))
Result / 1 B
1

C exports: stardust_document_member

Document.children

python49 B
def children(self, id: int) -> Iterator[NodeView]

Parameters

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

Errors

StatusWhen
"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:

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"
  }
}
python142 B
async with (await stardust.parse(seed_text, "json")) as source:
    result = sum([1 async for view in source.children((await source.root()))])
Result / 1 B
4

C exports: stardust_document_read_many

Document.members

python60 B
def members(self, id: int) -> Iterator[tuple[str, NodeView]]

Parameters

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

Errors

StatusWhen
"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:

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"
  }
}
python135 B
async with (await stardust.parse(seed_text, "json")) as source:
    result, view = (await anext(source.members((await source.root()))))
Result / 21 B
{
  "text": "#_ada"
}

C exports: stardust_document_read_many

Document.i64

python29 B
def i64(self, id: int) -> int

Parameters

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

Errors

StatusWhen
"invalid_argument"the node is not an integer number
"closed"the document is closed

Examples

document/i64

input_text holds this document:

json14 B
{
  "x": 41
}
python153 B
async with (await stardust.parse(input_text, "json")) as input:
    x = (await input.member((await input.root()), "x"))
    result = (await input.i64(x))
Result / 2 B
41

C exports: stardust_document_i64

Document.f64

python31 B
def f64(self, id: int) -> float

Parameters

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

Errors

StatusWhen
"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

number_text holds this document:

json4 B
1.0
python120 B
async with (await stardust.parse(number_text, "json")) as number:
    result = (await number.f64((await number.root())))
Result / 3 B
1.0

C exports: stardust_document_f64

Document.close

python23 B
def close(self) -> None

Consumes the receiver handle.

Parameters

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

Examples

document/close

expression_text holds this document:

json23 B
[
  "+",
  "?x",
  1
]
python123 B
async with (await stardust.parse(expression_text, "json")) as expression:
    result = (await expression.serialize("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 are owned strings and remain valid after the document closes.

FieldTypeUnitAbsentMeaning
kindstrNone——
booleanboolNone—True only for true.
parentint | NoneNoneNone—
positionintCount—Index within the parent container.
first_childint | NoneNoneNone—
next_siblingint | NoneNoneNone—
keystrNone—Member key of an object member, else empty.
textstrNone—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
parentint | NoneNone—
kindstr——
booleanbool—Value of a boolean node.
numberint | float | NoneNoneValue of a number node.
keystr—Member key when the parent is an object.
textstr—Value of a string node.