Skip to main content

Node

Struct Node 

Source
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

Source

pub fn as_ptr(&self) -> *mut JsonNode

Return the inner pointer to the underlying C value.

Source

pub unsafe fn from_glib_ptr_borrow(ptr: &*mut JsonNode) -> &Self

Borrows the underlying C value.

Source§

impl Node

Source

pub fn alloc() -> Node

Allocates a new, uninitialized node.

Use init() and its variants to initialize the returned value.

§Returns

the newly allocated node

Source

pub fn new(type_: NodeType) -> Node

Creates a new node holding the given @type_.

This is a convenience function for Json::Node::alloc() and init(), and it’s the equivalent of:

⚠️ The following code is in c ⚠️

   json_node_init (json_node_alloc (), type);
§type_

the type of the node to create

§Returns

the newly created node

Source

pub fn copy(&self) -> Node

Source

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.

Source

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

Source

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

Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn node_type(&self) -> NodeType

Retrieves the type of a @self.

§Returns

the type of the node

Source

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

Source

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

Source

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.

Source

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

Source

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

Source

pub fn init(&self, type_: NodeType) -> Node

Initializes a @self to a specific @type_.

If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.

§type_

the type of JSON node to initialize @self to

§Returns

the initialized node

Source

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

Source

pub fn init_boolean(&self, value: bool) -> Node

Initializes @self to JSON_NODE_VALUE and sets @value into it.

If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.

§value

a boolean value

§Returns

the initialized node

Source

pub fn init_double(&self, value: f64) -> Node

Initializes @self to JSON_NODE_VALUE and sets @value into it.

If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.

§value

a floating point value

§Returns

the initialized node

Source

pub fn init_int(&self, value: i64) -> Node

Initializes @self to JSON_NODE_VALUE and sets @value into it.

If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.

§value

an integer

§Returns

the initialized node

Source

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

Source

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

Source

pub fn init_string(&self, value: Option<&str>) -> Node

Initializes @self to JSON_NODE_VALUE and sets @value into it.

If the node has already been initialized once, it will be reset to the given type, and any data contained will be cleared.

§value

a string value

§Returns

the initialized node

Source

pub fn is_immutable(&self) -> bool

Available on crate feature v1_2 only.

Check whether the given @self has been marked as immutable by calling seal() on it.

§Returns

TRUE if the @self is immutable

Source

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

Source

pub fn seal(&self)

Available on crate feature 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.

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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_INT64
  • G_TYPE_DOUBLE
  • G_TYPE_BOOLEAN
  • G_TYPE_STRING

JSON-GLib will also automatically promote the following GValue types:

  • G_TYPE_INT to G_TYPE_INT64
  • G_TYPE_FLOAT to G_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

Source

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

Source

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

Source

pub fn type_name(&self) -> GString

Retrieves the user readable name of the data type contained by @self.

Note: The name is only meant for debugging purposes, and there is no guarantee the name will stay the same across different versions.

§Returns

a string containing the name of the type

Trait Implementations§

Source§

impl Clone for Node

Source§

fn clone(&self) -> Self

Makes a clone of this shared reference.

This increments the strong reference count of the reference. Dropping the reference will decrement it again.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Node

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Node

Available on crate feature v1_2 only.
Source§

impl From<Node> for Value

Source§

fn from(s: Node) -> Self

Converts to this type from the input type.
Source§

impl HasParamSpec for Node

Source§

type ParamSpec = ParamSpecBoxed

Source§

type SetValue = Node

Preferred value to be used as setter for the associated ParamSpec.
Source§

type BuilderFn = fn(&str) -> ParamSpecBoxedBuilder<'_, Node>

Source§

fn param_spec_builder() -> Self::BuilderFn

Source§

impl Hash for Node

Available on crate feature v1_2 only.
Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Node

Source§

fn cmp(&self, other: &Node) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Node

Available on crate feature v1_2 only.
Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Node

Source§

fn partial_cmp(&self, other: &Node) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StaticType for Node

Source§

fn static_type() -> Type

Returns the type identifier of Self.

Auto Trait Implementations§

§

impl !Send for Node

§

impl !Sync for Node

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Unpin for Node

§

impl UnsafeUnpin for Node

§

impl UnwindSafe for Node

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for T

Source§

impl<'a, T, C, E> FromValueOptional<'a> for T
where T: FromValue<'a, Checker = C>, C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError<E>>, E: Error + Send + 'static,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoClosureReturnValue for T
where T: Into<Value>,

Source§

impl<T> Property for T
where T: HasParamSpec,

Source§

type Value = T

Source§

impl<T> PropertyGet for T
where T: HasParamSpec,

Source§

type Value = T

Source§

fn get<R, F>(&self, f: F) -> R
where F: Fn(&<T as PropertyGet>::Value) -> R,

Source§

impl<T> StaticTypeExt for T
where T: StaticType,

Source§

fn ensure_type()

Ensures that the type has been registered with the type system.
Source§

impl<'a, T> ToGlibContainerFromSlice<'a, *const GList> for T

Source§

impl<'a, T> ToGlibContainerFromSlice<'a, *const GPtrArray> for T

Source§

impl<'a, T> ToGlibContainerFromSlice<'a, *mut GList> for T

Source§

impl<'a, T> ToGlibContainerFromSlice<'a, *mut GPtrArray> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TransparentType for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T> TryFromClosureReturnValue for T
where T: for<'a> FromValue<'a> + StaticType + 'static,

Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.