v0.4.0

JsonValue structv0.1.0

Source
A JSON value: one of the six kinds, owning whatever it holds.
pub struct JsonValue {
    // private fields
}

Move-only, because it owns strings and nested values. The allocator it was built with is carried along, so a subtree can be released without the caller having to remember where it came from — which is what makes a document assembled from several sources safe to take apart. The tag is private: it is read with Kind and set only together with its payload, so the two never disagree. The payload is read with the matching accessor; asking for the wrong one returns none rather than reinterpreting the bytes.

Associated functions

Null

pub func Null(allocator: Allocator) -> JsonValue

The literal null.

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: the null value

Source

Boolean

pub func Boolean(
    allocator: Allocator,
    value: bool
) -> JsonValue

A boolean value.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
valueboolthe value to act on

Returns: that boolean as a value

Source

Number

pub func Number(
    allocator: Allocator,
    text: char8[..]
) -> JsonValue throws JsonError

A number, from the text it is written with, checked against JSON's number grammar as JsonNumber does. Text the grammar refuses, such as 01, 1e or NaN, is MalformedNumber, and no value is made.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
textchar8[..]the number's text, borrowed for the call

Returns: that text as a number value; MalformedNumber when the text is not a JSON number

Source

Text

pub func Text(
    allocator: Allocator,
    value: char8[..]
) -> JsonValue throws TextError

A string value.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
valuechar8[..]the value to act on

Returns: that text as a string value, or InvalidUtf8 when it is not text

Source

Array

pub func Array(allocator: Allocator) -> JsonValue

An empty array.

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: an empty array

Source

Object

pub func Object(allocator: Allocator) -> JsonValue

An empty object.

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: an empty object

Source

Methods

Kind

pub func Kind(&self) -> JsonKind

Which kind this value is.

Returns: which of the six JSON kinds this is

Source

IsNull

pub func IsNull(&self) -> bool

Whether this is null.

Returns: whether this is the null value

Source

AsBoolean

pub func AsBoolean(&self) -> bool?

The boolean payload, or none when this is not a boolean.

Returns: the boolean, or none when this is not one

Source

AsNumber

pub func AsNumber(&self) -> &JsonNumber?

The number payload, borrowed, or none when this is not a number.

Returns: the number, or none when this is not one

Source

AsText

pub func AsText(&self) -> StringView?

The string payload, or none when this is not a string.

Returns: the string, or none when this is not one

Source

Length

pub func Length(&self) -> uint?

How many elements an array holds, or members an object holds; none for any other kind.

Returns: how many elements or members this holds, or none when it is neither an array nor an object

Source

Push

pub func Push(&var self, value: JsonValue) throws JsonError

Appends value to an array, taking ownership of it. Throws WrongKind when this is not an array; value is consumed either way, so a caller does not have to decide who owns it on failure. Terminates the program when the array cannot grow.

NameTypeDescription
valueJsonValuethe value to act on

Source

Insert

pub func Insert(
    &var self,
    name: String,
    value: JsonValue
) throws JsonError

Appends a member to an object, taking ownership of both name and value. The order members are added in is the order they are kept and written, which is what makes a document round-trip recognisably. Nothing here rejects a duplicate name — that policy lives in the parser, where the caller can choose it. Throws WrongKind when this is not an object; name and value are consumed either way. Terminates the program when the object cannot grow.

NameTypeDescription
nameStringthe member's name
valueJsonValuethe value to act on

Source

At

pub func At(&self, index: uint) -> &JsonValue?

The element at index, borrowed, or none when there is none. self stays borrowed while the reference is in use.

NameTypeDescription
indexuintwhich one, counting from zero

Returns: the element at index, or none when there is none

Source

Find

pub func Find(&self, name: StringView) -> &JsonValue?

The value of the member named name, borrowed, or none when there is none. With duplicate names present — which only a permissive parser policy allows — this finds the first, since that is the one a reader encounters.

NameTypeDescription
nameStringViewthe member's name

Returns: the first member with that name, or none when there is none

Source

MemberAt

pub func MemberAt(&self, index: uint) -> &JsonMember?

The member at index, borrowed, or none when there is none.

NameTypeDescription
indexuintwhich one, counting from zero

Returns: the member at index, or none when there is none

Source

RemoveFirst

pub func RemoveFirst(
    &var self,
    name: StringView
) -> JsonMember?

Removes the first member named name, if there is one. Used by the duplicate-name policy that keeps the last occurrence: dropping the earlier one is what keeps the object a map rather than a list that happens to repeat itself. The members after it are popped and pushed back rather than shifted, because shifting would move owning values around inside the vector, and a move out of a container is the one thing the ownership rules will not allow silently. Duplicates are rare enough that the cost never matters.

NameTypeDescription
nameStringViewthe member's name

Returns: the member removed, or none when this is not an object or holds no member with that name

Source

Destructor

func ~JsonValue(&var self)

Releases everything this value owns. Written out rather than left to the compiler, because a value owns its strings and its children own theirs: the recursion here is the shape of the document, and stating it makes the ownership of a nested tree something a reader can check.

Source

JsonError

pub enum JsonError: int32 {
    MalformedNumber = 2,
    WrongKind = 3
}

Why a JSON value could not be built or changed. Running out of memory is not one of the reasons: a value whose storage cannot be had terminates the program.

Source

Cases

NameValueDescription
MalformedNumber2Text that JSON's number grammar does not allow, such as 01, +1, .5, 1e or NaN.
WrongKind3An operation for one kind of value asked of another, such as pushing onto an object.

JsonKind

pub enum JsonKind: int32 {
    Null = 0,
    Boolean = 1,
    Number = 2,
    Text = 3,
    Array = 4,
    Object = 5
}

Which kind of JSON value this is.

Source

Cases

NameValueDescription
Null0The literal null.
Boolean1true or false.
Number2A number, whose precision JSON does not define.
Text3A string.
Array4An ordered sequence of values.
Object5A collection of name-value members.

JsonMember

pub struct JsonMember {
    pub name: String;
    pub value: JsonValue;
}

A name-value pair inside an object.

Source

Fields

NameTypeDescription
nameStringThe member's name.
valueJsonValueThe member's value.

Destructor

~JsonMember

func ~JsonMember(&var self)

Releases the member's name and value.

Source