pub struct Node { /* private fields */ }Expand description
A generic container of JSON data types.
JsonNode can contain fundamental types (integers, booleans, floating point
numbers, strings) and complex types (arrays and objects).
When parsing a JSON data stream you extract the root node and walk
the node tree by retrieving the type of data contained inside the
node with the JSON_NODE_TYPE macro. If the node contains a fundamental
type you can retrieve a copy of the GValue holding it with the
value() function, and then use the GValue API to extract
the data; if the node contains a complex type you can retrieve the
Object or the Array using object()
or array() respectively, and then retrieve the nodes
they contain.
A JsonNode may be marked as immutable using seal(). This
marks the node and all its descendents as read-only, and means that
subsequent calls to setter functions (such as set_array())
on them will abort as a programmer error. By marking a node tree as
immutable, it may be referenced in multiple places and its hash value cached
for fast lookups, without the possibility of a value deep within the tree
changing and affecting hash values. Immutable nodes may be passed to
functions which retain a reference to them without needing to take a copy.
A JsonNode supports two types of memory management: malloc/free
semantics, and reference counting semantics. The two may be mixed to a
limited extent: nodes may be allocated (which gives them a reference count
of 1), referenced one or more times, unreferenced exactly that number of
times (using Json::Node::unref()), then either unreferenced exactly
once more or freed (using Json::Node::free()) to destroy them.
The Json::Node::free() function must not be used when a node might
have a reference count not equal to 1. To this end, JSON-GLib uses
Json::Node::copy() and Json::Node::unref() internally.
GLib type: Shared boxed type with reference counted clone semantics.
Implementations§
Source§impl Node
impl Node
pub fn copy(&self) -> Node
Sourcepub fn dup_array(&self) -> Option<Array>
pub fn dup_array(&self) -> Option<Array>
Retrieves the JSON array inside @self.
The reference count of the returned array is increased.
It is a programmer error to call this on a node which doesn’t hold an
array value. Use JSON_NODE_HOLDS_ARRAY first.
§Returns
the JSON array with its reference count increased.
Sourcepub fn dup_object(&self) -> Option<Object>
pub fn dup_object(&self) -> Option<Object>
Retrieves the object inside @self.
The reference count of the returned object is increased.
It is a programmer error to call this on a node which doesn’t hold an
object value. Use JSON_NODE_HOLDS_OBJECT first.
§Returns
the JSON object
Sourcepub fn dup_string(&self) -> Option<GString>
pub fn dup_string(&self) -> Option<GString>
Gets a copy of the string value stored inside a node.
If the node does not hold a string value, NULL is returned.
§Returns
a copy of the string inside the node
Sourcepub fn array(&self) -> Option<Array>
pub fn array(&self) -> Option<Array>
Retrieves the JSON array stored inside a node.
It is a programmer error to call this on a node which doesn’t hold an
array value. Use JSON_NODE_HOLDS_ARRAY first.
§Returns
the JSON array
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Gets the boolean value stored inside a node.
If the node holds an integer or double value which is zero, FALSE is
returned; otherwise TRUE is returned.
If the node holds a JSON_NODE_NULL value or a value of another
non-boolean type, FALSE is returned.
§Returns
a boolean value.
Sourcepub fn double(&self) -> f64
pub fn double(&self) -> f64
Gets the double value stored inside a node.
If the node holds an integer value, it is returned as a double.
If the node holds a FALSE boolean value, 0.0 is returned; otherwise
a non-zero double is returned.
If the node holds a JSON_NODE_NULL value or a value of another
non-double type, 0.0 is returned.
§Returns
a double value.
Sourcepub fn int(&self) -> i64
pub fn int(&self) -> i64
Gets the integer value stored inside a node.
If the node holds a double value, its integer component is returned.
If the node holds a FALSE boolean value, 0 is returned; otherwise,
a non-zero integer is returned.
If the node holds a JSON_NODE_NULL value or a value of another
non-integer type, 0 is returned.
§Returns
an integer value.
Sourcepub fn object(&self) -> Option<Object>
pub fn object(&self) -> Option<Object>
Retrieves the object stored inside a node.
It is a programmer error to call this on a node which doesn’t hold an
object value. Use JSON_NODE_HOLDS_OBJECT first.
§Returns
the JSON object
Sourcepub fn parent(&self) -> Option<Node>
pub fn parent(&self) -> Option<Node>
Retrieves the parent node of the given @self.
§Returns
the parent node, or NULL if @self
is the root node
Sourcepub fn string(&self) -> Option<GString>
pub fn string(&self) -> Option<GString>
Gets the string value stored inside a node.
If the node does not hold a string value, NULL is returned.
§Returns
a string value.
Sourcepub fn value(&self) -> Value
pub fn value(&self) -> Value
Retrieves a value from a node and copies into @value.
When done using it, call g_value_unset() on the GValue to free the
associated resources.
It is a programmer error to call this on a node which doesn’t hold a scalar
value. Use JSON_NODE_HOLDS_VALUE first.
§Returns
§value
return location for an uninitialized value
Sourcepub fn value_type(&self) -> Type
pub fn value_type(&self) -> Type
Returns the GType of the payload of the node.
For JSON_NODE_NULL nodes, the returned type is G_TYPE_INVALID.
§Returns
the type for the payload
Sourcepub fn init_array(&self, array: Option<&Array>) -> Node
pub fn init_array(&self, array: Option<&Array>) -> Node
Initializes @self to JSON_NODE_ARRAY and sets @array into it.
This function will take a reference on @array.
If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.
§array
the JSON array to initialize @self with, or NULL
§Returns
the initialized node
Sourcepub fn init_boolean(&self, value: bool) -> Node
pub fn init_boolean(&self, value: bool) -> Node
Sourcepub fn init_double(&self, value: f64) -> Node
pub fn init_double(&self, value: f64) -> Node
Sourcepub fn init_null(&self) -> Node
pub fn init_null(&self) -> Node
Initializes @self to JSON_NODE_NULL.
If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.
§Returns
the initialized node
Sourcepub fn init_object(&self, object: Option<&Object>) -> Node
pub fn init_object(&self, object: Option<&Object>) -> Node
Initializes @self to JSON_NODE_OBJECT and sets @object into it.
This function will take a reference on @object.
If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.
§object
the JSON object to initialize @self with, or NULL
§Returns
the initialized node
Sourcepub fn init_string(&self, value: Option<&str>) -> Node
pub fn init_string(&self, value: Option<&str>) -> Node
Sourcepub fn is_immutable(&self) -> bool
Available on crate feature v1_2 only.
pub fn is_immutable(&self) -> bool
v1_2 only.Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Checks whether @self is a JSON_NODE_NULL.
A JSON_NODE_NULL node is not the same as a NULL node; a JSON_NODE_NULL
represents a literal null value in the JSON tree.
§Returns
TRUE if the node is null
Sourcepub fn seal(&self)
Available on crate feature v1_2 only.
pub fn seal(&self)
v1_2 only.Seals the given node, making it immutable to further changes.
In order to be sealed, the @self must have a type and value set. The value will be recursively sealed — if the node holds an object, that JSON object will be sealed, etc.
If the node is already immutable, this is a no-op.
Sourcepub fn set_array(&self, array: &Array)
pub fn set_array(&self, array: &Array)
Sets @array inside @self.
The reference count of @array is increased.
It is a programmer error to call this on a node which doesn’t hold an
array value. Use JSON_NODE_HOLDS_ARRAY first.
§array
a JSON array
Sourcepub fn set_boolean(&self, value: bool)
pub fn set_boolean(&self, value: bool)
Sets @value as the boolean content of the @self, replacing any existing content.
It is an error to call this on an immutable node, or on a node which is not a value node.
§value
a boolean value
Sourcepub fn set_double(&self, value: f64)
pub fn set_double(&self, value: f64)
Sets @value as the double content of the @self, replacing any existing content.
It is an error to call this on an immutable node, or on a node which is not a value node.
§value
a double value
Sourcepub fn set_int(&self, value: i64)
pub fn set_int(&self, value: i64)
Sets @value as the integer content of the @self, replacing any existing content.
It is an error to call this on an immutable node, or on a node which is not a value node.
§value
an integer value
Sourcepub fn set_object(&self, object: Option<&Object>)
pub fn set_object(&self, object: Option<&Object>)
Sets @objects inside @self.
The reference count of @object is increased.
If @object is NULL, the node’s existing object is cleared.
It is an error to call this on an immutable node, or on a node which is not an object node.
§object
a JSON object
Sourcepub fn set_parent(&self, parent: Option<&Node>)
pub fn set_parent(&self, parent: Option<&Node>)
Sets the parent node for the given node.
It is an error to call this with an immutable @parent.
The @self may be immutable.
§parent
the parent node
Sourcepub fn set_string(&self, value: &str)
pub fn set_string(&self, value: &str)
Sets @value as the string content of the @self, replacing any existing content.
It is an error to call this on an immutable node, or on a node which is not a value node.
§value
a string value
Sourcepub fn set_value(&self, value: &Value)
pub fn set_value(&self, value: &Value)
Sets a scalar value inside the given node.
The contents of the given GValue are copied into the JsonNode.
The following GValue types have a direct mapping to JSON types:
G_TYPE_INT64G_TYPE_DOUBLEG_TYPE_BOOLEANG_TYPE_STRING
JSON-GLib will also automatically promote the following GValue types:
G_TYPE_INTtoG_TYPE_INT64G_TYPE_FLOATtoG_TYPE_DOUBLE
It is an error to call this on an immutable node, or on a node which is not a value node.
§value
the value to set
Sourcepub fn take_array(&self, array: Array)
pub fn take_array(&self, array: Array)
Sets @array inside @self.
The reference count of @array is not increased.
It is a programmer error to call this on a node which doesn’t hold an
array value. Use JSON_NODE_HOLDS_ARRAY first.
§array
a JSON array
Sourcepub fn take_object(&self, object: Object)
pub fn take_object(&self, object: Object)
Sets @object inside @self.
The reference count of @object is not increased.
It is an error to call this on an immutable node, or on a node which is not an object node.
§object
a JSON object
Trait Implementations§
impl Eq for Node
v1_2 only.Source§impl HasParamSpec for Node
impl HasParamSpec for Node
Source§impl Ord for Node
impl Ord for Node
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Node
impl PartialOrd for Node
Source§impl StaticType for Node
impl StaticType for Node
Source§fn static_type() -> Type
fn static_type() -> Type
Self.