1515import calendar
1616import StringIO
1717import re
18+ import random
19+ import decimal
1820from functools import partial
1921import pandas .util .py3compat as py3compat
2022
@@ -36,6 +38,72 @@ def _skip_if_python_ver(skip_major, skip_minor=None):
3638 else partial (json .dumps , encoding = "utf-8" ))
3739
3840class UltraJSONTests (TestCase ):
41+
42+ def test_encodeDecimal (self ):
43+ sut = decimal .Decimal ("1337.1337" )
44+ encoded = ujson .encode (sut , double_precision = 100 )
45+ decoded = ujson .decode (encoded )
46+ self .assertEquals (decoded , 1337.1337 )
47+
48+ def test_encodeStringConversion (self ):
49+ input = "A string \\ / \b \f \n \r \t </script> &"
50+ not_html_encoded = '"A string \\ \\ \\ / \\ b \\ f \\ n \\ r \\ t <\\ /script> &"'
51+ html_encoded = '"A string \\ \\ \\ / \\ b \\ f \\ n \\ r \\ t \\ u003c\\ /script\\ u003e \\ u0026"'
52+
53+ def helper (expected_output , ** encode_kwargs ):
54+ output = ujson .encode (input , ** encode_kwargs )
55+ self .assertEquals (input , json .loads (output ))
56+ self .assertEquals (output , expected_output )
57+ self .assertEquals (input , ujson .decode (output ))
58+
59+ # Default behavior assumes encode_html_chars=False.
60+ helper (not_html_encoded , ensure_ascii = True )
61+ helper (not_html_encoded , ensure_ascii = False )
62+
63+ # Make sure explicit encode_html_chars=False works.
64+ helper (not_html_encoded , ensure_ascii = True , encode_html_chars = False )
65+ helper (not_html_encoded , ensure_ascii = False , encode_html_chars = False )
66+
67+ # Make sure explicit encode_html_chars=True does the encoding.
68+ helper (html_encoded , ensure_ascii = True , encode_html_chars = True )
69+ helper (html_encoded , ensure_ascii = False , encode_html_chars = True )
70+
71+ def test_doubleLongIssue (self ):
72+ sut = {u'a' : - 4342969734183514 }
73+ encoded = json .dumps (sut )
74+ decoded = json .loads (encoded )
75+ self .assertEqual (sut , decoded )
76+ encoded = ujson .encode (sut , double_precision = 100 )
77+ decoded = ujson .decode (encoded )
78+ self .assertEqual (sut , decoded )
79+
80+ def test_doubleLongDecimalIssue (self ):
81+ sut = {u'a' : - 12345678901234.56789012 }
82+ encoded = json .dumps (sut )
83+ decoded = json .loads (encoded )
84+ self .assertEqual (sut , decoded )
85+ encoded = ujson .encode (sut , double_precision = 100 )
86+ decoded = ujson .decode (encoded )
87+ self .assertEqual (sut , decoded )
88+
89+
90+ def test_encodeDecodeLongDecimal (self ):
91+ sut = {u'a' : - 528656961.4399388 }
92+ encoded = ujson .dumps (sut , double_precision = 15 )
93+ ujson .decode (encoded )
94+
95+ def test_decimalDecodeTest (self ):
96+ sut = {u'a' : 4.56 }
97+ encoded = ujson .encode (sut )
98+ decoded = ujson .decode (encoded )
99+ self .assertNotEqual (sut , decoded )
100+
101+ def test_decimalDecodeTestPrecise (self ):
102+ sut = {u'a' : 4.56 }
103+ encoded = ujson .encode (sut )
104+ decoded = ujson .decode (encoded , precise_float = True )
105+ self .assertEqual (sut , decoded )
106+
39107 def test_encodeDictWithUnicodeKeys (self ):
40108 input = { u"key1" : u"value1" , u"key1" : u"value1" , u"key1" : u"value1" , u"key1" : u"value1" , u"key1" : u"value1" , u"key1" : u"value1" }
41109 output = ujson .encode (input )
@@ -59,6 +127,7 @@ def test_encodeWithDecimal(self):
59127 def test_encodeDoubleNegConversion (self ):
60128 input = - math .pi
61129 output = ujson .encode (input )
130+
62131 self .assertEquals (round (input , 5 ), round (json .loads (output ), 5 ))
63132 self .assertEquals (round (input , 5 ), round (ujson .decode (output ), 5 ))
64133
@@ -93,10 +162,6 @@ def test_doublePrecisionTest(self):
93162 self .assertEquals (round (input , 3 ), json .loads (output ))
94163 self .assertEquals (round (input , 3 ), ujson .decode (output ))
95164
96- output = ujson .encode (input )
97- self .assertEquals (round (input , 5 ), json .loads (output ))
98- self .assertEquals (round (input , 5 ), ujson .decode (output ))
99-
100165 def test_invalidDoublePrecision (self ):
101166 input = 30.12345678901234567890
102167 output = ujson .encode (input , double_precision = 20 )
@@ -373,6 +438,15 @@ def test_decodeBrokenArrayEnd(self):
373438 return
374439 assert False , "Wrong exception"
375440
441+ def test_decodeArrayDepthTooBig (self ):
442+ input = '[' * (1024 * 1024 )
443+ try :
444+ ujson .decode (input )
445+ assert False , "Expected exception!"
446+ except (ValueError ):
447+ return
448+ assert False , "Wrong exception"
449+
376450 def test_decodeBrokenObjectEnd (self ):
377451 input = "}"
378452 try :
@@ -382,6 +456,15 @@ def test_decodeBrokenObjectEnd(self):
382456 return
383457 assert False , "Wrong exception"
384458
459+ def test_decodeObjectDepthTooBig (self ):
460+ input = '{' * (1024 * 1024 )
461+ try :
462+ ujson .decode (input )
463+ assert False , "Expected exception!"
464+ except (ValueError ):
465+ return
466+ assert False , "Wrong exception"
467+
385468 def test_decodeStringUnterminated (self ):
386469 input = "\" TESTING"
387470 try :
@@ -567,7 +650,7 @@ def test_numericIntFrcExp(self):
567650 self .assertAlmostEqual (output , json .loads (input ))
568651
569652 def test_decodeNumericIntExpEPLUS (self ):
570- input = "1337E+40 "
653+ input = "1337E+9 "
571654 output = ujson .decode (input )
572655 self .assertAlmostEqual (output , json .loads (input ))
573656
@@ -1192,7 +1275,165 @@ def test_datetimeindex(self):
11921275 decoded = Series (ujson .decode (ujson .encode (ts )))
11931276 idx_values = decoded .index .values .astype (np .int64 )
11941277 decoded .index = DatetimeIndex (idx_values )
1195- tm .assert_series_equal (np .round (ts , 5 ), decoded )
1278+ tm .assert_series_equal (ts , decoded )
1279+
1280+ def test_decodeArrayTrailingCommaFail (self ):
1281+ input = "[31337,]"
1282+ try :
1283+ ujson .decode (input )
1284+ except ValueError :
1285+ pass
1286+ else :
1287+ assert False , "expected ValueError"
1288+
1289+ def test_decodeArrayLeadingCommaFail (self ):
1290+ input = "[,31337]"
1291+ try :
1292+ ujson .decode (input )
1293+ except ValueError :
1294+ pass
1295+ else :
1296+ assert False , "expected ValueError"
1297+
1298+ def test_decodeArrayOnlyCommaFail (self ):
1299+ input = "[,]"
1300+ try :
1301+ ujson .decode (input )
1302+ except ValueError :
1303+ pass
1304+ else :
1305+ assert False , "expected ValueError"
1306+
1307+ def test_decodeArrayUnmatchedBracketFail (self ):
1308+ input = "[]]"
1309+ try :
1310+ ujson .decode (input )
1311+ except ValueError :
1312+ pass
1313+ else :
1314+ assert False , "expected ValueError"
1315+
1316+ def test_decodeArrayEmpty (self ):
1317+ input = "[]"
1318+ ujson .decode (input )
1319+
1320+ def test_decodeArrayOneItem (self ):
1321+ input = "[31337]"
1322+ ujson .decode (input )
1323+
1324+ def test_decodeBigValue (self ):
1325+ input = "9223372036854775807"
1326+ ujson .decode (input )
1327+
1328+ def test_decodeSmallValue (self ):
1329+ input = "-9223372036854775808"
1330+ ujson .decode (input )
1331+
1332+ def test_decodeTooBigValue (self ):
1333+ try :
1334+ input = "9223372036854775808"
1335+ ujson .decode (input )
1336+ except ValueError , e :
1337+ pass
1338+ else :
1339+ assert False , "expected ValueError"
1340+
1341+ def test_decodeTooSmallValue (self ):
1342+ try :
1343+ input = "-90223372036854775809"
1344+ ujson .decode (input )
1345+ except ValueError ,e :
1346+ pass
1347+ else :
1348+ assert False , "expected ValueError"
1349+
1350+ def test_decodeVeryTooBigValue (self ):
1351+ try :
1352+ input = "9223372036854775808"
1353+ ujson .decode (input )
1354+ except ValueError :
1355+ pass
1356+ else :
1357+ assert False , "expected ValueError"
1358+
1359+ def test_decodeVeryTooSmallValue (self ):
1360+ try :
1361+ input = "-90223372036854775809"
1362+ ujson .decode (input )
1363+ except ValueError :
1364+ pass
1365+ else :
1366+ assert False , "expected ValueError"
1367+
1368+ def test_decodeWithTrailingWhitespaces (self ):
1369+ input = "{}\n \t "
1370+ ujson .decode (input )
1371+
1372+ def test_decodeWithTrailingNonWhitespaces (self ):
1373+ try :
1374+ input = "{}\n \t a"
1375+ ujson .decode (input )
1376+ except ValueError :
1377+ pass
1378+ else :
1379+ assert False , "expected ValueError"
1380+
1381+ def test_decodeArrayWithBigInt (self ):
1382+ try :
1383+ ujson .loads ('[18446098363113800555]' )
1384+ except ValueError :
1385+ pass
1386+ else :
1387+ assert False , "expected ValueError"
1388+
1389+ def test_decodeArrayFaultyUnicode (self ):
1390+ try :
1391+ ujson .loads ('[18446098363113800555]' )
1392+ except ValueError :
1393+ pass
1394+ else :
1395+ assert False , "expected ValueError"
1396+
1397+
1398+ def test_decodeFloatingPointAdditionalTests (self ):
1399+ self .assertEquals (- 1.1234567893 , ujson .loads ("-1.1234567893" ))
1400+ self .assertEquals (- 1.234567893 , ujson .loads ("-1.234567893" ))
1401+ self .assertEquals (- 1.34567893 , ujson .loads ("-1.34567893" ))
1402+ self .assertEquals (- 1.4567893 , ujson .loads ("-1.4567893" ))
1403+ self .assertEquals (- 1.567893 , ujson .loads ("-1.567893" ))
1404+ self .assertEquals (- 1.67893 , ujson .loads ("-1.67893" ))
1405+ self .assertEquals (- 1.7893 , ujson .loads ("-1.7893" ))
1406+ self .assertEquals (- 1.893 , ujson .loads ("-1.893" ))
1407+ self .assertEquals (- 1.3 , ujson .loads ("-1.3" ))
1408+
1409+ self .assertEquals (1.1234567893 , ujson .loads ("1.1234567893" ))
1410+ self .assertEquals (1.234567893 , ujson .loads ("1.234567893" ))
1411+ self .assertEquals (1.34567893 , ujson .loads ("1.34567893" ))
1412+ self .assertEquals (1.4567893 , ujson .loads ("1.4567893" ))
1413+ self .assertEquals (1.567893 , ujson .loads ("1.567893" ))
1414+ self .assertEquals (1.67893 , ujson .loads ("1.67893" ))
1415+ self .assertEquals (1.7893 , ujson .loads ("1.7893" ))
1416+ self .assertEquals (1.893 , ujson .loads ("1.893" ))
1417+ self .assertEquals (1.3 , ujson .loads ("1.3" ))
1418+
1419+ def test_encodeBigSet (self ):
1420+ s = set ()
1421+ for x in xrange (0 , 100000 ):
1422+ s .add (x )
1423+ ujson .encode (s )
1424+
1425+ def test_encodeEmptySet (self ):
1426+ s = set ()
1427+ self .assertEquals ("[]" , ujson .encode (s ))
1428+
1429+ def test_encodeSet (self ):
1430+ s = set ([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ])
1431+ enc = ujson .encode (s )
1432+ dec = ujson .decode (enc )
1433+
1434+ for v in dec :
1435+ self .assertTrue (v in s )
1436+
11961437
11971438"""
11981439def test_decodeNumericIntFrcOverflow(self):
0 commit comments