Antra

Standardbibliothek

Antra::JSON

import Antra::JSON

JSON parsing utilities for Antra.

Tokenization and parsing happen natively in Antra::Native::JSON.

Überblick#

JSON.parse(json)
JSON.parse(json).!
JSON.tokenize(json)
JSON.tokenize(json).!

JSON.parse(...) liefert bei ungueltigem JSON strukturierte %Err[%Error{...}]; dekodiert wird nativ (Antra::Native::JSON). JSON.tokenize(...) bleibt vorerst der einfachere Tokenizer-/Diagnosepfad.

Funktionen#

fn encode(value)

Encodes an Antra value as a JSON string.

Returns %Ok[text] or %Err[%Error{...}].

Example:

JSON.encode(%{name: "Ada", active: true})
JSON.encode([1, 2, 3])
fn parse(json, opts = [])

Parses a JSON text and returns %Ok[value] or %Err[%Error{...}].

Example:

JSON.parse('{"name":"Ada","active":true}')
JSON.parse('{"name":"Ada"}', [as_strings: true])
JSON.parse('{"catchPhrase":"..."}', [as_keywords: true, normalize_keys: :snake_case])
fn tokenize(json)

Tokenizes a JSON text into a list of tagged tokens.

This is mainly useful for debugging and for understanding how the parser consumes the input step by step.

Example:

JSON.tokenize('{"x": 1}')

Quelle dieser Seite