schrodinger.models.jsonable module¶
A module for defining jsonable versions of classes (typically classes defined in third-party modules).
You can also find the registry of classes that are supported by the load(s) and
dump(s) functions in schrodinger.model.json
. Any object that is an instance
of one of the registered classes will be automatically jsonable using dump
and dumps
. To deserialize, you must specify the registered class to
load
or loads
. Example:
from schrodinger.models import json
my_set = set(range(1,2,3))
my_set_jsonstr = json.dumps(my_set)
new_set = json.loads(my_set_jsonstr, DataClass=set)
assert new_set == my_set
assert isinstance(new_set, set)
- Currently registered DataClasses:
structure.Structure
set
tuple
rdkit.Chem.rdchem.Mol
bytes
- class schrodinger.models.jsonable.JsonableSet¶
Bases:
schrodinger.models.json.JsonableClassMixin
,set
- ENCODING_KEY = '_python_set_'¶
- toJsonImplementation()¶
Abstract method that must be defined by all derived classes. Converts an instance of the derived class into a jsonifiable object.
- Returns
A dict made up of JSON native datatypes or Jsonable objects. See the link below for a table of such types. https://docs.python.org/2/library/json.html#encoders-and-decoders
- classmethod fromJsonImplementation(json_list)¶
Abstract method that must be defined by all derived classes. Takes in a dictionary and constructs an instance of the derived class.
- Parameters
json_dict (dict) – A dictionary loaded from a JSON string or file.
- Returns
An instance of the derived class.
- Return type
cls
- copy()¶
Return a shallow copy of a set.
- class schrodinger.models.jsonable.JsonableStructure(handle, *, take_ownership=True)¶
Bases:
schrodinger.models.json.JsonableClassMixin
,schrodinger.structure._structure.Structure
- toJsonImplementation()¶
Abstract method that must be defined by all derived classes. Converts an instance of the derived class into a jsonifiable object.
- Returns
A dict made up of JSON native datatypes or Jsonable objects. See the link below for a table of such types. https://docs.python.org/2/library/json.html#encoders-and-decoders
- classmethod fromJsonImplementation(json_str)¶
Abstract method that must be defined by all derived classes. Takes in a dictionary and constructs an instance of the derived class.
- Parameters
json_dict (dict) – A dictionary loaded from a JSON string or file.
- Returns
An instance of the derived class.
- Return type
cls
- class schrodinger.models.jsonable.JsonableTuple(iterable=(), /)¶
Bases:
schrodinger.models.json.JsonableClassMixin
,tuple
- ENCODING_KEY = '_python_tuple_'¶
- toJsonImplementation()¶
Abstract method that must be defined by all derived classes. Converts an instance of the derived class into a jsonifiable object.
- Returns
A dict made up of JSON native datatypes or Jsonable objects. See the link below for a table of such types. https://docs.python.org/2/library/json.html#encoders-and-decoders
- classmethod fromJsonImplementation(json_list)¶
Abstract method that must be defined by all derived classes. Takes in a dictionary and constructs an instance of the derived class.
- Parameters
json_dict (dict) – A dictionary loaded from a JSON string or file.
- Returns
An instance of the derived class.
- Return type
cls
- class schrodinger.models.jsonable.JsonableBytes¶
Bases:
schrodinger.models.json.JsonableClassMixin
,bytes
- toJsonImplementation() str ¶
Abstract method that must be defined by all derived classes. Converts an instance of the derived class into a jsonifiable object.
- Returns
A dict made up of JSON native datatypes or Jsonable objects. See the link below for a table of such types. https://docs.python.org/2/library/json.html#encoders-and-decoders
- classmethod fromJsonImplementation(b64encoded_bytes: str)¶
Abstract method that must be defined by all derived classes. Takes in a dictionary and constructs an instance of the derived class.
- Parameters
json_dict (dict) – A dictionary loaded from a JSON string or file.
- Returns
An instance of the derived class.
- Return type
cls
- class schrodinger.models.jsonable.JsonableNamedTuple¶
Bases:
schrodinger.models.json.JsonableClassMixin
A jsonabled NamedTuple that behaves like a normal named tuple but is jsonable if its fields are jsonable. Example:
class Coordinate(JsonableNamedTuple): x: float y: float description: str coord = Coordinate(x=1, y=2, description="molecule coord") assert coord == (1, 2, "molecule coord") serialized_coord = json.dumps(c) deserialized_coord = json.loads(serialized_coord, DataClass=Coordinate) assert deserialized_coord == (1, 2, "molecule coord")
- WARNING:: Instances of subclasses of this class will not evaluate as
instances of
JsonableNamedTuple
. This replicates the behavior oftyping.NamedTuple
.
- class schrodinger.models.jsonable.JsonableEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
schrodinger.models.jsonable._JsonableEnumBase
,enum.Enum
- __init__(*args, **kwargs)¶
- classmethod fromJsonImplementation(json_obj)¶
Abstract method that must be defined by all derived classes. Takes in a dictionary and constructs an instance of the derived class.
- Parameters
json_dict (dict) – A dictionary loaded from a JSON string or file.
- Returns
An instance of the derived class.
- Return type
cls
- toJsonImplementation()¶
Abstract method that must be defined by all derived classes. Converts an instance of the derived class into a jsonifiable object.
- Returns
A dict made up of JSON native datatypes or Jsonable objects. See the link below for a table of such types. https://docs.python.org/2/library/json.html#encoders-and-decoders
- class schrodinger.models.jsonable.JsonableIntEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
int
,schrodinger.models.json.JsonableClassMixin
,enum.Enum
- __init__(*args, **kwargs)¶
- classmethod fromJsonImplementation(json_obj)¶
Abstract method that must be defined by all derived classes. Takes in a dictionary and constructs an instance of the derived class.
- Parameters
json_dict (dict) – A dictionary loaded from a JSON string or file.
- Returns
An instance of the derived class.
- Return type
cls
- toJsonImplementation()¶
Abstract method that must be defined by all derived classes. Converts an instance of the derived class into a jsonifiable object.
- Returns
A dict made up of JSON native datatypes or Jsonable objects. See the link below for a table of such types. https://docs.python.org/2/library/json.html#encoders-and-decoders
- class schrodinger.models.jsonable.AbstractJsonSerializer¶
Bases:
object
A class for defining how serialization should be done for a particular object. This should only be used if you’re unable to use
json.JsonableClassMixin
. This can be used in conjunction withjson.load(s)
andjson.dump(s)
.Subclasses must define
ObjectClass
andJsonableClass
and overrideobjectFromJsonable
andjsonableFromObject
.Create a subclass here to add a new class to the global default serialization registry. (Consult with relevant parties before doing so…)
- Variables
ObjectClass – The non-jsonable third-party class (e.g. set, rdkit.Mol, etc.)
JsonableClass – The class that subclasses
ObjectClass
and mixes in JsonableClassMixin.
- ObjectClass = NotImplemented¶
- JsonableClass = NotImplemented¶
- __init__()¶
- classmethod objectFromJsonable(jsonable_obj)¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(obj)¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- classmethod objectFromJson(json_obj)¶
DO NOT OVERRIDE.
Return an instance of ObjectClass from a json object (i.e. an object made up of json native types).
- class schrodinger.models.jsonable.StructureSerializer¶
Bases:
schrodinger.models.jsonable.AbstractJsonSerializer
- ObjectClass¶
- JsonableClass¶
- classmethod objectFromJsonable(jsonable_structure)¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(structure_)¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- class schrodinger.models.jsonable.TupleSerializer¶
Bases:
schrodinger.models.jsonable.AbstractJsonSerializer
- ObjectClass¶
alias of
tuple
- JsonableClass¶
- classmethod objectFromJsonable(jsonable_tuple)¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(tuple_)¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- class schrodinger.models.jsonable.BytesSerializer¶
Bases:
schrodinger.models.jsonable.AbstractJsonSerializer
- ObjectClass¶
alias of
bytes
- JsonableClass¶
- classmethod objectFromJsonable(jsonable_bytes: schrodinger.models.jsonable.JsonableBytes) bytes ¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(bytes_) schrodinger.models.jsonable.JsonableBytes ¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- class schrodinger.models.jsonable.SetSerializer¶
Bases:
schrodinger.models.jsonable.AbstractJsonSerializer
- ObjectClass¶
alias of
set
- JsonableClass¶
- classmethod objectFromJsonable(jsonable_set)¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(set_)¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- class schrodinger.models.jsonable.MolSerializer¶
Bases:
schrodinger.models.jsonable.AbstractJsonSerializer
- ObjectClass¶
alias of
rdkit.Chem.rdchem.Mol
- JsonableClass¶
alias of
schrodinger.models.jsonable._JsonableMolWrapper
- classmethod objectFromJsonable(jsonable_mol)¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(mol)¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- class schrodinger.models.jsonable.DateTimeSerializer¶
Bases:
schrodinger.models.jsonable.AbstractJsonSerializer
- ObjectClass¶
alias of
datetime.datetime
- JsonableClass¶
alias of
schrodinger.models.jsonable._JsonableDateTime
- classmethod objectFromJsonable(jsonable_datetime)¶
Return an instance of
ObjectClass
from an instance ofJsonableClass
- classmethod jsonableFromObject(dt)¶
Return an instance of
JsonableClass
from an instance ofObjectClass
- schrodinger.models.jsonable.serializer¶
- schrodinger.models.jsonable.get_default_serializer(DataClass)¶