Browse the docs

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#

  • WriterError if the Writer fails. Bytes that it accepted before the failure stay written.

  • JsonError from JsonOutput.writeNumber for text that is not a JSON number, and from JsonOptions.child when the nesting is too deep.

Examples#

Choose two-space indentation#

silk
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#

silk
pub struct JsonOptions

The 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#

silk
pub indent: usize

The number of spaces for each level of indentation. 0 means compact output on one line.

Field depth#

silk
pub depth: usize

The depth of the current array or object. 0 at the top level.

Field depthLimit#

silk
pub depthLimit: usize

The largest depth that the writers allow.

Implementation Copy for JsonOptions#

silk
impl Copy for JsonOptions

JsonOutput#

silk
pub struct JsonOutput

The 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#

silk
impl JsonOptions

Associated function compact#

silk
pub fn compact() -> JsonOptions

Returns options for compact output on one line, with a depth limit of 64 (MAX_DEPTH).

Associated function indented#

silk
pub fn indented(width: usize) -> JsonOptions

Returns options for output with width spaces for each level, with a depth limit of 64.

Method child#

silk
pub effect fn child(self: &JsonOptions) -> JsonOptions ! JsonError

Returns the options for a nested array or object, one level deeper.

Errors#

  • JsonError with reason DepthExceeded if the depth is already at the limit.

Implementation JsonOutput#

silk
impl JsonOutput

Associated function writeString#

silk
pub effect fn writeString(value: string) -> () ! WriterError ? &mut Writer

Writes value as a quoted JSON string. It escapes ", \, and control characters.

Errors#

  • WriterError if the Writer fails. Bytes that it accepted before the failure stay written.

Associated function writeNumber#

silk
pub effect fn writeNumber(value: string) -> () ! JsonError | WriterError ? &mut Writer

Writes value as a JSON number, without changes, after it checks that value is a valid JSON number.

Errors#

  • JsonError with reason InvalidNumber if value is not a valid JSON number, such as "NaN" or "01". Nothing is written.

  • WriterError if the Writer fails. Bytes that it accepted before the failure stay written.

Associated function separator#

silk
pub effect fn separator(options: &JsonOptions, first: bool) -> () ! WriterError ? &mut Writer

Writes a comma if first is false, then a newline and indentation if indentation is on.

Associated function line#

silk
pub effect fn line(options: &JsonOptions) -> () ! WriterError ? &mut Writer

Writes a newline and the indentation for the current depth. Writes nothing for compact output.

Associated function colon#

silk
pub effect fn colon(options: &JsonOptions) -> () ! WriterError ? &mut Writer

Writes : 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.

View the source of this page