Json.Object

Fields

None

Methods

class

new ()

add_member (member_name, node)

dup_member (member_name)

equal (b)

foreach_member (func, *data)

get_array_member (member_name)

get_boolean_member (member_name)

get_boolean_member_with_default (member_name, default_value)

get_double_member (member_name)

get_double_member_with_default (member_name, default_value)

get_int_member (member_name)

get_int_member_with_default (member_name, default_value)

get_member (member_name)

get_members ()

get_null_member (member_name)

get_object_member (member_name)

get_size ()

get_string_member (member_name)

get_string_member_with_default (member_name, default_value)

get_values ()

has_member (member_name)

hash ()

is_immutable ()

ref ()

remove_member (member_name)

seal ()

set_array_member (member_name, value)

set_boolean_member (member_name, value)

set_double_member (member_name, value)

set_int_member (member_name, value)

set_member (member_name, node)

set_null_member (member_name)

set_object_member (member_name, value)

set_string_member (member_name, value)

unref ()

Details

class Json.Object

JsonObject is the representation of the object type inside JSON.

A JsonObject contains [struct`Json`.Node] “members”, which may contain fundamental types, arrays or other objects; each member of an object is accessed using a unique string, or “name”.

Since objects can be arbitrarily big, copying them can be expensive; for this reason they are reference counted. You can control the lifetime of a JsonObject using [method`Json`.Object.ref] and [method`Json`.Object.unref].

To add or overwrite a member with a given name, use [method`Json`.Object.set_member].

To extract a member with a given name, use [method`Json`.Object.get_member].

To retrieve the list of members, use [method`Json`.Object.get_members].

To retrieve the size of the object (that is, the number of members it has), use [method`Json`.Object.get_size].

classmethod new()
Returns:

the newly created object

Return type:

Json.Object

Creates a new object.

add_member(member_name, node)
Parameters:
  • member_name (str) – the name of the member

  • node (Json.Node) – the value of the member

Adds a new member for the given name and value into an object.

This function will return if the object already contains a member with the same name.

Deprecated since version 0.8: Use [method`Json`.Object.set_member] instead

dup_member(member_name)
Parameters:

member_name (str) – the name of the JSON object member to access

Returns:

a copy of the value for the requested object member

Return type:

Json.Node or None

Retrieves a copy of the value of the given member inside an object.

New in version 0.6.

equal(b)
Parameters:

b (Json.Object) – another JSON object

Returns:

TRUE if self and b are equal, and FALSE otherwise

Return type:

bool

Check whether self and b are equal objects, meaning they have the same set of members, and the values of corresponding members are equal.

New in version 1.2.

foreach_member(func, *data)
Parameters:

Iterates over all members of self and calls func on each one of them.

It is safe to change the value of a member of the oobject from within the iterator function, but it is not safe to add or remove members from the object.

The order in which the object members are iterated is the insertion order.

New in version 0.8.

