U )._r @s,dZddlmZddlmZddlmZddlmZddlmZddl m Z ddl m Z d d l m Z d d l mZd d lmZd d lmZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGd d!d!eZd"S)#a6 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 seen an example that adds support for :class:`~collections.OrderedDict`. Dicts don't have an order in Python or 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``. :: 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) :copyright: 2010 Pallets :license: BSD-3-Clause  b64decode) b64encode)datetimeUUIDMarkup http_date parse_date) iteritems) text_type)dumps)loadsc@s@eZdZdZdZdZddZddZdd Zd d Z d d Z dS)JSONTagzDBase class for defining type tags for :class:`TaggedJSONSerializer`. serializerNcCs ||_dS)z)Create a tagger for the given serializer.Nr)selfrrC:\Users\Miouzora\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask/json/tag.py__init__DszJSONTag.__init__cCstdS)z6Check if the given value should be tagged by this tag.NNotImplementedErrorrvaluerrrcheckHsz JSONTag.checkcCstdS)zfConvert the Python object to an object that is a valid JSON type. The tag will be added later.Nrrrrrto_jsonLszJSONTag.to_jsoncCstdS)zbConvert the JSON representation back to the correct type. The tag will already be removed.Nrrrrr to_pythonQszJSONTag.to_pythoncCs|j||iS)zSConvert the value to a valid JSON type and add the tag structure around it.)keyrrrrrtagVsz JSONTag.tag) __name__ __module__ __qualname____doc__ __slots__r!rrrr r"rrrrr;src@s0eZdZdZdZdZddZddZdd Zd 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 dicCs*t|to(t|dko(tt||jjkS)N) isinstancedictlennextiterrtagsrrrrrfs   z TagDict.checkcCs$tt|}|d|j||iS)N__)r-r.rr"rrr!rrrrms zTagDict.to_jsoncCs tt|}|dd||iS)N)r-r.r1rrrr qs zTagDict.to_pythonN r#r$r%r&r'r!rrr rrrrr(\s r(c@s$eZdZdZddZddZeZdS)PassDictrcCs t|tSN)r*r+rrrrryszPassDict.checkcstfddt|DS)Nc3s"|]\}}|j|fVqdSr5rr").0kvrrr sz#PassDict.to_json..)r+rrrr:rr|szPassDict.to_jsonNr#r$r%r'rrr"rrrrr4vsr4c@s,eZdZdZdZddZddZddZd S) TagTuplerz tcCs t|tSr5)r*tuplerrrrrszTagTuple.checkcsfdd|DS)Ncsg|]}j|qSrr6r7itemr:rr sz$TagTuple.to_json..rrrr:rrszTagTuple.to_jsoncCst|Sr5)r>rrrrr szTagTuple.to_pythonNr#r$r%r'r!rrr rrrrr=s r=c@s$eZdZdZddZddZeZdS)PassListrcCs t|tSr5)r*listrrrrrszPassList.checkcsfdd|DS)Ncsg|]}j|qSrr6r?r:rrrAsz$PassList.to_json..rrrr:rrszPassList.to_jsonNr<rrrrrCsrCc@s,eZdZdZdZddZddZddZd S) TagBytesrz bcCs t|tSr5)r*bytesrrrrrszTagBytes.checkcCst|dS)Nascii)rdecoderrrrrszTagBytes.to_jsoncCst|Sr5rrrrrr szTagBytes.to_pythonNrBrrrrrEs rEc@s0eZdZdZdZdZddZddZdd Zd S) TagMarkupzSerialize anything matching the :class:`~flask.Markup` API by having a ``__html__`` method to the result of that method. Always deserializes to an instance of :class:`~flask.Markup`.rz mcCstt|ddS)N__html__)callablegetattrrrrrrszTagMarkup.checkcCs t|Sr5)rrJrrrrrszTagMarkup.to_jsoncCst|Sr5rrrrrr szTagMarkup.to_pythonNr3rrrrrIs rIc@s,eZdZdZdZddZddZddZd S) TagUUIDrz ucCs t|tSr5)r*rrrrrrsz TagUUID.checkcCs|jSr5)hexrrrrrszTagUUID.to_jsoncCst|Sr5rrrrrr szTagUUID.to_pythonNrBrrrrrMs rMc@s,eZdZdZdZddZddZddZd S) TagDateTimerz dcCs t|tSr5)r*rrrrrrszTagDateTime.checkcCst|Sr5r rrrrrszTagDateTime.to_jsoncCst|Sr5r rrrrr szTagDateTime.to_pythonNrBrrrrrOs rOc@sZeZdZdZdZeeeee e e e gZ ddZdddZd d Zd d Zd dZddZdS)TaggedJSONSerializerawSerializer 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:`~flask.Markup` * :class:`~uuid.UUID` * :class:`~datetime.datetime` )r/ordercCs&i|_g|_|jD]}||qdSr5)r/rQ default_tagsregister)rclsrrrrs zTaggedJSONSerializer.__init__FNcCsd||}|j}|dk r<|s2||jkr2td|||j|<|dkrR|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. Nz Tag '{0}' is already registered.)r!r/KeyErrorformatrQappendinsert)rZ tag_classforceindexr"r!rrrrSs zTaggedJSONSerializer.registercCs(|jD]}||r||Sq|S)z8Convert a value to a tagged representation if necessary.)rQrr")rrr"rrrr"s  zTaggedJSONSerializer.tagcCs>t|dkr|Stt|}||jkr*|S|j|||S)z:Convert a tagged representation back to the original type.r))r,r-r.r/r r1rrruntag#s    zTaggedJSONSerializer.untagcCst||ddS)z3Tag the value and dump it to a compact JSON string.),:) separators)rr"rrrrr/szTaggedJSONSerializer.dumpscCst||jdS)zALoad data from a JSON string and deserialized any tagged objects.) object_hook)rr[rrrrr3szTaggedJSONSerializer.loads)FN)r#r$r%r&r'r(r4r=rCrErIrMrOrRrrSr"r[rrrrrrrPs"   rPN)r&base64rrruuidrZjinja2r Z werkzeug.httpr r _compatrrjsonrrobjectrr(r4r=rCrErIrMrOrPrrrrs**           !