21
21
22
22
23
23
class _StringGenerator (object ):
24
- def __init__ (self , string ):
25
- self .string = string
26
- self .index = - 1
27
- def peek (self ):
28
- i = self .index + 1
29
- if i < len (self .string ):
30
- return self .string [i ]
31
- else :
32
- return None
33
- def next (self ):
34
- self .index += 1
35
- if self .index < len (self .string ):
36
- return self .string [self .index ]
37
- else :
38
- raise StopIteration
39
- def all (self ):
40
- return self .string
24
+ def __init__ (self , string ):
25
+ self .string = string
26
+ self .index = - 1
27
+ def peek (self ):
28
+ i = self .index + 1
29
+ if i < len (self .string ):
30
+ return self .string [i ]
31
+ else :
32
+ return None
33
+ def __next__ (self ):
34
+ self .index += 1
35
+ if self .index < len (self .string ):
36
+ return self .string [self .index ]
37
+ else :
38
+ raise StopIteration
39
+ def all (self ):
40
+ return self .string
41
41
42
42
class WriteException (Exception ):
43
43
pass
@@ -58,7 +58,7 @@ def _read(self):
58
58
self ._eatWhitespace ()
59
59
peek = self ._peek ()
60
60
if peek is None :
61
- raise ReadException , "Nothing to read: '%s'" % self ._generator .all ()
61
+ raise ReadException ( "Nothing to read: '%s'" % self ._generator .all () )
62
62
if peek == '{' :
63
63
return self ._readObject ()
64
64
elif peek == '[' :
@@ -77,7 +77,7 @@ def _read(self):
77
77
self ._readComment ()
78
78
return self ._read ()
79
79
else :
80
- raise ReadException , "Input is not valid JSON: '%s'" % self ._generator .all ()
80
+ raise ReadException ( "Input is not valid JSON: '%s'" % self ._generator .all () )
81
81
82
82
def _readTrue (self ):
83
83
self ._assertNext ('t' , "true" )
@@ -103,7 +103,7 @@ def _readNull(self):
103
103
104
104
def _assertNext (self , ch , target ):
105
105
if self ._next () != ch :
106
- raise ReadException , "Trying to read %s: '%s'" % (target , self ._generator .all ())
106
+ raise ReadException ( "Trying to read %s: '%s'" % (target , self ._generator .all () ))
107
107
108
108
def _readNumber (self ):
109
109
isfloat = False
@@ -119,7 +119,7 @@ def _readNumber(self):
119
119
else :
120
120
return int (result )
121
121
except ValueError :
122
- raise ReadException , "Not a valid JSON number: '%s'" % result
122
+ raise ReadException ( "Not a valid JSON number: '%s'" % result )
123
123
124
124
def _readString (self ):
125
125
result = ""
@@ -132,20 +132,20 @@ def _readString(self):
132
132
if ch in 'brnft' :
133
133
ch = self .escapes [ch ]
134
134
elif ch == "u" :
135
- ch4096 = self ._next ()
136
- ch256 = self ._next ()
137
- ch16 = self ._next ()
138
- ch1 = self ._next ()
139
- n = 4096 * self ._hexDigitToInt (ch4096 )
140
- n += 256 * self ._hexDigitToInt (ch256 )
141
- n += 16 * self ._hexDigitToInt (ch16 )
142
- n += self ._hexDigitToInt (ch1 )
143
- ch = unichr (n )
135
+ ch4096 = self ._next ()
136
+ ch256 = self ._next ()
137
+ ch16 = self ._next ()
138
+ ch1 = self ._next ()
139
+ n = 4096 * self ._hexDigitToInt (ch4096 )
140
+ n += 256 * self ._hexDigitToInt (ch256 )
141
+ n += 16 * self ._hexDigitToInt (ch16 )
142
+ n += self ._hexDigitToInt (ch1 )
143
+ ch = chr (n )
144
144
elif ch not in '"/\\ ' :
145
- raise ReadException , "Not a valid escaped JSON character: '%s' in %s" % (ch , self ._generator .all ())
145
+ raise ReadException ( "Not a valid escaped JSON character: '%s' in %s" % (ch , self ._generator .all () ))
146
146
result = result + ch
147
147
except StopIteration :
148
- raise ReadException , "Not a valid JSON string: '%s'" % self ._generator .all ()
148
+ raise ReadException ( "Not a valid JSON string: '%s'" % self ._generator .all () )
149
149
assert self ._next () == '"'
150
150
return result
151
151
@@ -155,8 +155,8 @@ def _hexDigitToInt(self, ch):
155
155
except KeyError :
156
156
try :
157
157
result = int (ch )
158
- except ValueError :
159
- raise ReadException , "The character %s is not a hex digit." % ch
158
+ except ValueError :
159
+ raise ReadException ( "The character %s is not a hex digit." % ch )
160
160
return result
161
161
162
162
def _readComment (self ):
@@ -167,7 +167,7 @@ def _readComment(self):
167
167
elif second == '*' :
168
168
self ._readCStyleComment ()
169
169
else :
170
- raise ReadException , "Not a valid JSON comment: %s" % self ._generator .all ()
170
+ raise ReadException ( "Not a valid JSON comment: %s" % self ._generator .all () )
171
171
172
172
def _readCStyleComment (self ):
173
173
try :
@@ -176,10 +176,10 @@ def _readCStyleComment(self):
176
176
ch = self ._next ()
177
177
done = (ch == "*" and self ._peek () == "/" )
178
178
if not done and ch == "/" and self ._peek () == "*" :
179
- raise ReadException , "Not a valid JSON comment: %s, '/*' cannot be embedded in the comment." % self ._generator .all ()
179
+ raise ReadException ( "Not a valid JSON comment: %s, '/*' cannot be embedded in the comment." % self ._generator .all () )
180
180
self ._next ()
181
181
except StopIteration :
182
- raise ReadException , "Not a valid JSON comment: %s, expected */" % self ._generator .all ()
182
+ raise ReadException ( "Not a valid JSON comment: %s, expected */" % self ._generator .all () )
183
183
184
184
def _readDoubleSolidusComment (self ):
185
185
try :
@@ -201,7 +201,7 @@ def _readArray(self):
201
201
if not done :
202
202
ch = self ._next ()
203
203
if ch != "," :
204
- raise ReadException , "Not a valid JSON array: '%s' due to: '%s'" % (self ._generator .all (), ch )
204
+ raise ReadException ( "Not a valid JSON array: '%s' due to: '%s'" % (self ._generator .all (), ch ) )
205
205
assert ']' == self ._next ()
206
206
return result
207
207
@@ -211,12 +211,12 @@ def _readObject(self):
211
211
done = self ._peek () == '}'
212
212
while not done :
213
213
key = self ._read ()
214
- if type (key ) is not types . StringType :
215
- raise ReadException , "Not a valid JSON object key (should be a string): %s" % key
214
+ if type (key ) is not bytes :
215
+ raise ReadException ( "Not a valid JSON object key (should be a string): %s" % key )
216
216
self ._eatWhitespace ()
217
217
ch = self ._next ()
218
218
if ch != ":" :
219
- raise ReadException , "Not a valid JSON object: '%s' due to: '%s'" % (self ._generator .all (), ch )
219
+ raise ReadException ( "Not a valid JSON object: '%s' due to: '%s'" % (self ._generator .all (), ch ) )
220
220
self ._eatWhitespace ()
221
221
val = self ._read ()
222
222
result [key ] = val
@@ -225,8 +225,8 @@ def _readObject(self):
225
225
if not done :
226
226
ch = self ._next ()
227
227
if ch != "," :
228
- raise ReadException , "Not a valid JSON array: '%s' due to: '%s'" % (self ._generator .all (), ch )
229
- assert self ._next () == "}"
228
+ raise ReadException ( "Not a valid JSON array: '%s' due to: '%s'" % (self ._generator .all (), ch ) )
229
+ assert self ._next () == "}"
230
230
return result
231
231
232
232
def _eatWhitespace (self ):
@@ -242,7 +242,7 @@ def _peek(self):
242
242
return self ._generator .peek ()
243
243
244
244
def _next (self ):
245
- return self ._generator . next ( )
245
+ return next ( self ._generator )
246
246
247
247
class JsonWriter (object ):
248
248
@@ -257,18 +257,18 @@ def write(self, obj, escaped_forward_slash=False):
257
257
258
258
def _write (self , obj ):
259
259
ty = type (obj )
260
- if ty is types . DictType :
260
+ if ty is dict :
261
261
n = len (obj )
262
262
self ._append ("{" )
263
- for k , v in obj .items ():
263
+ for k , v in list ( obj .items () ):
264
264
self ._write (k )
265
265
self ._append (":" )
266
266
self ._write (v )
267
267
n = n - 1
268
268
if n > 0 :
269
269
self ._append ("," )
270
270
self ._append ("}" )
271
- elif ty is types . ListType or ty is types . TupleType :
271
+ elif ty is list or ty is tuple :
272
272
n = len (obj )
273
273
self ._append ("[" )
274
274
for item in obj :
@@ -277,31 +277,29 @@ def _write(self, obj):
277
277
if n > 0 :
278
278
self ._append ("," )
279
279
self ._append ("]" )
280
- elif ty is types . StringType or ty is types . UnicodeType :
280
+ elif ty is bytes or ty is str :
281
281
self ._append ('"' )
282
- obj = obj .replace ('\\ ' , r'\\' )
282
+ obj = obj .replace ('\\ ' , r'\\' )
283
283
if self ._escaped_forward_slash :
284
284
obj = obj .replace ('/' , r'\/' )
285
- obj = obj .replace ('"' , r'\"' )
286
- obj = obj .replace ('\b ' , r'\b' )
287
- obj = obj .replace ('\f ' , r'\f' )
288
- obj = obj .replace ('\n ' , r'\n' )
289
- obj = obj .replace ('\r ' , r'\r' )
290
- obj = obj .replace ('\t ' , r'\t' )
285
+ obj = obj .replace ('"' , r'\"' )
286
+ obj = obj .replace ('\b ' , r'\b' )
287
+ obj = obj .replace ('\f ' , r'\f' )
288
+ obj = obj .replace ('\n ' , r'\n' )
289
+ obj = obj .replace ('\r ' , r'\r' )
290
+ obj = obj .replace ('\t ' , r'\t' )
291
291
self ._append (obj )
292
292
self ._append ('"' )
293
- elif ty is types . IntType or ty is types . LongType :
293
+ elif ty is int or ty is float :
294
294
self ._append (str (obj ))
295
- elif ty is types .FloatType :
296
- self ._append ("%f" % obj )
297
295
elif obj is True :
298
296
self ._append ("true" )
299
297
elif obj is False :
300
298
self ._append ("false" )
301
299
elif obj is None :
302
300
self ._append ("null" )
303
301
else :
304
- raise WriteException , "Cannot write in JSON: %s" % repr (obj )
302
+ raise WriteException ( "Cannot write in JSON: %s" % repr (obj ) )
305
303
306
304
def write (obj , escaped_forward_slash = False ):
307
305
return JsonWriter ().write (obj , escaped_forward_slash )
0 commit comments