-
Notifications
You must be signed in to change notification settings - Fork 995
/
jsontest.py
471 lines (373 loc) · 15 KB
/
jsontest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/python
# -*- coding: latin2 -*-
## this test suite is an almost verbatim copy of the jsontest.py test suite
## found in json-py available from http://sourceforge.net/projects/json-py/
## Copyright (C) 2005 Patrick D. Logan
## 2007-03-15 - Viktor Ferenczi (python@cx.hu)
## Added unit tests for encoder/decoder extensions.
## Added throughput measurement.
## Typical values on a 3.0GHz Intel P4: about 8Mbytes/s
## 2007-04-02 - Viktor Ferenczi (python@cx.hu)
## Added unit test for encoding with automatic dict key to str conversion.
## 2007-05-04 - Viktor Ferenczi (python@cx.hu)
## Added unit tests for unicode encoding/decoding.
## More realistic, grid like data used for performance tests.
import datetime
import math
import re
import time
import unittest
import cjson
_exception = cjson.DecodeError
# The object tests should be order-independent. They're not.
# i.e. they should test for existence of keys and values
# with read/write invariance.
def _removeWhitespace(str):
return str.replace(" ", "")
class JsonTest(unittest.TestCase):
def testReadEmptyObject(self):
obj = cjson.decode("{}")
self.assertEqual({}, obj)
def testWriteEmptyObject(self):
s = cjson.encode({})
self.assertEqual("{}", _removeWhitespace(s))
def testReadStringValue(self):
obj = cjson.decode('{ "name" : "Patrick" }')
self.assertEqual({"name": "Patrick"}, obj)
def testReadEscapedQuotationMark(self):
obj = cjson.decode(r'"\""')
self.assertEqual(r'"', obj)
# def testReadEscapedSolidus(self):
# obj = cjson.decode(r'"\/"')
# self.assertEqual(r'/', obj)
def testReadEscapedReverseSolidus(self):
obj = cjson.decode(r'"\\"')
self.assertEqual("\\", obj)
def testReadEscapedBackspace(self):
obj = cjson.decode(r'"\b"')
self.assertEqual("\b", obj)
def testReadEscapedFormfeed(self):
obj = cjson.decode(r'"\f"')
self.assertEqual("\f", obj)
def testReadEscapedNewline(self):
obj = cjson.decode(r'"\n"')
self.assertEqual("\n", obj)
def testReadEscapedCarriageReturn(self):
obj = cjson.decode(r'"\r"')
self.assertEqual("\r", obj)
def testReadEscapedHorizontalTab(self):
obj = cjson.decode(r'"\t"')
self.assertEqual("\t", obj)
def testReadEscapedHexCharacter(self):
obj = cjson.decode(r'"\u000A"')
self.assertEqual("\n", obj)
obj = cjson.decode(r'"\u1001"')
self.assertEqual("\u1001", obj)
def testWriteEscapedQuotationMark(self):
s = cjson.encode(r'"')
self.assertEqual(r'"\""', _removeWhitespace(s))
def testWriteEscapedSolidus(self):
s = cjson.encode(r"/")
# self.assertEqual(r'"\/"', _removeWhitespace(s))
self.assertEqual('"/"', _removeWhitespace(s))
def testWriteNonEscapedSolidus(self):
s = cjson.encode(r"/")
self.assertEqual(r'"/"', _removeWhitespace(s))
def testWriteEscapedReverseSolidus(self):
s = cjson.encode("\\")
self.assertEqual(r'"\\"', _removeWhitespace(s))
def testWriteEscapedBackspace(self):
s = cjson.encode("\b")
self.assertEqual(r'"\b"', _removeWhitespace(s))
def testWriteEscapedFormfeed(self):
s = cjson.encode("\f")
self.assertEqual(r'"\f"', _removeWhitespace(s))
def testWriteEscapedNewline(self):
s = cjson.encode("\n")
self.assertEqual(r'"\n"', _removeWhitespace(s))
def testWriteEscapedCarriageReturn(self):
s = cjson.encode("\r")
self.assertEqual(r'"\r"', _removeWhitespace(s))
def testWriteEscapedHorizontalTab(self):
s = cjson.encode("\t")
self.assertEqual(r'"\t"', _removeWhitespace(s))
def testWriteEscapedHexCharacter(self):
s = cjson.encode("\u1001")
self.assertEqual(r'"\u1001"', _removeWhitespace(s))
def testReadBadEscapedHexCharacter(self):
self.assertRaises(_exception, self.doReadBadEscapedHexCharacter)
def doReadBadEscapedHexCharacter(self):
cjson.decode('"\\u10K5"')
def testReadBadObjectKey(self):
self.assertRaises(_exception, self.doReadBadObjectKey)
def doReadBadObjectKey(self):
cjson.decode('{ 44 : "age" }')
def testReadBadArray(self):
self.assertRaises(_exception, self.doReadBadArray)
def doReadBadArray(self):
cjson.decode("[1,2,3,,]")
def testReadBadObjectSyntax(self):
self.assertRaises(_exception, self.doReadBadObjectSyntax)
def doReadBadObjectSyntax(self):
cjson.decode('{"age", 44}')
def testWriteStringValue(self):
s = cjson.encode({"name": "Patrick"})
self.assertEqual('{"name":"Patrick"}', _removeWhitespace(s))
def testReadIntegerValue(self):
obj = cjson.decode('{ "age" : 44 }')
self.assertEqual({"age": 44}, obj)
def testReadNegativeIntegerValue(self):
obj = cjson.decode('{ "key" : -44 }')
self.assertEqual({"key": -44}, obj)
def testReadFloatValue(self):
obj = cjson.decode('{ "age" : 44.5 }')
self.assertEqual({"age": 44.5}, obj)
def testReadNegativeFloatValue(self):
obj = cjson.decode(' { "key" : -44.5 } ')
self.assertEqual({"key": -44.5}, obj)
def testReadBadNumber(self):
self.assertRaises(_exception, self.doReadBadNumber)
def doReadBadNumber(self):
cjson.decode("-44.4.4")
def testReadSmallObject(self):
obj = cjson.decode('{ "name" : "Patrick", "age":44} ')
self.assertEqual({"age": 44, "name": "Patrick"}, obj)
def testReadEmptyArray(self):
obj = cjson.decode("[]")
self.assertEqual([], obj)
def testWriteEmptyArray(self):
self.assertEqual("[]", _removeWhitespace(cjson.encode([])))
def testReadSmallArray(self):
obj = cjson.decode(' [ "a" , "b", "c" ] ')
self.assertEqual(["a", "b", "c"], obj)
def testWriteSmallArray(self):
self.assertEqual("[1,2,3,4]", _removeWhitespace(cjson.encode([1, 2, 3, 4])))
def testWriteSmallObject(self):
s = cjson.encode({"name": "Patrick", "age": 44})
self.assertEqual('{"age":44,"name":"Patrick"}', _removeWhitespace(s))
def testWriteFloat(self):
self.assertEqual("3.44556677", _removeWhitespace(cjson.encode(3.44556677)))
def testReadTrue(self):
self.assertEqual(True, cjson.decode("true"))
def testReadFalse(self):
self.assertEqual(False, cjson.decode("false"))
def testReadNull(self):
self.assertEqual(None, cjson.decode("null"))
def testWriteTrue(self):
self.assertEqual("true", _removeWhitespace(cjson.encode(True)))
def testWriteFalse(self):
self.assertEqual("false", _removeWhitespace(cjson.encode(False)))
def testWriteNull(self):
self.assertEqual("null", _removeWhitespace(cjson.encode(None)))
def testReadArrayOfSymbols(self):
self.assertEqual([True, False, None], cjson.decode(" [ true, false,null] "))
def testWriteArrayOfSymbolsFromList(self):
self.assertEqual("[true,false,null]", _removeWhitespace(cjson.encode([True, False, None])))
def testWriteArrayOfSymbolsFromTuple(self):
self.assertEqual("[true,false,null]", _removeWhitespace(cjson.encode((True, False, None))))
def testReadComplexObject(self):
src = """
{ "name": "Patrick", "age" : 44, "Employed?" : true, "Female?" : false, "grandchildren":null }
"""
obj = cjson.decode(src)
self.assertEqual(
{"name": "Patrick", "age": 44, "Employed?": True, "Female?": False, "grandchildren": None}, obj
)
def testReadLongArray(self):
src = """[ "used",
"abused",
"confused",
true, false, null,
1,
2,
[3, 4, 5]]
"""
obj = cjson.decode(src)
self.assertEqual(["used", "abused", "confused", True, False, None, 1, 2, [3, 4, 5]], obj)
def testReadIncompleteArray(self):
self.assertRaises(_exception, self.doReadIncompleteArray)
def doReadIncompleteArray(self):
cjson.decode("[")
def testReadComplexArray(self):
src = """
[
{ "name": "Patrick", "age" : 44,
"Employed?" : true, "Female?" : false,
"grandchildren":null },
"used",
"abused",
"confused",
1,
2,
[3, 4, 5]
]
"""
obj = cjson.decode(src)
self.assertEqual(
[
{"name": "Patrick", "age": 44, "Employed?": True, "Female?": False, "grandchildren": None},
"used",
"abused",
"confused",
1,
2,
[3, 4, 5],
],
obj,
)
def testWriteComplexArray(self):
obj = [
{"name": "Patrick", "age": 44, "Employed?": True, "Female?": False, "grandchildren": None},
"used",
"abused",
"confused",
1,
2,
[3, 4, 5],
]
self.assertEqual(
'[{"Female?":false,"age":44,"name":"Patrick","grandchildren":null,"Employed?":true},"used","abused","confused",1,2,[3,4,5]]',
_removeWhitespace(cjson.encode(obj)),
)
def testReadWriteCopies(self):
orig_obj = {"a": ' " '}
json_str = cjson.encode(orig_obj)
copy_obj = cjson.decode(json_str)
self.assertEqual(orig_obj, copy_obj)
self.assertEqual(True, orig_obj == copy_obj)
self.assertEqual(False, orig_obj is copy_obj)
def testStringEncoding(self):
s = cjson.encode([1, 2, 3])
self.assertEqual(str("[1,2,3]", "utf-8"), _removeWhitespace(s))
def testReadEmptyObjectAtEndOfArray(self):
self.assertEqual(["a", "b", "c", {}], cjson.decode('["a","b","c",{}]'))
def testReadEmptyObjectMidArray(self):
self.assertEqual(["a", "b", {}, "c"], cjson.decode('["a","b",{},"c"]'))
def testReadClosingObjectBracket(self):
self.assertEqual({"a": [1, 2, 3]}, cjson.decode('{"a":[1,2,3]}'))
def testEmptyObjectInList(self):
obj = cjson.decode("[{}]")
self.assertEqual([{}], obj)
def testObjectWithEmptyList(self):
obj = cjson.decode('{"test": [] }')
self.assertEqual({"test": []}, obj)
def testObjectWithNonEmptyList(self):
obj = cjson.decode('{"test": [3, 4, 5] }')
self.assertEqual({"test": [3, 4, 5]}, obj)
def testWriteLong(self):
self.assertEqual("12345678901234567890", cjson.encode(12345678901234567890))
def testEncoderExtension(self):
def dateEncoder(d):
assert isinstance(d, datetime.date)
return "new Date(Date.UTC(%d,%d,%d))" % (d.year, d.month, d.day)
self.assertEqual(
cjson.encode([1, datetime.date(2007, 1, 2), 2], extension=dateEncoder),
"[1, new Date(Date.UTC(2007,1,2)), 2]",
)
self.assertRaises(cjson.EncodeError, lambda: cjson.encode(1, extension=0))
def testDecoderExtension(self):
re_date = re.compile("^new\sDate\(Date\.UTC\(.*?\)\)")
def dateDecoder(json, idx):
json = json[idx:]
m = re_date.match(json)
if not m:
raise "cannot parse JSON string as Date object: %s" % json[idx:]
args = cjson.decode("[%s]" % json[18 : m.end() - 2])
dt = datetime.date(*args)
return (dt, m.end())
self.assertEqual(
cjson.decode("[1, new Date(Date.UTC(2007,1,2)), 2]", extension=dateDecoder),
[1, datetime.date(2007, 1, 2), 2],
)
self.assertEqual(
cjson.decode("[1, new Date(Date.UTC( 2007, 1 , 2 )) , 2]", extension=dateDecoder),
[1, datetime.date(2007, 1, 2), 2],
)
self.assertRaises(cjson.DecodeError, lambda: cjson.decode("1", extension=0))
def testEncodeKey2Str(self):
d = {"1": "str 1", 1: "int 1", 3.1415: "pi"}
self.assertRaises(cjson.EncodeError, lambda: cjson.encode(d))
# NOTE: decode needed for order invariance
self.assertEqual(
cjson.decode(cjson.encode(d, key2str=True)), {"1": "str 1", "1": "int 1", "3.1415": "pi"}
)
def testUnicodeEncode(self):
self.assertEqual(cjson.encode({"b": 2}), '{"b": 2}')
self.assertEqual(cjson.encode({'o"': "öõüû"}), r'{"o\"": "\u00f6\u0151\u00fc\u0171"}')
self.assertEqual(cjson.encode("öõüû", encoding="latin2"), r'"\u00f6\u0151\u00fc\u0171"')
self.assertRaises(cjson.EncodeError, lambda: cjson.encode("öõüû", encoding="ascii"))
def testUnicodeDecode(self):
self.assertEqual(cjson.decode('{"b": 2}', all_unicode=True), {"b": 2})
self.assertEqual(cjson.decode(r'{"o\"": "\u00f6\u0151\u00fc\u0171"}'), {'o"': "öõüû"})
self.assertEqual(
cjson.decode(r'{"o\"": "\u00f6\u0151\u00fc\u0171"}', encoding="latin2"), {'o"': "öõüû"}
)
self.assertEqual(cjson.decode(r'"\u00f6\u0151\u00fc\u0171"', all_unicode=True), "öõüû")
self.assertEqual(cjson.decode(r'"\u00f6\u0151\u00fc\u0171"', encoding="latin2"), "öõüû")
self.assertRaises(cjson.DecodeError, lambda: cjson.decode('"öõüû"', encoding="ascii"))
def testUnicodeEncodeDecode(self):
for s in ("abc", "aáé", "öõüû"):
self.assertEqual(cjson.decode(cjson.encode(s)), s.decode("latin1"))
def measureEncoderThroughput(data):
bytes = 0
st = time.time()
cnt = 0
while True:
dt = time.time() - st
if dt >= 0.5 and cnt > 9:
break
bytes += len(cjson.encode(data))
cnt += 1
return int(bytes / 1024 / dt)
def measureDecoderThroughput(data):
json = cjson.encode(data)
bytes = 0
st = time.time()
cnt = 0
while True:
dt = time.time() - st
if dt >= 0.5 and cnt > 9:
break
cjson.decode(json)
bytes += len(json)
cnt += 1
return int(math.floor(bytes / dt / 1024.0 + 0.5))
def measureThroughput():
# Try to imitate realistic data, for example a large grid of records
data = [
dict(
[
(
"cell(%d,%d)" % (x, y),
(
None,
False,
True,
0,
1,
x + y,
x * y,
math.pi,
math.pi * x * y,
"str(%d,%d)%s" % (x, y, "#" * (x / 10)),
"unicode[%04X]:%s" % (x * y, chr(x * y)),
),
)
for x in range(y)
]
)
for y in range(1, 100)
]
json = cjson.encode(data)
print("Test data: tuples in dicts in a list, %d bytes as JSON string" % len(json))
print("Encoder throughput: ~%d kbyte/s" % measureEncoderThroughput(data))
print("Decoder throughput: ~%d kbyte/s" % measureDecoderThroughput(data))
def main():
try:
unittest.main()
# suite = unittest.TestLoader().loadTestsFromTestCase(JsonTest)
# unittest.TextTestRunner(verbosity=2).run(suite)
finally:
measureThroughput()
if __name__ == "__main__":
main()