Skip to content

ENH: update bundled ujson to latest v1.33 #3946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/io/tests/test_json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,21 @@ def test_frame_from_json_bad_data(self):
# too few indices
json = StringIO('{"columns":["A","B"],'
'"index":["2","3"],'
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}"')
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}')
self.assertRaises(ValueError, read_json, json,
orient="split")

# too many columns
json = StringIO('{"columns":["A","B","C"],'
'"index":["1","2","3"],'
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}"')
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}')
self.assertRaises(AssertionError, read_json, json,
orient="split")

# bad key
json = StringIO('{"badkey":["A","B"],'
'"index":["2","3"],'
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}"')
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}')
self.assertRaises(TypeError, read_json, json,
orient="split")

Expand Down
253 changes: 247 additions & 6 deletions pandas/io/tests/test_json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import calendar
import StringIO
import re
import random
import decimal
from functools import partial
import pandas.util.py3compat as py3compat

Expand All @@ -36,6 +38,72 @@ def _skip_if_python_ver(skip_major, skip_minor=None):
else partial(json.dumps, encoding="utf-8"))

class UltraJSONTests(TestCase):

def test_encodeDecimal(self):
sut = decimal.Decimal("1337.1337")
encoded = ujson.encode(sut, double_precision=100)
decoded = ujson.decode(encoded)
self.assertEquals(decoded, 1337.1337)

def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t </script> &"
not_html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t <\\/script> &"'
html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t \\u003c\\/script\\u003e \\u0026"'

def helper(expected_output, **encode_kwargs):
output = ujson.encode(input, **encode_kwargs)
self.assertEquals(input, json.loads(output))
self.assertEquals(output, expected_output)
self.assertEquals(input, ujson.decode(output))

# Default behavior assumes encode_html_chars=False.
helper(not_html_encoded, ensure_ascii=True)
helper(not_html_encoded, ensure_ascii=False)

# Make sure explicit encode_html_chars=False works.
helper(not_html_encoded, ensure_ascii=True, encode_html_chars=False)
helper(not_html_encoded, ensure_ascii=False, encode_html_chars=False)

# Make sure explicit encode_html_chars=True does the encoding.
helper(html_encoded, ensure_ascii=True, encode_html_chars=True)
helper(html_encoded, ensure_ascii=False, encode_html_chars=True)

def test_doubleLongIssue(self):
sut = {u'a': -4342969734183514}
encoded = json.dumps(sut)
decoded = json.loads(encoded)
self.assertEqual(sut, decoded)
encoded = ujson.encode(sut, double_precision=100)
decoded = ujson.decode(encoded)
self.assertEqual(sut, decoded)

def test_doubleLongDecimalIssue(self):
sut = {u'a': -12345678901234.56789012}
encoded = json.dumps(sut)
decoded = json.loads(encoded)
self.assertEqual(sut, decoded)
encoded = ujson.encode(sut, double_precision=100)
decoded = ujson.decode(encoded)
self.assertEqual(sut, decoded)


def test_encodeDecodeLongDecimal(self):
sut = {u'a': -528656961.4399388}
encoded = ujson.dumps(sut, double_precision=15)
ujson.decode(encoded)

def test_decimalDecodeTest(self):
sut = {u'a': 4.56}
encoded = ujson.encode(sut)
decoded = ujson.decode(encoded)
self.assertNotEqual(sut, decoded)

def test_decimalDecodeTestPrecise(self):
sut = {u'a': 4.56}
encoded = ujson.encode(sut)
decoded = ujson.decode(encoded, precise_float=True)
self.assertEqual(sut, decoded)

def test_encodeDictWithUnicodeKeys(self):
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" }
output = ujson.encode(input)
Expand All @@ -59,6 +127,7 @@ def test_encodeWithDecimal(self):
def test_encodeDoubleNegConversion(self):
input = -math.pi
output = ujson.encode(input)

self.assertEquals(round(input, 5), round(json.loads(output), 5))
self.assertEquals(round(input, 5), round(ujson.decode(output), 5))

Expand Down Expand Up @@ -93,10 +162,6 @@ def test_doublePrecisionTest(self):
self.assertEquals(round(input, 3), json.loads(output))
self.assertEquals(round(input, 3), ujson.decode(output))

output = ujson.encode(input)
self.assertEquals(round(input, 5), json.loads(output))
self.assertEquals(round(input, 5), ujson.decode(output))

def test_invalidDoublePrecision(self):
input = 30.12345678901234567890
output = ujson.encode(input, double_precision = 20)
Expand Down Expand Up @@ -373,6 +438,15 @@ def test_decodeBrokenArrayEnd(self):
return
assert False, "Wrong exception"

def test_decodeArrayDepthTooBig(self):
input = '[' * (1024 * 1024)
try:
ujson.decode(input)
assert False, "Expected exception!"
except(ValueError):
return
assert False, "Wrong exception"

