module documentation

A compact representation for lossless serialization of non-standard JSON types. ~flask.sessions.SecureCookieSessionInterface uses this to serialize the session data, but it may be useful in other places. It can be extended to support other types.

Let's see an example that adds support for ~collections.OrderedDict. Dicts don't have an order in JSON, so to handle this we will dump the items as a list of [key, value] pairs. Subclass JSONTag and give it the new key ' od' to identify the type. The session serializer processes dicts first, so insert the new tag at the front of the order since OrderedDict must be processed before dict.

from flask.json.tag import JSONTag

class TagOrderedDict(JSONTag):
    __slots__ = ('serializer',)
    key = ' od'

    def check(self, value):
        return isinstance(value, OrderedDict)

    def to_json(self, value):
        return [[k, self.serializer.tag(v)] for k, v in iteritems(value)]

    def to_python(self, value):
        return OrderedDict(value)

app.session_interface.serializer.register(TagOrderedDict, index=0)
Class ​JSONTag Base class for defining type tags for TaggedJSONSerializer.
Class ​Tagged​JSONSerializer Serializer that uses a tag system to compactly represent objects that are not JSON types. Passed as the intermediate serializer to itsdangerous.Serializer.
Class ​Pass​Dict Undocumented
Class ​Pass​List Undocumented
Class ​Tag​Bytes Undocumented
Class ​Tag​Date​Time Undocumented
Class ​Tag​Dict Tag for 1-item dicts whose only key matches a registered tag.
Class ​Tag​Markup Serialize anything matching the ~markupsafe.Markup API by having a __html__ method to the result of that method. Always deserializes to an instance of ~markupsafe.Markup.
Class ​Tag​Tuple Undocumented
Class ​Tag​UUID Undocumented