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 {}
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
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)
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))
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)
def __repr__(self): return encode_basestring_ascii(self.value)
def _format_dict(obj, deferred): pairs = (encode_basestring_ascii(k) + ':' + _format(v, deferred) for k, v in iteritems(obj)) return '{' + ','.join(pairs) + '}'