コード例 #1
0
ファイル: jsonify.py プロジェクト: DINKIN/tg2
 def default(self, obj):
     if isinstance(obj, self._registered_types_list):
         # Minor optimization, enter loop only when we are instance of a supported type.
         for type_, encoder in self._registered_types_map.items():
             if isinstance(obj, type_):
                 return encoder(obj)
     elif hasattr(obj, '__json__') and callable(obj.__json__):
         return obj.__json__()
     elif isinstance(obj, (datetime.date, datetime.datetime)):
         if self._isodates:
             if isinstance(obj, datetime.datetime):
                 obj = obj.replace(microsecond=0)
             return obj.isoformat()
         else:
             return str(obj)
     elif isinstance(obj, decimal.Decimal):
         return float(obj)
     elif is_saobject(obj):
         return dictify_sqla(obj)
     elif is_mingobject(obj):
         return dictify_ming(obj)
     elif is_query_result(obj):
         return dict(rows=list(obj), count=obj.rowcount)
     elif is_query_row(obj):
         return dict(rows=dict(obj), count=1)
     elif is_objectid(obj):
         return str(obj)
     elif isinstance(obj, MultiDict):
         return obj.mixed()
     elif isinstance(obj, types.GeneratorType):
         return list(obj)
     else:
         return _JSONEncoder.default(self, obj)
コード例 #2
0
 def default(self, obj):
     if isinstance(obj, self._registered_types_list):
         # Minor optimization, enter loop only when we are instance of a supported type.
         for type_, encoder in self._registered_types_map.items():
             if isinstance(obj, type_):
                 return encoder(obj)
     elif hasattr(obj, '__json__') and callable(obj.__json__):
         return obj.__json__()
     elif isinstance(obj, (datetime.date, datetime.datetime)):
         if self._isodates:
             if isinstance(obj, datetime.datetime):
                 obj = obj.replace(microsecond=0)
             return obj.isoformat()
         else:
             return str(obj)
     elif isinstance(obj, decimal.Decimal):
         return float(obj)
     elif is_saobject(obj):
         return dictify_sqla(obj)
     elif is_mingobject(obj):
         return dictify_ming(obj)
     elif is_query_result(obj):
         return dict(rows=list(obj), count=obj.rowcount)
     elif is_query_row(obj):
         return dict(rows=dict(obj), count=1)
     elif is_objectid(obj):
         return str(obj)
     elif isinstance(obj, MultiDict):
         return obj.mixed()
     elif isinstance(obj, types.GeneratorType):
         return list(obj)
     else:
         return _JSONEncoder.default(self, obj)
コード例 #3
0
ファイル: jsonify.py プロジェクト: DINKIN/tg2
def encode(obj, encoder=None, iterencode=False):
    """Return a JSON string representation of a Python object."""
    if encoder is None:
        encoder = _default_encoder

    encode_func = encoder.encode
    if iterencode:
        encode_func = encoder.iterencode

    if isinstance(obj, string_type):
        return encode_func(obj)

    try:
        value = obj['test']
    except TypeError:
        if not hasattr(obj, '__json__') and not is_saobject(obj) and not is_mingobject(obj):
            raise JsonEncodeError('Your Encoded object must be dict-like.')
    except:
        pass

    return encode_func(obj)
コード例 #4
0
def encode(obj, encoder=None, iterencode=False):
    """Return a JSON string representation of a Python object."""
    if encoder is None:
        encoder = _default_encoder

    encode_func = encoder.encode
    if iterencode:
        encode_func = encoder.iterencode

    if isinstance(obj, string_type):
        return encode_func(obj)

    if encoder._allow_lists is False:
        try:
            value = obj['test']
        except TypeError:
            if not hasattr(obj, '__json__') and not is_saobject(obj) and not is_mingobject(obj):
                raise JsonEncodeError('Your Encoded object must be dict-like.')
        except:
            pass

    return encode_func(obj)