JsonValue structv0.1.0
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.
| 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 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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
text | char8[..] | the number's text, borrowed for the call |
Returns: that text as a number value; MalformedNumber when the text is not a JSON number
Text
pub func Text(
allocator: Allocator,
value: char8[..]
) -> JsonValue throws 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 InvalidUtf8 when it is not text
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) -> JsonKind
Which kind this value is.
Returns: which of the six JSON kinds this is
IsNull
pub func IsNull(&self) -> bool
Whether this is null.
Returns: whether this is the null value
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
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
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
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
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.
| Name | Type | Description |
|---|---|---|
value | JsonValue | the value to act on |
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.
| Name | Type | Description |
|---|---|---|
name | String | the member's name |
value | JsonValue | the value to act on |
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.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the element at index, or none when there is none
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.
| Name | Type | Description |
|---|---|---|
name | StringView | the member's name |
Returns: the first member with that name, or none when there is none
MemberAt
pub func MemberAt(&self, index: uint) -> &JsonMember?
The member at index, borrowed, or none when there is none.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the member at index, or none when there is none
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.
| Name | Type | Description |
|---|---|---|
name | StringView | the member's name |
Returns: the member removed, or none when this is not an object or holds no member with that name
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.
Related
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.
Cases
| Name | Value | Description |
|---|---|---|
MalformedNumber | 2 | Text that JSON's number grammar does not allow, such as 01, +1, .5, 1e or NaN. |
WrongKind | 3 | An 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.
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(&var self)
Releases the member's name and value.