def test_decodeBrokenObjectEnd(self):
input = "}"
try:
Expand All @@ -382,6 +456,15 @@ def test_decodeBrokenObjectEnd(self):
return
assert False, "Wrong exception"

def test_decodeObjectDepthTooBig(self):
input = '{' * (1024 * 1024)
try:
ujson.decode(input)
assert False, "Expected exception!"
except(ValueError):
return
assert False, "Wrong exception"

def test_decodeStringUnterminated(self):
input = "\"TESTING"
try:
Expand Down Expand Up @@ -567,7 +650,7 @@ def test_numericIntFrcExp(self):
self.assertAlmostEqual(output, json.loads(input))

def test_decodeNumericIntExpEPLUS(self):
input = "1337E+40"
input = "1337E+9"
output = ujson.decode(input)
self.assertAlmostEqual(output, json.loads(input))

Expand Down Expand Up @@ -1192,7 +1275,165 @@ def test_datetimeindex(self):
decoded = Series(ujson.decode(ujson.encode(ts)))
idx_values = decoded.index.values.astype(np.int64)
decoded.index = DatetimeIndex(idx_values)
tm.assert_series_equal(np.round(ts, 5), decoded)
tm.assert_series_equal(ts, decoded)

def test_decodeArrayTrailingCommaFail(self):
input = "[31337,]"
try:
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeArrayLeadingCommaFail(self):
input = "[,31337]"
try:
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeArrayOnlyCommaFail(self):
input = "[,]"
try:
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeArrayUnmatchedBracketFail(self):
input = "[]]"
try:
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeArrayEmpty(self):
input = "[]"
ujson.decode(input)

def test_decodeArrayOneItem(self):
input = "[31337]"
ujson.decode(input)

def test_decodeBigValue(self):
input = "9223372036854775807"
ujson.decode(input)

def test_decodeSmallValue(self):
input = "-9223372036854775808"
ujson.decode(input)

def test_decodeTooBigValue(self):
try:
input = "9223372036854775808"
ujson.decode(input)
except ValueError, e:
pass
else:
assert False, "expected ValueError"

def test_decodeTooSmallValue(self):
try:
input = "-90223372036854775809"
ujson.decode(input)
except ValueError,e:
pass
else:
assert False, "expected ValueError"

def test_decodeVeryTooBigValue(self):
try:
input = "9223372036854775808"
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeVeryTooSmallValue(self):
try:
input = "-90223372036854775809"
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeWithTrailingWhitespaces(self):
input = "{}\n\t "
ujson.decode(input)

def test_decodeWithTrailingNonWhitespaces(self):
try:
input = "{}\n\t a"
ujson.decode(input)
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeArrayWithBigInt(self):
try:
ujson.loads('[18446098363113800555]')
except ValueError:
pass
else:
assert False, "expected ValueError"

def test_decodeArrayFaultyUnicode(self):
try:
ujson.loads('[18446098363113800555]')
except ValueError:
pass
else:
assert False, "expected ValueError"


def test_decodeFloatingPointAdditionalTests(self):
self.assertEquals(-1.1234567893, ujson.loads("-1.1234567893"))
self.assertEquals(-1.234567893, ujson.loads("-1.234567893"))
self.assertEquals(-1.34567893, ujson.loads("-1.34567893"))
self.assertEquals(-1.4567893, ujson.loads("-1.4567893"))
self.assertEquals(-1.567893, ujson.loads("-1.567893"))
self.assertEquals(-1.67893, ujson.loads("-1.67893"))
self.assertEquals(-1.7893, ujson.loads("-1.7893"))
self.assertEquals(-1.893, ujson.loads("-1.893"))
self.assertEquals(-1.3, ujson.loads("-1.3"))

self.assertEquals(1.1234567893, ujson.loads("1.1234567893"))
self.assertEquals(1.234567893, ujson.loads("1.234567893"))
self.assertEquals(1.34567893, ujson.loads("1.34567893"))
self.assertEquals(1.4567893, ujson.loads("1.4567893"))
self.assertEquals(1.567893, ujson.loads("1.567893"))
self.assertEquals(1.67893, ujson.loads("1.67893"))
self.assertEquals(1.7893, ujson.loads("1.7893"))
self.assertEquals(1.893, ujson.loads("1.893"))
self.assertEquals(1.3, ujson.loads("1.3"))

def test_encodeBigSet(self):
s = set()
for x in xrange(0, 100000):
s.add(x)
ujson.encode(s)

def test_encodeEmptySet(self):
s = set()
self.assertEquals("[]", ujson.encode(s))

def test_encodeSet(self):
s = set([1,2,3,4,5,6,7,8,9])
enc = ujson.encode(s)
dec = ujson.decode(enc)

for v in dec:
self.assertTrue(v in s)


"""
def test_decodeNumericIntFrcOverflow(self):
Expand Down
Loading