Parse functionv0.1.0

Source
Parses input into a value, rejecting duplicate names.
pub func Parse(
    allocator: Allocator,
    input: char8[..]
) -> JsonValue ! JsonParseError

The default limits apply. A failure gives back the reason and its position and no value at all, so a document that is not a document cannot be mistaken for one that is — which the old shape allowed, since it answered a refused parse with null, and null is a document. Fails with any case of JsonParseError, positioned at the byte where parsing stopped.

Parameters

NameTypeDescription
allocatorAllocatorwhere the value is built
inputchar8[..]the document, as UTF-8 bytes

Returns: the document, or the failure that stopped it

DuplicateKeyPolicy

pub enum DuplicateKeyPolicy: int32 {
    Reject = 0,
    KeepFirst = 1,
    KeepLast = 2,
    KeepAll = 3
}

What to do about an object containing the same name twice.

Source

Cases

NameValueDescription
Reject0Refuse the document. The default, and the only policy that cannot be used to make two readers disagree.
KeepFirst1Keep the first occurrence and discard the rest.
KeepLast2Keep the last occurrence, which is what most JavaScript-derived parsers do.
KeepAll3Keep every occurrence, in order. The document is preserved exactly, and lookup finds the first.

ParseWith

pub func ParseWith(
    allocator: Allocator,
    input: char8[..],
    limits: JsonLimits,
    duplicates: DuplicateKeyPolicy
) -> JsonValue ! JsonParseError

Parses input under chosen limits and a chosen duplicate-name policy. The long form of Parse, for a caller who needs something other than the defaults. The policy is a parameter rather than a mutable setting on a shared parser, so two documents read under different policies in the same program cannot influence one another. Fails with any case of JsonParseError, positioned at the byte where parsing stopped. A limit that the document exceeds arrives as TooDeep or TooManyEntries from the parser, or nested inside Lexical when it was the lexer that measured it.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the value is built
inputchar8[..]the document, as UTF-8 bytes
limitsJsonLimitswhat to accept
duplicatesDuplicateKeyPolicywhat to do about an object naming the same member twice

Returns: the document, or the failure that stopped it