unrest.coercers
Property
Property(self, name, type=None, formatter=None)
A Property wrapper used instead of a string in a Rest
properties parameter.
Arguments
- name: This property name
- type: The sqlalchemy type used for type coercion
- formatter: An optional function to transform the parameter as string
Serialize
Serialize(self, model, columns, properties, relationships)
Base serializer class
Casts python sqlalchemy data into a JSON compliant type according to the sqlalchemy column type.
Not all types are implemented as of now and it's fairly easy to add:
Just add a serialize_type
method for type
and it shall work.
The serialize class can be configured with the rest function and on the UnRest declaration.
For example:
from unrest.coercers import Serialize
class BetterSerialize(Serialize):
def serialize_matrix(self, type, data):
return data.matrix_to_string()
rest = UnRest(app, session, SerializeClass=BetterSerialize)
...
Arguments
- model: The sqlachemy item to serialize.
- columns: The list of columns to serialize.
- properties: The list of properties to serialize.
- relationships: The list of relationships to serialize.
dict
Serialize.dict()
Serialize the given model to a JSON compatible dict
Deserialize
Deserialize(self, payload, columns)
Base deserializer class
Casts raw data back to compatible python sqlalchemy type.
Not all types are implemented as of now and it's fairly easy to add:
Just add a deserialize_type
method for type
and it shall work.
The deserialize class can be configured with the rest function and on the UnRest declaration.
For example:
from unrest.coercers import Deserialize
class BetterDeserialize(Deserialize):
def deserialize_matrix(self, type, data):
return Matrix.from_string(data)
rest = UnRest(app, session, DeserializeClass=BetterDeserialize)
...
Arguments
- payload: The payload to deserialize
- columns: The list of columns to deserialize
merge
Deserialize.merge(item, payload=None)
Deserialize the given payload into the existing sqlalchemy item
create
Deserialize.create(factory)
Deserialize objects in the given payload into a list of new items
created with the factory
function.