Building documents

These operations construct a document one node at a time. For most operations, parse a document in your selected format. Use these operations only when you cannot parse the document from text.

  1. Create a builder.
  2. Add the nodes in preorder.
  3. Seal the builder to create an owned document.

Operations

OperationReturnsSummary
builderbuilderCreate an owned document builder.
addnodebuilder.add(node) appends one node and returns its preorder ID.
add_manynonebuilder.add_many(nodes) appends nodes in order in one native call.
sealdocumentbuilder.seal() transfers the constructed document into an independent owned document.
closenoneRelease the builder handle.

builder

odin67 B
builder :: proc(allocator := context.allocator) -> (Builder, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Out_Of_Memorynative memory is exhausted

Notes

Add nodes in depth-first preorder. An append failure makes the builder unusable.

Examples

builder

odin329 B
builder := stardust.builder() or_return
defer stardust.close(&builder)

object := stardust.add(&builder, {parent = stardust.NO_NODE, kind = .Object}) or_return
stardust.add(&builder, {parent = object, kind = .Number, key = "x", number = i64(41)}) or_return
result := stardust.seal(builder) or_return
defer stardust.close(&result)
Result / 13 B
{
  "x": 41
}

C exports: stardust_document_builder_create

add

odin96 B
add :: proc(builder: ^Builder, node: Node_Input, allocator := context.allocator) -> (u32, Error)

Requires exclusive use of the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
builder^BuilderBorrowed for the callRequiredThe builder owner for this operation. Keep it alive during the call.
nodeNode_InputValueRequiredThe next node's kind, parent and typed value.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_Argumentthe node breaks the preorder rules or has an invalid kind or number
.Closedthe builder is closed
.Out_Of_Memorynative memory is exhausted

Notes

The root has no parent. Each later parent must be an open container or one of its ancestors.

Examples

builder/add

odin329 B
builder := stardust.builder() or_return
defer stardust.close(&builder)

object := stardust.add(&builder, {parent = stardust.NO_NODE, kind = .Object}) or_return
stardust.add(&builder, {parent = object, kind = .Number, key = "x", number = i64(41)}) or_return
result := stardust.seal(builder) or_return
defer stardust.close(&result)
Result / 13 B
{
  "x": 41
}

Add a typed integer node

odin217 B
input := stardust.Node_Input{parent = stardust.NO_NODE, kind = .Number, number = i64(41)}
builder := stardust.builder() or_return
defer stardust.close(&builder)

stardust.add(&builder, input) or_return
result := input
Result / 103 B
{
  "parent": null,
  "kind": "number",
  "boolean": false,
  "number": 41,
  "key": "",
  "text": ""
}

C exports: stardust_document_builder_add_many, stardust_document_builder_error

add_many

odin97 B
add_many :: proc(builder: ^Builder, nodes: []Node_Input, allocator := context.allocator) -> Error

Requires exclusive use of the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
builder^BuilderBorrowed for the callRequiredThe builder owner for this operation. Keep it alive during the call.
nodes[]Node_InputValueRequiredNodes in depth-first preorder to append together.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_Argumenta node breaks the preorder rules or has an invalid kind or number
.Closedthe builder is closed
.Out_Of_Memorynative memory is exhausted

Examples

builder/add_many

odin299 B
builder := stardust.builder() or_return
defer stardust.close(&builder)

stardust.add_many(&builder, {
    {parent = stardust.NO_NODE, kind = .Object},
    {parent = 0, kind = .Number, key = "x", number = i64(41)},
}) or_return
result := stardust.seal(builder) or_return
defer stardust.close(&result)
Result / 13 B
{
  "x": 41
}

C exports: stardust_document_builder_add_many, stardust_document_builder_error

seal

odin83 B
seal :: proc(builder: Builder, allocator := context.allocator) -> (Document, Error)

Requires exclusive use of the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
builderBuilderBorrowed for the callRequiredThe builder owner for this operation. Keep it alive during the call.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_Argumentthe builder has no root or a container is still open
.Closedthe builder is closed
.Out_Of_Memorynative memory is exhausted

Notes

The builder accepts no more nodes after sealing.

Examples

builder/seal

odin329 B
builder := stardust.builder() or_return
defer stardust.close(&builder)

object := stardust.add(&builder, {parent = stardust.NO_NODE, kind = .Object}) or_return
stardust.add(&builder, {parent = object, kind = .Number, key = "x", number = i64(41)}) or_return
result := stardust.seal(builder) or_return
defer stardust.close(&result)
Result / 13 B
{
  "x": 41
}

C exports: stardust_document_builder_seal, stardust_document_builder_error

close

odin32 B
close :: proc(builder: ^Builder)

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
builder^BuilderConsumed ownerRequiredThe builder owner to release. Do not use it after this call.

Examples

builder/close

input_text holds this document:

json14 B
{
  "x": 41
}
odin149 B
builder := stardust.builder() or_return
stardust.close(&builder)

result := stardust.parse(input_text, .JSON) or_return
defer stardust.close(&result)
Result / 13 B
{
  "x": 41
}

C exports: stardust_document_builder_release