JsonValue structv0.1.0

Source
A JSON value: one of the six kinds, owning whatever it holds.
pub struct JsonValue {
    pub kind: JsonKind;
    // 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 inspected with Kind and the payload read with the matching accessor; asking for the wrong one reports failure rather than reinterpreting the bytes.

Fields

NameTypeDescription
kindJsonKindWhich of the six kinds this is.

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 ! TextError

A number, from the text it was written with.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
textchar8[..]the text to act on

Returns: that text as a number value, or the null value when the allocator refused

Source

Text

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

A string value.

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

Returns: that text as a string value, or the null value when the allocator refused

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: &JsonValue) -> JsonKind

Which kind this value is.

Returns: which of the six JSON kinds this is

Source

IsNull

pub func IsNull(self: &JsonValue) -> bool

Whether this is null.

Returns: whether this is the null value

Source

AsBoolean

pub func AsBoolean(
    self: &JsonValue,
    result: *var bool
) -> bool

The boolean payload, or false when this is not a boolean — which Kind distinguishes from a genuine false.

NameTypeDescription
result*var boolreceives the value when this reports true

Returns: whether this is a boolean

Source

AsNumber

pub func AsNumber(self: &JsonValue) -> *JsonNumber

The number payload, borrowed.

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

Source

AsText

pub func AsText(self: &JsonValue) -> StringView

The string payload, or an empty view when this is not a string.

Returns: the string, or an empty view when this is not one

Source

Length

pub func Length(self: &JsonValue) -> uint

How many elements an array holds, or members an object holds; zero for anything else.

Returns: how many elements or members this holds; zero for anything else

Source

Push

pub func Push(
    self: &var JsonValue,
    value: JsonValue
) -> bool

Appends value to an array, taking ownership of it. Reports false when this is not an array or the allocator refused; value is consumed either way, so a caller does not have to decide who owns it on failure.

NameTypeDescription
valueJsonValuethe value to act on

Returns: whether the element was appended

Source

Insert

pub func Insert(
    self: &var JsonValue,
    name: String,
    value: JsonValue
) -> bool

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.

NameTypeDescription
nameStringthe member's name
valueJsonValuethe value to act on

Returns: whether the member was added

Source

At

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

The element at index, borrowed, or null when there is none.

NameTypeDescription
indexuintwhich one, counting from zero

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

Source

Find

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

The value of the member named name, borrowed, or null 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 null when there is none

Source

MemberAt

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

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

NameTypeDescription
indexuintwhich one, counting from zero

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

Source

RemoveFirst

pub func RemoveFirst(
    self: &var JsonValue,
    name: StringView
) -> bool

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: whether a member with that name was removed

Source

Destructor

func ~JsonValue(self: &var JsonValue)

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

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(self: &var JsonMember)

Releases the member's name and value.

Source