openff.toolkit.utils.serialization.Serializable

class openff.toolkit.utils.serialization.Serializable[source]

Mix-in to add serialization and deserialization support via JSON, YAML, BSON, TOML, MessagePack, and XML.

For more information on these formats, see: JSON, BSON, YAML, TOML, MessagePack, and XML.

To use this mix-in, the class inheriting from this class must have implemented to_dict() and from_dict() methods that utilize dictionaries containing only serialiable Python objects.

Warning

The serialization/deserialiation schemes used here place some strict constraints on what kinds of dict objects can be serialized. No effort is made to add further protection to ensure serialization is possible. Use with caution.

Examples

Example class using Serializable mix-in:

>>> from openff.toolkit.utils.serialization import Serializable
>>> class Thing(Serializable):
...     def __init__(self, description):
...         self.description = description
...
...     def to_dict(self):
...         return { 'description' : self.description }
...
...     @classmethod
...     def from_dict(cls, d):
...         return cls(d['description'])
...
>>> # Create an example object
>>> thing = Thing('blorb')

Get JSON representation:

>>> json_thing = thing.to_json()

Reconstruct an object from its JSON representation:

>>> thing_from_json = Thing.from_json(json_thing)

Get BSON representation:

>>> bson_thing = thing.to_bson()

Reconstruct an object from its BSON representation:

>>> thing_from_bson = Thing.from_bson(bson_thing)

Get YAML representation:

>>> yaml_thing = thing.to_yaml()

Reconstruct an object from its YAML representation:

>>> thing_from_yaml = Thing.from_yaml(yaml_thing)

Get MessagePack representation:

>>> messagepack_thing = thing.to_messagepack()

Reconstruct an object from its MessagePack representation:

>>> thing_from_messagepack = Thing.from_messagepack(messagepack_thing)

Get XML representation:

>>> xml_thing = thing.to_xml()
__init__()

Methods

__init__()

from_bson(serialized)

Instantiate an object from a BSON serialized representation.

from_dict(d)

from_json(serialized)

Instantiate an object from a JSON serialized representation.

from_messagepack(serialized)

Instantiate an object from a MessagePack serialized representation.

from_pickle(serialized)

Instantiate an object from a pickle serialized representation.

from_toml(serialized)

Instantiate an object from a TOML serialized representation.

from_xml(serialized)

Instantiate an object from an XML serialized representation.

from_yaml(serialized)

Instantiate from a YAML serialized representation.

to_bson()

Return a BSON serialized representation.

to_dict()

to_json([indent])

Return a JSON serialized representation.

to_messagepack()

Return a MessagePack representation.

to_pickle()

Return a pickle serialized representation.

to_toml()

Return a TOML serialized representation.

to_xml([indent])

Return an XML representation.

to_yaml()

Return a YAML serialized representation.

to_json(indent=None)[source]

Return a JSON serialized representation.

Specification: https://www.json.org/

Parameters

indent (int, optional, default=None) – If not None, will pretty-print with specified number of spaces for indentation

Returns

serialized (str) – A JSON serialized representation of the object

classmethod from_json(serialized)[source]

Instantiate an object from a JSON serialized representation.

Specification: https://www.json.org/

Parameters

serialized (str) – A JSON serialized representation of the object

Returns

instance (cls) – An instantiated object

to_bson()[source]

Return a BSON serialized representation.

Specification: http://bsonspec.org/

Returns

serialized (bytes) – A BSON serialized representation of the objecft

classmethod from_bson(serialized)[source]

Instantiate an object from a BSON serialized representation.

Specification: http://bsonspec.org/

Parameters

serialized (bytes) – A BSON serialized representation of the object

Returns

instance (cls) – An instantiated object

to_toml()[source]

Return a TOML serialized representation.

Specification: https://github.com/toml-lang/toml

Returns

serialized (str) – A TOML serialized representation of the object

classmethod from_toml(serialized)[source]

Instantiate an object from a TOML serialized representation.

Specification: https://github.com/toml-lang/toml

Parameters

serlialized (str) – A TOML serialized representation of the object

Returns

instance (cls) – An instantiated object

to_yaml()[source]

Return a YAML serialized representation.

Specification: http://yaml.org/

Returns

serialized (str) – A YAML serialized representation of the object

classmethod from_yaml(serialized)[source]

Instantiate from a YAML serialized representation.

Specification: http://yaml.org/

Parameters

serialized (str) – A YAML serialized representation of the object

Returns

instance (cls) – Instantiated object

to_messagepack()[source]

Return a MessagePack representation.

Specification: https://msgpack.org/index.html

Returns

serialized (bytes) – A MessagePack-encoded bytes serialized representation of the object

classmethod from_messagepack(serialized)[source]

Instantiate an object from a MessagePack serialized representation.

Specification: https://msgpack.org/index.html

Parameters

serialized (bytes) – A MessagePack-encoded bytes serialized representation

Returns

instance (cls) – Instantiated object.

to_xml(indent=2)[source]

Return an XML representation.

Specification: https://www.w3.org/XML/

Parameters

indent (int, optional, default=2) – If not None, will pretty-print with specified number of spaces for indentation

Returns

serialized (bytes) – A MessagePack-encoded bytes serialized representation.

classmethod from_xml(serialized)[source]

Instantiate an object from an XML serialized representation.

Specification: https://www.w3.org/XML/

Parameters

serialized (bytes) – An XML serialized representation

Returns

instance (cls) – Instantiated object.

to_pickle()[source]

Return a pickle serialized representation.

Warning

This is not recommended for safe, stable storage since the pickle specification may change between Python versions.

Returns

serialized (str) – A pickled representation of the object

classmethod from_pickle(serialized)[source]

Instantiate an object from a pickle serialized representation.

Warning

This is not recommended for safe, stable storage since the pickle specification may change between Python versions.

Parameters

serialized (str) – A pickled representation of the object

Returns

instance (cls) – An instantiated object