예제 #1
0
    def encode(self, o):
        """Return a JSON string representation of a Python data structure.
        >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
        '{"foo": ["bar", "baz"]}'
        """
        # This is for extremely simple cases and benchmarks.
        if isinstance(o, six.string_types):
            if isinstance(o, str):
                _encoding = self.encoding
                if (_encoding is not None and not (_encoding == 'utf-8')):
                    o = o.decode(_encoding)
            if self.ensure_ascii:
                return encode_basestring_ascii(o)
            else:
                return encode_basestring(o)
        # This doesn't pass the iterator directly to ''.join() because the
        # exceptions aren't as detailed.  The list call should be roughly
        # equivalent to the PySequence_Fast that ''.join() would do.
        chunks = self.iterencode(o, _one_shot=True)
        if not isinstance(chunks, (list, tuple)):
            chunks = list(chunks)

        out = ''.join(chunks)
        out = re.sub(fix_key, r'\1 =', out)  # libconfig's name
        out = re.sub(fix_hex, r'\1', out)  # libconfig's hex
        out = re.sub(fix_hex, r'\1', out)  # libconfig's hex
        return out[1:-1]  # remove outter {}
예제 #2
0
def _atom(o):
    if isinstance(o, jsonb):
        return o
    elif isinstance(o, str):
        return encode_basestring_ascii(o)
    elif o is None:
        return 'null'
    elif o is True:
        return 'true'
    elif o is False:
        return 'false'
    elif isinstance(o, int):
        return int.__repr__(o)
    elif isinstance(o, float):
        if o != o:
            return 'NaN'
        elif o == _INFINITY:
            return 'Infinity'
        elif o == -_INFINITY:
            return '-Infinity'
        else:
            return float.__repr__(o)
    elif isinstance(o, Decimal):
        return str(o)  # keeps the decimals, float would truncate them
    # not a common type
    return None
예제 #3
0
파일: modjson.py 프로젝트: jesterpm/pyIRC
    def encode(self, o):
        """Return a JSON string representation of a Python data structure.

        >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
        '{"foo": ["bar", "baz"]}'

        """
        # This is for extremely simple cases and benchmarks.
        if isinstance(o, basestring):
            if isinstance(o, str):
                _encoding = self.encoding
                if (_encoding is not None
                        and not (_encoding == 'utf-8')):
                    o = o.decode(_encoding)
            if self.ensure_ascii:
                return encode_basestring_ascii(o)
            else:
                return encode_basestring(o)
        # This doesn't pass the iterator directly to ''.join() because the
        # exceptions aren't as detailed.  The list call should be roughly
        # equivalent to the PySequence_Fast that ''.join() would do.

        chunks = self.iterencode(o, {}, _one_shot=True)
        if not isinstance(chunks, (list, tuple)):
            chunks = list(chunks)
        return ''.join(chunks)
예제 #4
0
파일: json.py 프로젝트: Ajedi32/bpm
 def _encode_obj(obj, depth):
     if isinstance(obj, str):
         # Because who cares about portability
         yield encoder.encode_basestring_ascii(obj)
     elif obj is None:
         yield "null"
     elif obj is True:
         yield "true"
     elif obj is False:
         yield "false"
     elif isinstance(obj, int):
         yield str(obj)
     elif isinstance(obj, (tuple, list)):
         for chunk in _encode_list(obj, depth):
             yield chunk
     elif isinstance(obj, dict):
         for chunk in _encode_dict(obj, depth):
             yield chunk
     else:
         raise TypeError("Can't encode %r" % (obj))
예제 #5
0
 def _encode_obj(obj, depth):
     if isinstance(obj, str):
         # Because who cares about portability
         yield encoder.encode_basestring_ascii(obj)
     elif obj is None:
         yield "null"
     elif obj is True:
         yield "true"
     elif obj is False:
         yield "false"
     elif isinstance(obj, int):
         yield str(obj)
     elif isinstance(obj, (tuple, list)):
         for chunk in _encode_list(obj, depth):
             yield chunk
     elif isinstance(obj, dict):
         for chunk in _encode_dict(obj, depth):
             yield chunk
     else:
         raise TypeError("Can't encode %r" % (obj))
예제 #6
0
def _format(obj, deferred):
    if obj is None:
        return 'null'
    elif obj is True:
        return 'true'
    elif obj is False:
        return 'false'
    elif isinstance(obj, (int, float, datetime, date, time, Decimal)):
        return str(obj)
    elif isinstance(obj, string_types):
        return encode_basestring_ascii(obj)
    elif isinstance(obj, dict):
        return _format_dict(obj, deferred)
    elif isinstance(obj, (list, tuple)):
        return _format_list(obj, deferred)
    elif isinstance(obj, DeferredValue):
        return _format_deferred(obj, deferred)
    elif isinstance(obj, Condition):
        return _format_cond(obj, deferred)
    elif isinstance(obj, ConditionNode):
        return _format_tree(obj, deferred)
    raise ValueError('Unknown object type')
def brief_encode_basestring_ascii(s):
    text = encode_basestring_ascii(s)
    text = text[:HRDjangoJSONEncoder.MAX_ITEM_LENGTH - 3] + '...' \
        if len(text) > HRDjangoJSONEncoder.MAX_ITEM_LENGTH else text
    return json.dumps(text)
예제 #8
0
파일: nodes.py 프로젝트: vmagamedov/kinko
 def __repr__(self):
     return encode_basestring_ascii(self.value)
예제 #9
0
def _format_dict(obj, deferred):
    pairs = (encode_basestring_ascii(k) + ':' + _format(v, deferred)
             for k, v in iteritems(obj))
    return '{' + ','.join(pairs) + '}'
예제 #10
0
 def __repr__(self):
     return encode_basestring_ascii(self.value)