This module is just wrapper over python’s JSON module. It allows you to serialize collections.namedtuple() and other default python data in very comfortable way - if you initialize it properly, it just works and you don’t have to take care about anything.
Note
This module exists only because standard JSON module can’t serialize collections.namedtuple(). If you don’t need to serialize collections.namedtuple(), you don’t need this module.
collections.namedtuple() is serialized to dict with special property __nt_name, where the name of the collections.namedtuple() class is stored.
Live example:
>>> from collections import namedtuple
>>> from edeposit.amqp.serializers import serialize
>>>
>>> Person = namedtuple("Person", ["name", "surname"])
>>> p = Person("Lishaak", "Bystroushaak")
>>> p
Person(name='Lishaak', surname='Bystroushaak')
>>> serialize(p)
'{"surname": "Bystroushaak", "name": "Lishaak", "__nt_name": "Person"}'
If you try to serialize module from different hierarchy path, than where it will be deserialized, you may run into problems with isinstance(), which compares by full path, not just by class identity.
Lets take object p from previous example:
>>> p
Person(name='Lishaak', surname='Bystroushaak')
>>> type(p)
<class '__main__.Person'>
As you can see, type of the object is <class '__main__.Person'>. In case where the collections.namedtuple() class would be defined in module Y, you would get something like <class 'Y.Person'> and you would run into errors, when you would try to compare these two for identity.
This is not the issue in normal collections.namedtuple() usage, because you usually wouldn´t have two definitions of same class. In case of desesrialization, you can run into this problems.
Trivial solution is to compare without full paths, just the names of the classes by using iiOfAny().
Serialize class hierarchy into JSON.
Parameters: | data (any) – any python type serializable to json, with added support of collections.namedtuple() |
---|---|
Returns: | unicode – json string |
Deserialize classes from JSON back to python data.
Parameters: |
---|
Call example:
deserialize(data, globals())
Warning
Call the globals() every time, you call this function, because your variable context could change. Also don’t pass a blank dict - it may work sometimes, but fail unpredictably later.
Returns: | any – any python type (make sure you have namedtuples imported) |
---|
Returns true, if instance is instance of any (iiOfAny) of the classes.
This function doesn’t use isinstance() check, it just compares the class names.
This can be generaly dangerous, but it is really useful when you are comparing class serialized in one module and deserialized in another.
This causes, that module paths in class internals are different and isinstance() and type() comparsions thus fails.
Use this function instead, if you wan’t to check what type is your deserialized message.
Parameters: |
|
---|---|
Returns: | bool – True if instance can be instance of any of the classes. |
Whole project is hosted at github:
and can be installed using PIP:
pip install edeposit.amqp.serializers