JsonValue structv0.1.0
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
| Name | Type | Description |
|---|---|---|
kind | JsonKind | Which of the six kinds this is. |
Associated functions
Null
pub func Null(allocator: Allocator) -> JsonValue
The literal null.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: the null value
Boolean
pub func Boolean(
allocator: Allocator,
value: bool
) -> JsonValue
A boolean value.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | bool | the value to act on |
Returns: that boolean as a value
Number
pub func Number(
allocator: Allocator,
text: char8[..]
) -> JsonValue ! TextError
A number, from the text it was written with.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
text | char8[..] | the text to act on |
Returns: that text as a number value, or the null value when the allocator refused
Text
pub func Text(
allocator: Allocator,
value: char8[..]
) -> JsonValue ! TextError
A string value.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | char8[..] | the value to act on |
Returns: that text as a string value, or the null value when the allocator refused
Array
pub func Array(allocator: Allocator) -> JsonValue
An empty array.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: an empty array
Object
pub func Object(allocator: Allocator) -> JsonValue
An empty object.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: an empty object
Methods
Kind
pub func Kind(self: &JsonValue) -> JsonKind
Which kind this value is.
Returns: which of the six JSON kinds this is
IsNull
pub func IsNull(self: &JsonValue) -> bool
Whether this is null.
Returns: whether this is the null value
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.
| Name | Type | Description |
|---|---|---|
result | *var bool | receives the value when this reports true |
Returns: whether this is a boolean
AsNumber
pub func AsNumber(self: &JsonValue) -> *JsonNumber
The number payload, borrowed.
Returns: the number, or null when this is not one
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
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
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.
| Name | Type | Description |
|---|---|---|
value | JsonValue | the value to act on |
Returns: whether the element was appended
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.
| Name | Type | Description |
|---|---|---|
name | String | the member's name |
value | JsonValue | the value to act on |
Returns: whether the member was added
At
pub func At(self: &JsonValue, index: uint) -> *JsonValue
The element at index, borrowed, or null when there is none.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the element at index, or null when there is none
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.
| Name | Type | Description |
|---|---|---|
name | StringView | the member's name |
Returns: the first member with that name, or null when there is none
MemberAt
pub func MemberAt(
self: &JsonValue,
index: uint
) -> *JsonMember
The member at index, borrowed, or null when there is none.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the member at index, or null when there is none
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.
| Name | Type | Description |
|---|---|---|
name | StringView | the member's name |
Returns: whether a member with that name was removed
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.
Related
JsonKind
pub enum JsonKind: int32 {
Null = 0,
Boolean = 1,
Number = 2,
Text = 3,
Array = 4,
Object = 5
}
Which kind of JSON value this is.
Cases
| Name | Value | Description |
|---|---|---|
Null | 0 | The literal null. |
Boolean | 1 | true or false. |
Number | 2 | A number, whose precision JSON does not define. |
Text | 3 | A string. |
Array | 4 | An ordered sequence of values. |
Object | 5 | A collection of name-value members. |
JsonMember
pub struct JsonMember {
pub name: String;
pub value: JsonValue;
}
A name-value pair inside an object.
Fields
| Name | Type | Description |
|---|---|---|
name | String | The member's name. |
value | JsonValue | The member's value. |
Destructor
~JsonMember
func ~JsonMember(self: &var JsonMember)
Releases the member's name and value.