66 An object is an unordered set of name/value pairs. An object begins with
77 { left brace and ends with right brace }. Each name is followed by : (color)
88 and the name/value pairs are separated by , (comma)
9+
10+ json.tool can be used to validate json from the shell
11+ $echo '{"json":"obj"}' | python -mjson.tool
12+ {
13+ "json": "obj"
14+ }
15+ $echo '{1.2:3.4}' | python -mjson.tool
16+ Returns "Expecting property name enclosed in double quotes
17+
18+ Tutorial here: http://pymotw.com/2/json/
919"""
1020
1121import json
1222
1323
24+ def print_original_object (myobject ):
25+ """ Helper function to find out what the original object is """
26+ print "Original object is type: " , type (myobject )
27+ print "Original object is: " , repr (myobject )
28+
29+
30+ def print_returned_object (myobject ):
31+ """ Helpter function to find out what the returned object is """
32+ print "Returned object is type: " , type (myobject )
33+ print "Returned object is: " , repr (myobject )
34+
35+
1436def encoding_basic_python_object ():
37+ print "ENCODING BASIC PYTHON OBJECT"
1538 print "Example 1:"
16- a = json .dumps (['foo' , {'bar' : ('baz' , None , 1.0 , 2 )}])
17- print type (a ) #<type 'str'>
18- print a # ["foo", {"bar": ["baz", null, 1.0, 2]}]
39+ myobject = ['foo' , {'bar' : ('baz' , None , 1.0 , 2 )}]
40+ print_original_object (myobject )
41+ # Original object is type: <type 'list'>
42+ # Original object is ['foo', {'bar': ('baz', None, 1.0, 2)}]
43+ a = json .dumps (myobject )
44+ print_returned_object (myobject )
45+ # Returned object is type: <type 'list'>
46+ # Returned object is: ['foo', {'bar': ('baz', None, 1.0, 2)}]
1947
2048 print "Example 2:"
21- b = json .dumps ({"c" : 0 , "b" : 0 , "a" : 0 }, sort_keys = True )
22- print type (b ) #<type 'str'>
23- print b # {"a": 0, "b": 0, "c": 0}
49+ myobject = {"c" : 0 , "b" : 0 , "a" : 0 }
50+ print_original_object (myobject )
51+ # Original object is type: <type 'dict'>
52+ # Original object is {'a': 0, 'c': 0, 'b': 0}
53+ b = json .dumps (myobject , sort_keys = True )
54+ print_returned_object (myobject )
55+ # Returns type: <type 'str'>
56+ # Original object is: {"a": 0, "b": 0, "c": 0}
2457
2558
2659def compact_encoding ():
60+ print "COMPACT ENCODING"
2761 print "Example 1:"
28- c = json .dumps ([1 ,2 ,3 ,{'4' : 5 , '6' : 7 }], separators = (',' , ':' ))
29- print type (c ) #<type 'str'>
30- print c # [1,2,3,{"4":5,"6":7}]
62+ myobject = [1 ,2 ,3 ,{'4' : 5 , '6' : 7 }]
63+ print_original_object (myobject )
64+ # Original object is type: <type 'list'>
65+ # Original object is [1, 2, 3, {'4': 5, '6': 7}]
66+ c = json .dumps (myobject , separators = (',' , ':' ))
67+ print_returned_object (myobject )
68+ # Returned object is type: <type 'list'>
69+ # Returned object is: [1, 2, 3, {'4': 5, '6': 7}]
3170
3271
3372def pretty_print ():
73+ print "PRETTY PRINTING (tldr; use indent to make it look nice"
3474 print "Example 1:"
35- d = json .dumps ({'4' : 5 , '6' : 7 }, sort_keys = True ,
75+ myobject = {'4' : 5 , '6' : 7 }
76+ print_original_object (myobject )
77+ # Original object is type: <type 'dict'>
78+ # Original object is {'4': 5, '6': 7}
79+ d = json .dumps (myobject , sort_keys = True ,
3680 indent = 2 , separators = (',' , ':' ))
3781 print type (d ) #<type 'str'>
3882 print d
@@ -43,10 +87,16 @@ def pretty_print():
4387
4488
4589def decoding_JSON ():
90+ print "DECODING JSON"
4691 print "Example 1:"
47- e = json .loads ('["foo", {"bar":["baz", null, 1.0, 2]}]' )
48- print type (e ) #<type 'list'>
49- print e #[u'foo', {u'bar': u['baz', None, 1.0, 2]}]
92+ myobject = '["foo", {"bar":["baz", null, 1.0, 2]}]'
93+ print_original_object (myobject )
94+ # Original object is type: <type 'str'>
95+ # Original object is ["foo", {"bar":["baz", null, 1.0, 2]}]
96+ e = json .loads (myobject )
97+ print_returned_object (myobject )
98+ # Returned object is type: <type 'str'>
99+ # Returned object is: ["foo", {"bar":["baz", null, 1.0, 2]}]
50100
51101
52102#For specializing_JSON_object_decoding() example
@@ -57,14 +107,34 @@ def as_complex(dct):
57107
58108
59109def specializing_JSON_object_decoding ():
110+ print "Specializing JSON object decoding"
60111 print "Example 1:"
61- print json .loads ('{"__complex__": true, "real": 1, "imag": 2}' ,
112+ myobject = '{"__complex__": true, "real": 1, "imag": 2}'
113+ print_original_object (myobject )
114+ # Original object is type: <type 'str'>
115+ # Original object is {"__complex__": true, "real": 1, "imag": 2}
116+ f = json .loads (myobject ,
62117 object_hook = as_complex ) #(1+2j)
118+ print_returned_object (myobject )
119+ # Returned object is type: <type 'str'>
120+ # Returned object is: {"__complex__": true, "real": 1, "imag": 2}
121+
122+
123+ def dumped_length ():
124+ """ Shows different file sizes of each """
125+ print "DUMPED LENGTH"
126+ data = [ { 'a' :'A' , 'b' :(2 , 4 ), 'c' :3.0 } ]
127+ print 'DATA:' , repr (data ) # DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}]
128+ print 'repr(data) :' , len (repr (data )) # 35
129+ print 'dumps(data) :' , len (json .dumps (data )) # 35
130+ print 'dumps(data, indent=2) :' , len (json .dumps (data , indent = 2 )) # 76
131+ print 'dumps(data, separators):' , len (json .dumps (data , separators = (',' ,':' ))) # 29
63132
64133
65134if __name__ == '__main__' :
66- encoding_basic_python_object ()
67- compact_encoding ()
68- pretty_print ()
135+ # encoding_basic_python_object()
136+ # compact_encoding()
137+ # pretty_print()
69138 decoding_JSON ()
70- specializing_JSON_object_decoding ()
139+ #specializing_JSON_object_decoding()
140+ #dumped_length()
0 commit comments