finish documentation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
[ci skip]
authored by David Lord · committed by David Lord · parent bbd15d5 · Browse files · 2 files·+50 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+50 added−22 removed
- other:+50 added−22 removed
Provenance
- Commit
9bee250- Tree
086b001- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
bbd15d5
2 files changed+50 added−22 removed
modified:docs/api.rst+1 added-14 removed
340 unmodified lines341 342 .. autoclass:: JSONDecoder343 :members:344 345-Tagged JSON346-~~~~~~~~~~~345+.. automodule:: flask.json.tag346 348-A compact representation for lossless serialization of non-standard JSON types.349-:class:`~flask.sessions.SecureCookieSessionInterface` uses this to serialize350-the session data, but it may be useful in other places.351-352-.. py:currentmodule:: flask.json.tag353-354-.. autoclass:: TaggedJSONSerializer355- :members:356-357-.. autoclass:: JSONTag358- :members:359-347 Template Rendering348 ------------------349 350 .. currentmodule:: flask475 unmodified linesmodified:flask/json/tag.py+49 added-8 removed
1+"""2+Tagged JSON3+~~~~~~~~~~~4+5+A compact representation for lossless serialization of non-standard JSON types.6+:class:`~flask.sessions.SecureCookieSessionInterface` uses this to serialize7+the session data, but it may be useful in other places. It can be extended to8+support other types.9+10+.. autoclass:: TaggedJSONSerializer11+ :members:12+13+.. autoclass:: JSONTag14+ :members:15+16+Let's seen an example that adds support for :class:`~collections.OrderedDict`.17+Dicts don't have an order in Python or JSON, so to handle this we will dump18+the items as a list of ``[key, value]`` pairs. Subclass :class:`JSONTag` and19+give it the new key ``' od'`` to identify the type. The session serializer20+processes dicts first, so insert the new tag at the front of the order since21+``OrderedDict`` must be processed before ``dict``. ::22+23+ from flask.json.tag import JSONTag24+25+ class TagOrderedDict(JSONTag):26+ __slots__ = ('serializer',)27+ key = ' od'28+29+ def check(self, value):30+ return isinstance(value, OrderedDict)31+32+ def to_json(self, value):33+ return [[k, self.serializer.tag(v)] for k, v in iteritems(value)]34+35+ def to_python(self, value):36+ return OrderedDict(value)37+38+ app.session_interface.serializer.register(TagOrderedDict, 0)39+40+"""41+42 from base64 import b64decode, b64encode43 from datetime import datetime44 from uuid import UUID45 43 unmodified lines89 Internally, the dict key is suffixed with `__`, and the suffix is removed90 when deserializing.91 """92 52- __slots__ = ('serializer',)93+ __slots__ = ()94 key = ' di'95 96 def check(self, value):97 return (11 unmodified lines109 return {key[:-2]: value[key]}110 111 112 class PassDict(JSONTag):72- __slots__ = ('serializer',)113+ __slots__ = ()114 115 def check(self, value):116 return isinstance(value, dict)117 5 unmodified lines123 tag = to_json124 125 126 class TagTuple(JSONTag):86- __slots__ = ('serializer',)127+ __slots__ = ()128 key = ' t'129 130 def check(self, value):131 return isinstance(value, tuple)5 unmodified lines137 return tuple(value)138 139 140 class PassList(JSONTag):100- __slots__ = ('serializer',)141+ __slots__ = ()142 143 def check(self, value):144 return isinstance(value, list)145 3 unmodified lines149 tag = to_json150 151 152 class TagBytes(JSONTag):112- __slots__ = ('serializer',)153+ __slots__ = ()154 key = ' b'155 156 def check(self, value):157 return isinstance(value, bytes)9 unmodified lines167 """Serialize anything matching the :class:`~flask.Markup` API by168 having a ``__html__`` method to the result of that method. Always169 deserializes to an instance of :class:`~flask.Markup`."""170 130- __slots__ = ('serializer',)171+ __slots__ = ()172 key = ' m'173 174 def check(self, value):175 return callable(getattr(value, '__html__', None))5 unmodified lines181 return Markup(value)182 183 184 class TagUUID(JSONTag):144- __slots__ = ('serializer',)185+ __slots__ = ()186 key = ' u'187 188 def check(self, value):189 return isinstance(value, UUID)5 unmodified lines195 return UUID(value)196 197 198 class TagDateTime(JSONTag):158- __slots__ = ('serializer',)199+ __slots__ = ()200 key = ' d'201 202 def check(self, value):203 return isinstance(value, datetime)94 unmodified lines