B t1`H(@sFddlmZmZmZddlmZddlmZGdddeZeZ dS))absolute_importdivisionunicode_literals) string_types)PY3_ORDERED_DICTc@seZdZdZd ddZddZddZd d efd d Zd dZ d!ddZ e Z d"ddZ ddZ e ZddZddZddZddZddZdS)# JSONBackenda8Manages encoding and decoding using various backends. It tries these modules in this order: simplejson, json, demjson simplejson is a fast and popular backend and is tried first. json comes with Python and is tried second. demjson is the most permissive backend and is tried last. TcCs||_g|_i|_i|_i|_i|_i|_d|_|d|d|dddd|dd d d |d |d t }dd|if}d|ddf|||d|_dS)NF simplejsonjsonZdemjsonencodedecodeJSONDecodeErrorZjsonlibwriteread ReadErrorZyajlujson sort_keys)rZescape_forward_slashes)rr r zdjango.util.simplejson) _fallthrough_backend_names _encoders _decoders_encoder_options_decoder_options_decoder_exceptions _verified load_backendr)selfZ fallthroughsortZ json_optsrr6/tmp/pip-unpacked-wheel-wy5q9vwy/jsonpickle/backend.py__init__s(      zJSONBackend.__init__cCs|jr dStddS)z4Ensures that we've loaded at least one JSON backend.NzXjsonpickle requires at least one of the following: python2.6, simplejson, or demjson)rAssertionError)rrrr_verify:szJSONBackend._verifycCs ||_dS)a Disable jsonpickle's fallthrough-on-error behavior By default, jsonpickle tries the next backend when decoding or encoding using a backend fails. This can make it difficult to force jsonpickle to use a specific backend, and catch errors, because the error will be suppressed and may not be raised by the subsequent backend. Calling `enable_backend(False)` will make jsonpickle immediately re-raise any exceptions raised by the backends. N)r)renablerrrenable_fallthroughDszJSONBackend.enable_fallthroughdumpsloadscCsy t|}Wntk r dSXy*x$|dddD]}t||}q8WWntk r`dSX||j|||r||j|||sdSt|t r||j |||sdSn ||j |<|j |gif|j |gif|j|d|_dS)aLoad a JSON backend by name. This method loads a backend and sets up references to that backend's loads/dumps functions and exception classes. :param dumps: is the name of the backend's encode method. The method should take an object and return a string. Defaults to 'dumps'. :param loads: names the backend's method for the reverse operation -- returning a Python object from a string. :param loads_exc: can be either the name of the exception class used to denote decoding errors, or it can be a direct reference to the appropriate exception class itself. If it is a name, then the assumption is that an exception class of that name can be found in the backend module's namespace. :param load: names the backend's 'load' method. :param dump: names the backend's 'dump' method. :rtype bool: True on success, False if the backend could not be loaded. F.rNT) __import__ ImportErrorsplitgetattrAttributeError_storerr isinstancerrr setdefaultrrappendr)rnamer%r&Z loads_excmodattrrrrrUs*    zJSONBackend.load_backendcCsl|j|d|j|d|j|d|j|d|j|d||jkr\|j|t|j|_ dS)z,Remove all entries for a particular backend.N) rpoprrrrrremoveboolr)rr1rrrremove_backends  zJSONBackend.remove_backendNc Cs||js*|jd}|j||||dSxbt|jD]T\}}y|j||||dStk r}z|t|jdkrx|Wdd}~XYq6Xq6WdS)z Attempt to encode an object into JSON. This tries the loaded backends in order and passes along the last exception if no backend is able to encode the object. r)indent separatorsrN)r"rrbackend_encode enumerate Exceptionlen)robjr8r9r1idxerrrr s zJSONBackend.encodec Cs\|j|gif\}}|}|dk r.||d<|dk r>||d<|ft|}|j|||S)Nr8r9)rgetcopytupler) rr1r>r8r9optargs optkwargsZencoder_kwargsZ encoder_argsrrrr:szJSONBackend.backend_encodec Cs||js$|jd}|||Sxdt|jD]V\}}y |||S|j|k r}z|t|jdkrt|nWdd}~XYq0Xq0WdS)z Attempt to decode an object from a JSON string. This tries the loaded backends in order and passes along the last exception if no backends are able to decode the string. rrN)r"rrbackend_decoder;rr=)rstringr1r?r@rrrr s   zJSONBackend.decodecCs4|j|dif\}}|}|j||f||S)Nr)rrArBr)rr1rGrDrEZdecoder_kwargsrrrrFszJSONBackend.backend_decodecCs:||jkr&|j||jd|nd|}t|dS)a Set the preferred json backend. If a preferred backend is set then jsonpickle tries to use it before any other backend. For example:: set_preferred_backend('simplejson') If the backend is not one of the built-in jsonpickle backends (json/simplejson, or demjson) then you must load the backend prior to calling set_preferred_backend. AssertionError is raised if the backend has not been loaded. rz%The "%s" backend has not been loaded.N)rr5insertr!)rr1errmsgrrrset_preferred_backends   z!JSONBackend.set_preferred_backendcOs||f|j|<dS)a Associate encoder-specific options with an encoder. After calling set_encoder_options, any calls to jsonpickle's encode method will pass the supplied args and kwargs along to the appropriate backend's encode method. For example:: set_encoder_options('simplejson', sort_keys=True, indent=4) set_encoder_options('demjson', compactly=False) See the appropriate encoder's documentation for details about the supported arguments and keyword arguments. N)r)rr1argskwargsrrrset_encoder_optionsszJSONBackend.set_encoder_optionscOs||f|j|<dS)a  Associate decoder-specific options with a decoder. After calling set_decoder_options, any calls to jsonpickle's decode method will pass the supplied args and kwargs along to the appropriate backend's decode method. For example:: set_decoder_options('simplejson', encoding='utf8', cls=JSONDecoder) set_decoder_options('demjson', strict=True) See the appropriate decoder's documentation for details about the supported arguments and keyword arguments. N)r)rr1rKrLrrrset_decoder_options szJSONBackend.set_decoder_optionscCs6yt||||<Wntk r0||dSXdS)NFT)r+r,r7)rdctbackendr>r1rrrr-s  zJSONBackend._store)T)NN)NN)__name__ __module__ __qualname____doc__r r"r$ ValueErrorrr7r r%r:r r&rFrJrMrNr-rrrrrs   ' ;  rN) __future__rrrcompatrrobjectrr rrrrs   !