pg.JSONConvertible¶
Accessible via pg.JSONConvertible
, pg.object_utils.JSONConvertible
.
- class JSONConvertible[source]¶
Bases:
object
Interface for classes whose instances are convertible from/to JSON.
A JSON convertible object is an object that can be converted into plain Python objects, hence can be serialized into or deserialized from JSON.
Subclasses of
JSONConvertible
should implement:to_json
: A method that returns a plain Python dict with a _type property whose value should identify the class.from_json
: A class method that takes a plain Python dict and returns an instance of the class.
Example:
class MyObject(pg.JSONConvertible): def __init__(self, x: int): self.x = x def to_json(self, **kwargs): return { '_type': 'MyObject', 'x': self.x } @classmethod def from_json(cls, json_value, **kwargs): return cls(json_value['x'])
All symbolic types (see
pg.Symbolic
) are JSON convertible.Methods:
Get converter from source type to a JSON simple type.
add_module_alias
(source_name, target_name)Adds a module alias so previous serialized objects could be loaded.
class_from_typename
(type_name)Gets the class for a registered type name.
from_json
(json_value, **kwargs)Creates an instance of this class from a plain Python value.
is_registered
(type_name)Returns True if a type name is registered.
register
(type_name, subclass[, ...])Registers a class with a type name.
Returns an iterator of registered (serialization key, class) tuples.
to_json
(**kwargs)Returns a plain Python value as a representation for this object.
to_json_dict
(fields, *[, exclude_default, ...])Helper method to create JSON dict from class and field.
- classmethod add_module_alias(source_name, target_name)[source]¶
Adds a module alias so previous serialized objects could be loaded.
- Return type:
- classmethod class_from_typename(type_name)[source]¶
Gets the class for a registered type name.
- Return type:
- Parameters:
type_name – A string as the global unique type identifier for requested class.
- Returns:
A type object if registered, otherwise None.
- classmethod from_json(json_value, **kwargs)[source]¶
Creates an instance of this class from a plain Python value.
NOTE(daiyip):
pg.Symbolic
overridesfrom_json
class method.- Return type:
- Parameters:
json_value – JSON value type.
**kwargs – Keyword arguments as flags to control object creation.
- Returns:
An instance of cls.
- classmethod is_registered(type_name)[source]¶
Returns True if a type name is registered. Otherwise False.
- Return type:
- classmethod register(type_name, subclass, override_existing=False)[source]¶
Registers a class with a type name.
The type name will be used as the key for class lookup during deserialization. A class can be registered with multiple type names, but a type name should be uesd only for one class.
- Return type:
- Parameters:
type_name – A global unique string identifier for subclass.
subclass – A subclass of JSONConvertible.
override_existing – If True, override the class if the type name is already present in the registry. Otherwise an error will be raised.
- classmethod registered_types()[source]¶
Returns an iterator of registered (serialization key, class) tuples.
- Return type:
- abstract to_json(**kwargs)[source]¶
Returns a plain Python value as a representation for this object.
A plain Python value are basic python types that can be serialized into JSON, e.g:
bool
,int
,float
,str
,dict
(with string keys),list
,tuple
where the container types should have plain Python values as their values.