JsonObject

class cometa.json.json_object.JsonObject(ptr)[source]

Bases: object

Represents a parsed JSON object, array, or value.

This class serves as the primary data type for interacting with JSON data. It can represent any valid JSON value, including objects, arrays, strings, numbers, booleans, or null.

JSON objects are immutable once created in this context.

classmethod parse(json_string)[source]

Parses a JSON string into a JsonObject.

Parameters:

json_string (str) – The JSON string to parse.

Returns:

The parsed object.

Return type:

JsonObject

property type: JsonObjectType

Retrieves the type of the JSON object.

property refcount: int

Returns the number of active references to the underlying C object.

property last_error: str

Returns the last error message recorded for this object.

to_json(json_format=JsonFormat.COMPACT)[source]

Serializes the JSON object into a string.

Parameters:

json_format (JsonFormat) – The output format (COMPACT or PRETTY).

Returns:

The JSON string.

Return type:

str

__len__()[source]

Returns the number of items in the array or keys in the object. Returns 0 for other types.

Return type:

int

__getitem__(key)[source]

Retrieves a value from the JSON object or array.

Parameters:

key (int | str) – Index for arrays, key string for objects.

Returns:

The value wrapper.

Return type:

JsonObject

Raises:
  • TypeError – If the key type doesn’t match the container type.

  • IndexError – If array index is out of bounds.

  • KeyError – If object key is missing.

__contains__(key)[source]

Checks if a key exists in a JSON object.

Parameters:

key (str)

Return type:

bool

keys()[source]

Iterates over keys if this is a JSON object.

Return type:

Iterator[str]

values()[source]

Iterates over values if this is a JSON object.

Return type:

Iterator[JsonObject]

items()[source]

Iterates over (key, value) pairs if this is a JSON object.

Return type:

Iterator[Tuple[str, JsonObject]]

is_null()[source]

Checks if the JSON object represents a null value.

Return type:

bool

as_bool()[source]

Returns the boolean value if the type is BOOLEAN, else None.

Return type:

bool | None

as_str()[source]

Returns the string value if the type is STRING, else None.

Return type:

str | None

as_int()[source]

Returns the integer value if the type is NUMBER.

This method intelligently handles signed vs unsigned 64-bit integers supported by the underlying library.

Return type:

int | None

as_float()[source]

Returns the floating point value if the type is NUMBER.

Return type:

float | None

__bool__()[source]

Pythonic truthiness: - Null -> False - False -> False - 0 -> False - Empty String -> False - Empty Array/Object -> False - Everything else -> True

Return type:

bool

__init__(ptr)[source]

Internal constructor. Use factories like parse instead.

Return type:

None

__enter__()[source]

Context manager entry (no-op).

Return type:

JsonObject

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit (no-op).

Return type:

None

__repr__()[source]

Returns a detailed string representation for debugging.

Return type:

str

__str__()[source]

Returns the compact JSON string representation.

Return type:

str