Class (GI Struct)

Json-1.0JsonNode

A generic container of JSON data types.

Json.Node 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 GObject.Value holding it with the Json.Node.get_value function, and then use the GObject.Value API to extract the data; if the node contains a complex type you can retrieve the Json.Object or the Json.Array using Json.Node.get_object or Json.Node.get_array respectively, and then retrieve the nodes they contain.

A Json.Node may be marked as immutable using Json.Node.seal. This marks the node and all its descendents as read-only, and means that subsequent calls to setter functions (such as Json.Node.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 Json.Node 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.

Index

Constructors

Properties

$gtype: GType<Json.Node>

Methods

  • Copies node.

    If the node contains complex data types, their reference counts are increased, regardless of whether the node is mutable or immutable.

    The copy will be immutable if, and only if, node is immutable. However, there should be no need to copy an immutable node.

    Returns Json.Node

    the copied of the given node

  • Retrieves the JSON array inside node.

    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 Json.Array

    the JSON array with its reference count increased.

  • Retrieves the object inside node.

    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 Json.Object

    the JSON object

  • Gets a copy of the string value stored inside a node.

    If the node does not hold a string value, NULL is returned.

    Returns string

    a copy of the string inside the node

  • Check whether a and b are equal node, meaning they have the same type and same values (checked recursively).

    Note that integer values are compared numerically, ignoring type, so a double value 4.0 is equal to the integer value 4.

    Parameters

    Returns boolean

    TRUE if a and b are equal; FALSE otherwise

  • 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 Json.Array

    the JSON array

  • 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 boolean

    a boolean value.

  • 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 number

    a double value.

  • 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 number

    an integer value.

  • 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 Json.Object

    the JSON object

  • Gets the string value stored inside a node.

    If the node does not hold a string value, NULL is returned.

    Returns string

    a string value.

  • Retrieves a value from a node and copies into value.

    When done using it, call g_value_unset() on the GObject.Value 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 unknown

  • Calculate a hash value for the given key.

    The hash is calculated over the node and its value, recursively. If the node is immutable, this is a fast operation; otherwise, it scales proportionally with the size of the node’s value (for example, with the number of members in the JSON object if this node contains an object).

    Returns number

    hash value for key

  • Initializes a node 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.

    Parameters

    • type: Json.NodeType

      the type of JSON node to initialize node to

    Returns Json.Node

    the initialized node

  • Initializes node 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.

    Parameters

    • Optionalarray: Json.Array

      the JSON array to initialize node with, or NULL

    Returns Json.Node

    the initialized node

  • Initializes node 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.

    Parameters

    • value: boolean

      a boolean value

    Returns Json.Node

    the initialized node

  • Initializes node 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.

    Parameters

    • value: number

      a floating point value

    Returns Json.Node

    the initialized node

  • Initializes node 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.

    Parameters

    • value: number

      an integer

    Returns Json.Node

    the initialized node

  • Initializes node 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 Json.Node

    the initialized node

  • Initializes node 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.

    Parameters

    • Optionalobject: Json.Object

      the JSON object to initialize node with, or NULL

    Returns Json.Node

    the initialized node

  • Initializes node 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.

    Parameters

    • Optionalvalue: string

      a string value

    Returns Json.Node

    the initialized node

  • Checks whether node 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 boolean

    TRUE if the node is null

  • Seals the given node, making it immutable to further changes.

    In order to be sealed, the node 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.

    Returns void

  • Sets array inside node.

    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.

    Parameters

    Returns void

  • Sets value as the boolean content of the node, 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.

    Parameters

    • value: boolean

      a boolean value

    Returns void

  • Sets value as the double content of the node, 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.

    Parameters

    • value: number

      a double value

    Returns void

  • Sets value as the integer content of the node, 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.

    Parameters

    • value: number

      an integer value

    Returns void

  • Sets objects inside node.

    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.

    Parameters

    Returns void

  • Sets the parent node for the given node.

    It is an error to call this with an immutable parent.

    The node may be immutable.

    Parameters

    • Optionalparent: Json.Node

      the parent node

    Returns void

  • Sets value as the string content of the node, 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.

    Parameters

    • value: string

      a string value

    Returns void

  • Sets a scalar value inside the given node.

    The contents of the given GObject.Value are copied into the Json.Node.

    The following GObject.Value 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 GObject.Value 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.

    Parameters

    • value: any

      the value to set

    Returns void

  • Sets array inside node.

    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.

    Parameters

    Returns void

  • Sets object inside node.

    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.

    Parameters

    Returns void

  • Retrieves the user readable name of the data type contained by node.

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

    Returns string

    a string containing the name of the type

  • Decrements the reference count of node.

    If the reference count reaches zero, the node is freed.

    Returns void