Search results

There are no results.

std.json.Parser

class pub Parser

A DOM parser for turning a stream of bytes into a JSON document.

This parser only supports parsing ByteArray values as input. If you need to parse very large documents, it's best to separate the objects on a per line basis, then parse the document one line at a time.

Nested arrays and objects

This parser enforces a limit on the number of nested arrays and objects. The default is sufficiently large that most documents won't result in an error being thrown. This limit is controlled by the field Parser.max_depth.

Fields

max_depth

let pub @max_depth: Int

The maximum number of nested values.

When parsing an array or object that exceeds this depth, an error is thrown.

Static methods

new

Show source code
Hide source code
fn pub static new[T: Bytes](input: ref T) -> Parser {
  Parser(PullParser.new(input), depth: 0, max_depth: 100)
}
fn pub static new[T: Bytes](input: ref T) -> Parser

Returns a new parser that will parse the given Bytes value.

Instance methods

parse

Show source code
Hide source code
fn pub move parse -> Result[Json, Error] {
  let result = try value

  # Only trailing whitespace is allowed.
  match try @pull.peek {
    case Some(val) -> throw @pull.unexpected(val)
    case _ -> {}
  }

  Result.Ok(result)
}
fn pub move parse -> Result[Json, Error]

Parses the input into a JSON object.

If the input is invalid JSON, this method throws an Error.

Examples

import std.json (Parser)

let parser = Parser.new('[10, 20]')

parser.parse.get # => Json.Array([Json.Int(10), Json.Int(20)])