pg.utils.JSONConversionContext

Accessible via pg.utils.JSONConversionContext.

class JSONConversionContext[source]

Bases: pg.JSONConvertible

JSON conversion context.

JSONConversionContext is introduced to handle serialization scenarios where operations cannot be performed in a single pass. For example: Serialization and deserialization of shared objects across different locations.

# Shared object serialization/deserialization.

In PyGlove, only values referenced by pg.Ref and non-PyGlove managed objects are sharable. This ensures that multiple references to the same object are serialized only once. During deserialization, the object is created just once and shared among all references.

Classes:

ObjectEntry(value, serialized, ref_index, ...)

Methods:

from_json(json_value, **kwargs)

Deserializes a JSONConvertible value from JSON value.

get_shared(ref_index)

Gets the shared object of a ref index.

next_shared_index()

Returns the next shared index.

serialize_maybe_shared(value[, json_fn])

Track maybe shared objects and returns their JSON representation.

to_json(*, root, **kwargs)

Serializes a root node with the context to JSON value.

class ObjectEntry(value, serialized, ref_index, ref_count)[source]

Bases: object

classmethod from_json(json_value, **kwargs)[source]

Deserializes a JSONConvertible value from JSON value.

Return type:

pg.utils.JSONConversionContext

get_shared(ref_index)[source]

Gets the shared object of a ref index.

Return type:

ObjectEntry

next_shared_index()[source]

Returns the next shared index.

Return type:

int

serialize_maybe_shared(value, json_fn=None, **kwargs)[source]

Track maybe shared objects and returns their JSON representation.

Return type:

Union[int, float, bool, str, List[Any], Dict[Union[str, int], Any]]

to_json(*, root, **kwargs)[source]

Serializes a root node with the context to JSON value.

Return type:

Union[int, float, bool, str, List[Any], Dict[Union[str, int], Any]]