get_array_member(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

the array inside the object’s member

Return type:

Json.Array or None

Convenience function that retrieves the array stored in member_name of self. It is an error to specify a member_name which does not exist.

If member_name contains null, then this function will return NULL.

See also: [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_boolean_member(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

the boolean value of the object’s member

Return type:

bool

Convenience function that retrieves the boolean value stored in member_name of self. It is an error to specify a member_name which does not exist.

See also: [method`Json`.Object.get_boolean_member_with_default], [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_boolean_member_with_default(member_name, default_value)
Parameters:
  • member_name (str) – the name of the self member

  • default_value (bool) – the value to return if member_name is not valid

Returns:

the boolean value of the object’s member, or the given default

Return type:

bool

Convenience function that retrieves the boolean value stored in member_name of self.

If member_name does not exist, does not contain a scalar value, or contains null, then default_value is returned instead.

New in version 1.6.

get_double_member(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

the floating point value of the object’s member

Return type:

float

Convenience function that retrieves the floating point value stored in member_name of self. It is an error to specify a member_name which does not exist.

See also: [method`Json`.Object.get_double_member_with_default], [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_double_member_with_default(member_name, default_value)
Parameters:
  • member_name (str) – the name of the self member

  • default_value (float) – the value to return if member_name is not valid

Returns:

the floating point value of the object’s member, or the given default

Return type:

float

Convenience function that retrieves the floating point value stored in member_name of self.

If member_name does not exist, does not contain a scalar value, or contains null, then default_value is returned instead.

New in version 1.6.

get_int_member(member_name)
Parameters:

member_name (str) – the name of the object member

Returns:

the integer value of the object’s member

Return type:

int

Convenience function that retrieves the integer value stored in member_name of self. It is an error to specify a member_name which does not exist.

See also: [method`Json`.Object.get_int_member_with_default], [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_int_member_with_default(member_name, default_value)
Parameters:
  • member_name (str) – the name of the object member

  • default_value (int) – the value to return if member_name is not valid

Returns:

the integer value of the object’s member, or the given default

Return type:

int

Convenience function that retrieves the integer value stored in member_name of self.

If member_name does not exist, does not contain a scalar value, or contains null, then default_value is returned instead.

New in version 1.6.

get_member(member_name)
Parameters:

member_name (str) – the name of the JSON object member to access

Returns:

the value for the requested object member

Return type:

Json.Node or None

Retrieves the value of the given member inside an object.

get_members()
Returns:

the member names of the object

Return type:

[str] or None

Retrieves all the names of the members of an object.

You can obtain the value for each member by iterating the returned list and calling [method`Json`.Object.get_member].

get_null_member(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

TRUE if the value is null

Return type:

bool

Convenience function that checks whether the value stored in member_name of self is null. It is an error to specify a member_name which does not exist.

See also: [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_object_member(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

the object inside the object’s member

Return type:

Json.Object or None

Convenience function that retrieves the object stored in member_name of self. It is an error to specify a member_name which does not exist.

If member_name contains null, then this function will return NULL.

See also: [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_size()
Returns:

the number of members

Return type:

int

Retrieves the number of members of a JSON object.

get_string_member(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

the string value of the object’s member

Return type:

str

Convenience function that retrieves the string value stored in member_name of self. It is an error to specify a member_name that does not exist.

See also: [method`Json`.Object.get_string_member_with_default], [method`Json`.Object.get_member], [method`Json`.Object.has_member]

New in version 0.8.

get_string_member_with_default(member_name, default_value)
Parameters:
  • member_name (str) – the name of the self member

  • default_value (str) – the value to return if member_name is not valid

Returns:

the string value of the object’s member, or the given default

Return type:

str

Convenience function that retrieves the string value stored in member_name of self.

If member_name does not exist, does not contain a scalar value, or contains null, then default_value is returned instead.

New in version 1.6.

get_values()
Returns:

the member values of the object

Return type:

[Json.Node] or None

Retrieves all the values of the members of an object.

has_member(member_name)
Parameters:

member_name (str) – the name of a JSON object member

Returns:

TRUE if the JSON object has the requested member

Return type:

bool

Checks whether self has a member named member_name.

hash()
Returns:

hash value for self

Return type:

int

Calculate a hash value for the given self (a JSON object).

The hash is calculated over the object and all its members, recursively. If the object is immutable, this is a fast operation; otherwise, it scales proportionally with the number of members in the object.

New in version 1.2.

is_immutable()
Returns:

TRUE if the object is immutable

Return type:

bool

Checks whether the given object has been marked as immutable by calling [method`Json`.Object.seal] on it.

New in version 1.2.

ref()
Returns:

the given object, with the reference count increased by one.

Return type:

Json.Object

Acquires a reference on the given object.

remove_member(member_name)
Parameters:

member_name (str) – the name of the member to remove

Removes member_name from self, freeing its allocated resources.

seal()

Seals the object, making it immutable to further changes.

This function will recursively seal all members of the object too.

If the object is already immutable, this is a no-op.

New in version 1.2.

set_array_member(member_name, value)
Parameters:
  • member_name (str) – the name of the member

  • value (Json.Array) – the value of the member

Convenience function for setting an object member with an array value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.take_array]

New in version 0.8.

set_boolean_member(member_name, value)
Parameters:
  • member_name (str) – the name of the member

  • value (bool) – the value of the member

Convenience function for setting an object member with a boolean value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.init_boolean]

New in version 0.8.

set_double_member(member_name, value)
Parameters:
  • member_name (str) – the name of the member

  • value (float) – the value of the member

Convenience function for setting an object member with a floating point value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.init_double]

New in version 0.8.

set_int_member(member_name, value)
Parameters:
  • member_name (str) – the name of the member

  • value (int) – the value of the member

Convenience function for setting an object member with an integer value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.init_int]

New in version 0.8.

set_member(member_name, node)
Parameters:
  • member_name (str) – the name of the member

  • node (Json.Node) – the value of the member

Sets the value of a member inside an object.

If the object does not have a member with the given name, a new member is created.

If the object already has a member with the given name, the current value is overwritten with the new.

New in version 0.8.

set_null_member(member_name)
Parameters:

member_name (str) – the name of the member

Convenience function for setting an object member with a null value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.init_null]

New in version 0.8.

set_object_member(member_name, value)
Parameters:
  • member_name (str) – the name of the member

  • value (Json.Object) – the value of the member

Convenience function for setting an object member with an object value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.take_object]

New in version 0.8.

set_string_member(member_name, value)
Parameters:
  • member_name (str) – the name of the member

  • value (str) – the value of the member

Convenience function for setting an object member with a string value.

See also: [method`Json`.Object.set_member], [method`Json`.Node.init_string]

New in version 0.8.

unref()

Releases a reference on the given object.

If the reference count reaches zero, the object is destroyed and all its resources are freed.