Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(76)

Side by Side Diff: Lib/json/__init__.py

Issue 7311: [issue4136] merge json library with simplejson 2.0.3 Base URL: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: Created 5 years, 6 months ago , Downloaded from: http://bugs.python.org/file11822/json_issue4136_r66961.diff
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/json/decoder.py » ('j') | Lib/json/decoder.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 r"""A simple, fast, extensible JSON encoder and decoder 1 r"""A simple, fast, extensible JSON encoder and decoder
2 2
3 JSON (JavaScript Object Notation) <http://json.org> is a subset of 3 JSON (JavaScript Object Notation) <http://json.org> is a subset of
4 JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data 4 JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
5 interchange format. 5 interchange format.
6 6
7 json exposes an API familiar to uses of the standard library 7 json exposes an API familiar to uses of the standard library
8 marshal and pickle modules. 8 marshal and pickle modules.
9 9
10 Encoding basic Python object hierarchies:: 10 Encoding basic Python object hierarchies::
(...skipping 11 matching lines...) Expand all
22 {"a": 0, "b": 0, "c": 0} 22 {"a": 0, "b": 0, "c": 0}
23 >>> from StringIO import StringIO 23 >>> from StringIO import StringIO
24 >>> io = StringIO() 24 >>> io = StringIO()
25 >>> json.dump(['streaming API'], io) 25 >>> json.dump(['streaming API'], io)
26 >>> io.getvalue() 26 >>> io.getvalue()
27 '["streaming API"]' 27 '["streaming API"]'
28 28
29 Compact encoding:: 29 Compact encoding::
30 30
31 >>> import json 31 >>> import json
32 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) 32 >>> compact = json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
33 '[1,2,3,{"4":5,"6":7}]' 33 >>> # Can't assume dict ordering
34 >>> compact in ('[1,2,3,{"4":5,"6":7}]', '[1,2,3,{"6":7,"4":5}]')
35 True
34 36
35 Pretty printing (using repr() because of extraneous whitespace in the output):: 37 Pretty printing (using repr() because of extraneous whitespace in the output)::
36 38
37 >>> import json 39 >>> import json
38 >>> print repr(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)) 40 >>> print repr(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
39 '{\n "4": 5, \n "6": 7\n}' 41 '{\n "4": 5, \n "6": 7\n}'
40 42
41 Decoding JSON:: 43 Decoding JSON::
42 44
43 >>> import json 45 >>> import json
44 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') 46 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == ["foo", {"bar":[ "baz", None, 1.0, 2]}]
45 [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] 47 True
46 >>> json.loads('"\\"foo\\bar"') 48 >>> json.loads('"\\"foo\\bar"') == '"foo\x08ar'
47 u'"foo\x08ar' 49 True
48 >>> from StringIO import StringIO 50 >>> from StringIO import StringIO
49 >>> io = StringIO('["streaming API"]') 51 >>> io = StringIO('["streaming API"]')
50 >>> json.load(io) 52 >>> json.load(io) == ["streaming API"]
51 [u'streaming API'] 53 True
52 54
53 Specializing JSON object decoding:: 55 Specializing JSON object decoding::
54 56
55 >>> import json 57 >>> import json
56 >>> def as_complex(dct): 58 >>> def as_complex(dct):
57 ... if '__complex__' in dct: 59 ... if '__complex__' in dct:
58 ... return complex(dct['real'], dct['imag']) 60 ... return complex(dct['real'], dct['imag'])
59 ... return dct 61 ... return dct
60 ... 62 ...
61 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}', 63 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
62 ... object_hook=as_complex) 64 ... object_hook=as_complex)
63 (1+2j) 65 (1+2j)
64 >>> import decimal 66 >>> from decimal import Decimal
65 >>> json.loads('1.1', parse_float=decimal.Decimal) 67 >>> json.loads('1.1', parse_float=Decimal) == Decimal("1.1")
66 Decimal('1.1') 68 True
67 69
68 Extending JSONEncoder:: 70 Extending JSONEncoder::
69 71
70 >>> import json 72 >>> import json
71 >>> class ComplexEncoder(json.JSONEncoder): 73 >>> class ComplexEncoder(json.JSONEncoder):
72 ... def default(self, obj): 74 ... def default(self, obj):
73 ... if isinstance(obj, complex): 75 ... if isinstance(obj, complex):
74 ... return [obj.real, obj.imag] 76 ... return [obj.real, obj.imag]
75 ... return json.JSONEncoder.default(self, obj) 77 ... return json.JSONEncoder.default(self, obj)
76 ... 78 ...
77 >>> dumps(2 + 1j, cls=ComplexEncoder) 79 >>> dumps(2 + 1j, cls=ComplexEncoder)
78 '[2.0, 1.0]' 80 '[2.0, 1.0]'
79 >>> ComplexEncoder().encode(2 + 1j) 81 >>> ComplexEncoder().encode(2 + 1j)
80 '[2.0, 1.0]' 82 '[2.0, 1.0]'
81 >>> list(ComplexEncoder().iterencode(2 + 1j)) 83 >>> ''.join(ComplexEncoder().iterencode(2 + 1j))
82 ['[', '2.0', ', ', '1.0', ']'] 84 '[2.0, 1.0]'
83 85
84 86
85 Using json.tool from the shell to validate and 87 Using json.tool from the shell to validate and
86 pretty-print:: 88 pretty-print::
87 89
88 $ echo '{"json":"obj"}' | python -mjson.tool 90 $ echo '{"json":"obj"}' | python -mjson.tool
89 { 91 {
90 "json": "obj" 92 "json": "obj"
91 } 93 }
92 $ echo '{ 1.2:3.4}' | python -mjson.tool 94 $ echo '{ 1.2:3.4}' | python -mjson.tool
93 Expecting property name: line 1 column 2 (char 2) 95 Expecting property name: line 1 column 2 (char 2)
94 96
95 Note that the JSON produced by this module's default settings 97 Note that the JSON produced by this module's default settings
96 is a subset of YAML, so it may be used as a serializer for that as well. 98 is a subset of YAML, so it may be used as a serializer for that as well.
97 99
98 """ 100 """
99 101 __version__ = '2.0.3'
100 __version__ = '1.9'
101 __all__ = [ 102 __all__ = [
102 'dump', 'dumps', 'load', 'loads', 103 'dump', 'dumps', 'load', 'loads',
103 'JSONDecoder', 'JSONEncoder', 104 'JSONDecoder', 'JSONEncoder',
104 ] 105 ]
105 106
106 __author__ = 'Bob Ippolito <bob@redivi.com>' 107 __author__ = 'Bob Ippolito <bob@redivi.com>'
107 108
108 from .decoder import JSONDecoder 109 from .decoder import JSONDecoder
109 from .encoder import JSONEncoder 110 from .encoder import JSONEncoder
110 111
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 cls = JSONDecoder 310 cls = JSONDecoder
310 if object_hook is not None: 311 if object_hook is not None:
311 kw['object_hook'] = object_hook 312 kw['object_hook'] = object_hook
312 if parse_float is not None: 313 if parse_float is not None:
313 kw['parse_float'] = parse_float 314 kw['parse_float'] = parse_float
314 if parse_int is not None: 315 if parse_int is not None:
315 kw['parse_int'] = parse_int 316 kw['parse_int'] = parse_int
316 if parse_constant is not None: 317 if parse_constant is not None:
317 kw['parse_constant'] = parse_constant 318 kw['parse_constant'] = parse_constant
318 return cls(encoding=encoding, **kw).decode(s) 319 return cls(encoding=encoding, **kw).decode(s)
OLDNEW
« no previous file with comments | « no previous file | Lib/json/decoder.py » ('j') | Lib/json/decoder.py » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1278:e6ce13d99bf5