silk.json.output.
Low-level JSON output: strings, numbers, and punctuation, and the JsonOptions for layout.
When to use#
Use JsonOptions.compact or JsonOptions.indented to choose the layout for
Json.serialize and Json.writeWith. Use JsonOutput directly only when you write JSON by
hand, for example in a Serialize implementation that JsonObject cannot express.
Details#
Each operation writes to the Writer at once. It does not flush and does not allocate.
Errors#
WriterErrorif theWriterfails. Bytes that it accepted before the failure stay written.JsonErrorfromJsonOutput.writeNumberfor text that is not a JSON number, and fromJsonOptions.childwhen the nesting is too deep.
Examples#
Choose two-space indentation#
import silk.json.output { JsonOptions }
pub fn main() -> i32 {
let options = JsonOptions.indented(2)
if options.indent != 2 || options.depthLimit != 64 {
return 1
}
return 0
}Import as JsonOutput with import silk.json.output { JsonOutput }.
Public declarations: 2.
JsonOptions#
pub struct JsonOptionsThe layout and depth limit for JSON output.
Details#
Make one with JsonOptions.compact or JsonOptions.indented and pass it to
Json.serialize or Json.writeWith. The writers make a child with JsonOptions.child for
each nested array or object.
Field indent#
pub indent: usizeThe number of spaces for each level of indentation. 0 means compact output on one line.
Field depth#
pub depth: usizeThe depth of the current array or object. 0 at the top level.
Field depthLimit#
pub depthLimit: usizeThe largest depth that the writers allow.
Implementation Copy for JsonOptions#
impl Copy for JsonOptionsJsonOutput#
pub struct JsonOutputThe owner type of the low-level output operations. It has no fields and is never constructed.
When to use#
Use its operations only when you write JSON by hand, for example in a Serialize
implementation that JsonObject cannot express.
Details#
Each operation writes to the Writer at once. It does not flush and does not allocate. If the
Writer fails, the bytes that it accepted before the failure stay written.
Implementation JsonOptions#
impl JsonOptionsAssociated function compact#
pub fn compact() -> JsonOptionsReturns options for compact output on one line, with a depth limit of 64 (MAX_DEPTH).
Associated function indented#
pub fn indented(width: usize) -> JsonOptionsReturns options for output with width spaces for each level, with a depth limit of 64.
Method child#
pub effect fn child(self: &JsonOptions) -> JsonOptions ! JsonErrorReturns the options for a nested array or object, one level deeper.
Errors#
JsonErrorwith reasonDepthExceededif the depth is already at the limit.
Implementation JsonOutput#
impl JsonOutputAssociated function writeString#
pub effect fn writeString(value: string) -> () ! WriterError ? &mut WriterWrites value as a quoted JSON string. It escapes ", \, and control characters.
Errors#
WriterErrorif theWriterfails. Bytes that it accepted before the failure stay written.
Associated function writeNumber#
pub effect fn writeNumber(value: string) -> () ! JsonError | WriterError ? &mut WriterWrites value as a JSON number, without changes, after it checks that value is a valid JSON
number.
Errors#
JsonErrorwith reasonInvalidNumberifvalueis not a valid JSON number, such as"NaN"or"01". Nothing is written.WriterErrorif theWriterfails. Bytes that it accepted before the failure stay written.
Associated function separator#
pub effect fn separator(options: &JsonOptions, first: bool) -> () ! WriterError ? &mut WriterWrites a comma if first is false, then a newline and indentation if indentation is on.
Associated function line#
pub effect fn line(options: &JsonOptions) -> () ! WriterError ? &mut WriterWrites a newline and the indentation for the current depth. Writes nothing for compact output.
Associated function colon#
pub effect fn colon(options: &JsonOptions) -> () ! WriterError ? &mut WriterWrites : after a key. It adds a space if indentation is on.
Category: Data formats.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.