Json v0.1.0
JSON parsing and serialization
Installation
Add Json to the [Dependencies] table of your package's Rux.toml:
[Dependencies]
Json = { Namespace = "Rux", Version = "0.1.0" }
Standard packages are declared by hand: the registry does not serve them, so rux add Rux/Json cannot add them.
Dependencies
Guide
What it provides
- A document model — move-only
JsonValuetrees with explicit<-transfers and deterministic~Typecleanup, covering the six kinds RFC 8259 defines. Objects keep arrival order, and numbers keep their original text, so an identifier past 2^53 comes back as itself rather than as the nearest double. - A parser — strict RFC 8259, refusing every JavaScript-only form. Duplicate object names are rejected by default, and the other three policies are chosen per call.
- A document or a reason, never both —
ParseandParseWithreturnJsonValue ! JsonParseError. A refusal hands back no value at all, which is the distinction the old shape could not make: it answered a refused parse withnull, andnullis a perfectly good document, so a caller who did not read the outcome beside it could not tell one from the other. Whatever the parser had built when it stopped is released rather than handed over, so a caller is never left holding a partial tree.match Parse(allocator, bytes) { .Success(document) => Use(document), .Failure(reason) => Report(reason.Offset(), reason) } - Failures carry where, and the lexer's reason with them — every case of
JsonParseErrorholds the zero-based byte offset where parsing stopped, reachable throughText::ParseFailure, andLexicalnests theJsonLexErrorinside itself so a caller holding the failure cannot be holding half of it.Displaygives the description andDebugnames the case with its position:JsonParseError::Lexical(MalformedNumber, 1). - Limits — depth, bytes, string length and container size, each refused under its own positioned reason, so a document written to exhaust the stack or the heap is refused while there is still room to refuse it. A limit the parser measures arrives as
TooDeeporTooManyEntries; one the lexer measures arrives nested insideLexical. - Streaming — events in document order from a stored
Rux/Ioreader handle, with a move-only owned buffer bounded by the largest token rather than by the document and safe operations borrowed through references.JsonEventReader::Failureanswers the same positionedJsonParseErrorthe tree parser does, as an optional, so the two report a refusal the same way; its offset counts from the start of the document, not from the start of the buffer. A source whoseReadfails with anything but the end of the stream stops the reader withJsonParseError::Source, andJsonEventReader::SourceErrorhands back theIoErrorthe source reported, so a broken stream is never mistaken for a malformed document. - Writers — compact and pretty, borrowing values without copying, escaping exactly what the grammar requires and no more, and refusing to write a NaN or an infinity because JSON cannot spell one. They report
! FormatError, the same contract every writer in the workspace uses, so a document goes into anything aTextWriterreaches with no translation at the seam. What the destination said travels unchanged: a buffer that is merely full reportsInsufficientCapacityand a stream that has died reportsWriterFailure, whereJsonWriteErrorhad one case standing for both. A non-finite number, and a document nested pastMaximumWriteDepth, areValueOutOfRange— the value is good and the requested form cannot hold it.
Index
Types
| Name | Summary |
|---|---|
JsonEvent | What just happened in the document. |
JsonEventReader | Reads a document as events, pulling bytes as it needs them. |
JsonTokenKind | What kind of token this is. |
JsonLexError | Why lexing stopped. |
JsonLimits | What a caller will accept from a document. |
JsonToken | One token, and where in the input it came from. |
JsonLexer | A JSON lexer over a byte slice. |
DuplicateKeyPolicy | What to do about an object containing the same name twice. |
JsonParseError | Why parsing stopped, and where. |
JsonParser | A parser over a document. |
JsonKind | Which kind of JSON value this is. |
JsonNumber | A JSON number, held as the text it was written with. |
JsonMember | A name-value pair inside an object. |
JsonValue | A JSON value: one of the six kinds, owning whatever it holds. |
JsonStyle | How a document should be laid out. |
Functions
| Name | Summary |
|---|---|
Parse | Parses input into a value, rejecting duplicate names. |
ParseWith | Parses input under chosen limits and a chosen duplicate-name policy. |
WriteEscaped | Writes the escaped form of text, including its surrounding quotes. |
WriteValue | Writes value to writer in the given style. |
Constants
| Name | Summary |
|---|---|
MaximumWriteDepth | How deep a document may nest before the writer refuses. |