Skip to content
Myr

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

2 files+50 added−22 removed

modified:docs/api.rst+1 added-14 removed
340 unmodified lines
341342.. autoclass:: JSONDecoder343 :members:344345Tagged JSON346~~~~~~~~~~~345.. automodule:: flask.json.tag346348A compact representation for lossless serialization of non-standard JSON types.349:class:`~flask.sessions.SecureCookieSessionInterface` uses this to serialize350the session data, but it may be useful in other places.351352.. py:currentmodule:: flask.json.tag353354.. autoclass:: TaggedJSONSerializer355 :members:356357.. autoclass:: JSONTag358 :members:359347Template Rendering348------------------349350.. currentmodule:: flask
475 unmodified lines
modified:flask/json/tag.py+49 added-8 removed
1"""2Tagged JSON3~~~~~~~~~~~45A compact representation for lossless serialization of non-standard JSON types.6:class:`~flask.sessions.SecureCookieSessionInterface` uses this to serialize7the session data, but it may be useful in other places. It can be extended to8support other types.910.. autoclass:: TaggedJSONSerializer11 :members:1213.. autoclass:: JSONTag14 :members:1516Let's seen an example that adds support for :class:`~collections.OrderedDict`.17Dicts don't have an order in Python or JSON, so to handle this we will dump18the items as a list of ``[key, value]`` pairs. Subclass :class:`JSONTag` and19give it the new key ``' od'`` to identify the type. The session serializer20processes dicts first, so insert the new tag at the front of the order since21``OrderedDict`` must be processed before ``dict``. ::2223 from flask.json.tag import JSONTag2425 class TagOrderedDict(JSONTag):26 __slots__ = ('serializer',)27 key = ' od'2829 def check(self, value):30 return isinstance(value, OrderedDict)3132 def to_json(self, value):33 return [[k, self.serializer.tag(v)] for k, v in iteritems(value)]3435 def to_python(self, value):36 return OrderedDict(value)3738 app.session_interface.serializer.register(TagOrderedDict, 0)3940"""4142from base64 import b64decode, b64encode43from datetime import datetime44from uuid import UUID45
43 unmodified lines
89 Internally, the dict key is suffixed with `__`, and the suffix is removed90 when deserializing.91 """9252 __slots__ = ('serializer',)93 __slots__ = ()94 key = ' di'9596 def check(self, value):97 return (
11 unmodified lines
109 return {key[:-2]: value[key]}110111112class PassDict(JSONTag):72 __slots__ = ('serializer',)113 __slots__ = ()114115 def check(self, value):116 return isinstance(value, dict)117
5 unmodified lines
123 tag = to_json124125126class TagTuple(JSONTag):86 __slots__ = ('serializer',)127 __slots__ = ()128 key = ' t'129130 def check(self, value):131 return isinstance(value, tuple)
5 unmodified lines
137 return tuple(value)138139140class PassList(JSONTag):100 __slots__ = ('serializer',)141 __slots__ = ()142143 def check(self, value):144 return isinstance(value, list)145
3 unmodified lines
149 tag = to_json150151152class TagBytes(JSONTag):112 __slots__ = ('serializer',)153 __slots__ = ()154 key = ' b'155156 def check(self, value):157 return isinstance(value, bytes)
9 unmodified lines
167 """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`."""170130 __slots__ = ('serializer',)171 __slots__ = ()172 key = ' m'173174 def check(self, value):175 return callable(getattr(value, '__html__', None))
5 unmodified lines
181 return Markup(value)182183184class TagUUID(JSONTag):144 __slots__ = ('serializer',)185 __slots__ = ()186 key = ' u'187188 def check(self, value):189 return isinstance(value, UUID)
5 unmodified lines
195 return UUID(value)196197198class TagDateTime(JSONTag):158 __slots__ = ('serializer',)199 __slots__ = ()200 key = ' d'201202 def check(self, value):203 return isinstance(value, datetime)
94 unmodified lines