U ]@sddlZddlmZddlmZmZmZmZmZzddlm Z Wne k rZe j Z YnXz ddl ZWne k rdZYnXddlmZmamamamZddlmZddlmZmZGd d d eZGd d d eZdS) N)isclass) JSONEncoder_make_iterencodeencode_basestringencode_basestring_asciiINFINITY) FLOAT_REPRF) get_all_argsPY2_BUILTINS_STRPY3_BUILTINS_STR builtins_str ClassType) JSONObject)#get_qualified_name_for_class_objectget_qualified_name_for_classc@seZdZddZdddZdS)JSONExtendedEncodercCs t||S)aCustom isinstance method Override this method if you want to custom treatment for classes inheriting from builtins such as `dict` or `list`. Returning `False` for these classes, causes the `default` method to be called for the object. See `JSONObjectEncoder` for an example on how to make use of this. :param obj: Object to be encoded :param cls: Class which is checked for :return: `True` if `obj` is an instance of `cls`, `False` else :rtype: False  isinstanceselfobjclsr=/tmp/pip-target-hkr6o2s8/lib/python/jsonconversion/encoder.pyr s zJSONExtendedEncoder.isinstanceFc Cs|jr i}nd}|jrt}nt}t|dddkr@||jfdd}|jttt fdd}t ||j ||j ||j |j |j|j||jd }||d S) aZThis is almost a copy of the base class implementation of this method The changes are inspired by http://stackoverflow.com/a/17684652 The execution might be slower than that of the base class, as the `c_make_encoder` is never called. However, the changes allow the implementation of a custom `isinstance` method. NZenocingzutf-8cSst|tr||}||S)N)rstrdecode)oZ _orig_encoderZ _encodingrrr_encoder>s  z0JSONExtendedEncoder.iterencode.._encodercSsJ||krd}n$||krd}n||kr*d}n||S|sFtdt||S)NNaNInfinityz -Infinityz2Out of range float values are not JSON compliant: ) ValueErrorrepr)r allow_nan_repr_inf_neginftextrrrfloatstrCsz0JSONExtendedEncoder.iterencode..floatstrrr)check_circular ensure_asciirrgetattrencodingr"rrrdefaultindent key_separatoritem_separator sort_keysskipkeysr)rr _one_shotmarkersrr' _iterencoderrr iterencode.s.zJSONExtendedEncoder.iterencodeN)F)__name__ __module__ __qualname__rr5rrrrrsrcs8eZdZdZfddZfddZfddZZS)JSONObjectEncoderaCustom JSON encoder class especially for state machines This JSON encoder class inherits from the basic JSON encoder class. It can encode all classes deriving from JSONObject. In addition, tuples (encoded by :py:class:`JSONObjectEncoder`) are maintained and type objects (e.g. int, object, float) are handled. Finally, it is tried to convert dictionary keys to integers. c st|_d|krD|d}|ttfkr,||_ntdk rDtdtttt|j}t |}t | D]}||krd||=qd|f|dS)Nr z(builtins_str must be either '{}' or '{}') r popr r r formatsuperr9__init__r listkeys)rkwargsZcustom_builtins_strZparental_constructorZparental_constructor_argskey __class__rrr=es   zJSONObjectEncoder.__init__cs$t|ttfrdStt|||S)NF)rsettupler<r9rrBrrrzszJSONObjectEncoder.isinstancecst|tr&|}t||j|d<|St|ttfrVt|rLdt||jiSd|j iSt|t rx|jdt |d}|St|t r|jdt |d}|St rt|t jrd|dStt||SdS)aTThis method is called after all base class encoding logic Here, additional conversions of objects to dictionaries can be defined. Both JSONObject and type objects are treated. :param obj: Object to be converted to a dictionary :return: Dictionary representing the given object :rtype: dict __jsonqualname__Z__type__z.set)rFitemsz.tuplez numpy.ndarrayN)rrZto_dictrr typer rrr6rDr>rEnpZndarraytolistr<r9r,)rr dictionaryrBrrr,s*    zJSONObjectEncoder.default)r6r7r8__doc__r=rr, __classcell__rrrBrr9]s  r9)sysinspectrZ json.encoderrrrrrr ImportErrorfloat__repr__numpyrIjsonconversionr r r r r Zjsonconversion.jsonobjectrZjsonconversion.conversionrrrr9rrrr s     ?