U a"@sdZddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z d d lmZd d lmZGd ddZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d ZdS)!a Tagged JSON ~~~~~~~~~~~ A compact representation for lossless serialization of non-standard JSON types. :class:`~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. .. autoclass:: TaggedJSONSerializer :members: .. autoclass:: JSONTag :members: Let's see an example that adds support for :class:`~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 :class:`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``. .. code-block:: python 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) N b64decode) b64encode)datetimeUUIDMarkup http_date parse_date)dumps)loadsc@seZdZUdZdZdZejee d<dddddZ ej e d d d Z ej ej d d d Zej ej d ddZej ej d ddZdS)JSONTagzDBase class for defining type tags for :class:`TaggedJSONSerializer`. serializerNkeyTaggedJSONSerializer)rreturncCs ||_dS)z)Create a tagger for the given serializer.Nr)selfrrOC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-e702oxwa\flask\json\tag.py__init__BszJSONTag.__init__valuercCstdS)z6Check if the given value should be tagged by this tag.NNotImplementedErrorrrrrrcheckFsz JSONTag.checkcCstdS)zfConvert the Python object to an object that is a valid JSON type. The tag will be added later.Nrrrrrto_jsonJszJSONTag.to_jsoncCstdS)zbConvert the JSON representation back to the correct type. The tag will already be removed.Nrrrrr to_pythonOszJSONTag.to_pythoncCs|j||iS)zSConvert the value to a valid JSON type and add the tag structure around it.)rr!rrrrtagTsz JSONTag.tag)__name__ __module__ __qualname____doc__ __slots__rtOptionalstr__annotations__rAnyboolr r!r"r#rrrrr9s rc@sReZdZdZdZdZejedddZ ejejdddZ ejejdd d Z d S) TagDictzTag for 1-item dicts whose only key matches a registered tag. Internally, the dict key is suffixed with `__`, and the suffix is removed when deserializing. rz dircCs*t|to(t|dko(tt||jjkS)N) isinstancedictlennextiterrtagsrrrrr ds   z TagDict.checkcCs&tt|}|d|j||iS)N__)r4r5rr#rrrrrrr!ks zTagDict.to_jsoncCs tt|}|dd||iS)N)r4r5r8rrrr"os zTagDict.to_pythonN r$r%r&r'r(rr)r-r.r r!r"rrrrr/Zs r/c@s:eZdZdZejedddZejejdddZeZ dS)PassDictrrcCs t|tSN)r1r2rrrrr wszPassDict.checkcsfdd|DS)Ncsi|]\}}|j|qSrrr#).0kvrrr }sz$PassDict.to_json..)itemsrrrArr!zszPassDict.to_jsonN r$r%r&r(r)r-r.r r!r#rrrrr;tsr;c@sNeZdZdZdZejedddZejejdddZ ejejddd Z d S) TagTuplerz trcCs t|tSr<)r1tuplerrrrr szTagTuple.checkcsfdd|DS)Ncsg|]}j|qSrr=r>itemrArr sz$TagTuple.to_json..rrrrArr!szTagTuple.to_jsoncCst|Sr<)rFrrrrr"szTagTuple.to_pythonN r$r%r&r(rr)r-r.r r!r"rrrrrEs rEc@s:eZdZdZejedddZejejdddZeZ dS)PassListrrcCs t|tSr<)r1listrrrrr szPassList.checkcsfdd|DS)Ncsg|]}j|qSrr=rGrArrrIsz$PassList.to_json..rrrrArr!szPassList.to_jsonNrDrrrrrKsrKc@sNeZdZdZdZejedddZejejdddZ ejejddd Z d S) TagBytesrz brcCs t|tSr<)r1bytesrrrrr szTagBytes.checkcCst|dS)Nascii)rdecoderrrrr!szTagBytes.to_jsoncCst|Sr<rrrrrr"szTagBytes.to_pythonNrJrrrrrMs rMc@sReZdZdZdZdZejedddZ ejejdddZ ejejdd d Z d S) TagMarkupzSerialize anything matching the :class:`~markupsafe.Markup` API by having a ``__html__`` method to the result of that method. Always deserializes to an instance of :class:`~markupsafe.Markup`.rz mrcCstt|ddS)N__html__)callablegetattrrrrrr szTagMarkup.checkcCs t|Sr<)r+rRrrrrr!szTagMarkup.to_jsoncCst|Sr<rrrrrr"szTagMarkup.to_pythonNr:rrrrrQs rQc@sNeZdZdZdZejedddZejejdddZ ejejddd Z d S) TagUUIDrz urcCs t|tSr<)r1rrrrrr sz TagUUID.checkcCs|jSr<)hexrrrrr!szTagUUID.to_jsoncCst|Sr<rrrrrr"szTagUUID.to_pythonNrJrrrrrUs rUc@sNeZdZdZdZejedddZejejdddZ ejejddd Z d S) TagDateTimerz drcCs t|tSr<)r1rrrrrr szTagDateTime.checkcCst|Sr<r rrrrr!szTagDateTime.to_jsoncCst|Sr<r rrrrr"szTagDateTime.to_pythonNrJrrrrrWs rWc@seZdZdZdZeeeee e e e gZ ddddZdejeeejeddd d Zejejeejfd d d Zejeejfejd ddZejed ddZeejd ddZdS)ra|Serializer that uses a tag system to compactly represent objects that are not JSON types. Passed as the intermediate serializer to :class:`itsdangerous.Serializer`. The following extra types are supported: * :class:`dict` * :class:`tuple` * :class:`bytes` * :class:`~markupsafe.Markup` * :class:`~uuid.UUID` * :class:`~datetime.datetime` )r6orderN)rcCs&i|_g|_|jD]}||qdSr<)r6rX default_tagsregister)rclsrrrrs zTaggedJSONSerializer.__init__F) tag_classforceindexrcCsf||}|j}|dk r>|s4||jkr4td|d||j|<|dkrT|j|n|j||dS)aURegister a new tag with this serializer. :param tag_class: tag class to register. Will be instantiated with this serializer instance. :param force: overwrite an existing tag. If false (default), a :exc:`KeyError` is raised. :param index: index to insert the new tag in the tag order. Useful when the new tag is a special case of an existing tag. If ``None`` (default), the tag is appended to the end of the order. :raise KeyError: if the tag key is already registered and ``force`` is not true. NzTag 'z' is already registered.)rr6KeyErrorrXappendinsert)rr\r]r^r#rrrrrZs zTaggedJSONSerializer.registerrcCs(|jD]}||r||Sq|S)z8Convert a value to a tagged representation if necessary.)rXr r#)rrr#rrrr#s  zTaggedJSONSerializer.tagcCs>t|dkr|Stt|}||jkr*|S|j|||S)z:Convert a tagged representation back to the original type.r0)r3r4r5r6r"r8rrruntag&s    zTaggedJSONSerializer.untagcCst||ddS)z3Tag the value and dump it to a compact JSON string.),:) separators)rr#rrrrr2szTaggedJSONSerializer.dumpscCst||jdS)zALoad data from a JSON string and deserialized any tagged objects.) object_hook)rrbrrrrr6szTaggedJSONSerializer.loads)FN)r$r%r&r'r(r/r;rErKrMrQrUrWrYrr)Typerr.r*intrZr-Dictr+r#rbrrrrrrrs0   ! r)r'typingr)base64rrruuidrZ markupsafer Z werkzeug.httpr r jsonrrrr/r;rErKrMrQrUrWrrrrrs(*         !