def test_encodeNumericOverflow(self): try: ujson.encode(12839128391289382193812939) except OverflowError: pass else: assert False, "expected OverflowError"
def testDataFrame(self): df = DataFrame([[1, 2, 3], [4, 5, 6]], index=["a", "b"], columns=["x", "y", "z"]) # column indexed outp = DataFrame(ujson.decode(ujson.encode(df))) self.assertTrue((df == outp).values.all()) assert_array_equal(df.columns, outp.columns) assert_array_equal(df.index, outp.index) outp = DataFrame(**ujson.decode(ujson.encode(df, orient="split"))) self.assertTrue((df == outp).values.all()) assert_array_equal(df.columns, outp.columns) assert_array_equal(df.index, outp.index) outp = DataFrame(ujson.decode(ujson.encode(df, orient="records"))) outp.index = df.index self.assertTrue((df == outp).values.all()) assert_array_equal(df.columns, outp.columns) outp = DataFrame(ujson.decode(ujson.encode(df, orient="values"))) outp.index = df.index self.assertTrue((df.values == outp.values).all()) outp = DataFrame(ujson.decode(ujson.encode(df, orient="index"))) self.assertTrue((df.transpose() == outp).values.all()) assert_array_equal(df.transpose().columns, outp.columns) assert_array_equal(df.transpose().index, outp.index)
def testDataFrame(self): df = DataFrame([[1,2,3], [4,5,6]], index=['a', 'b'], columns=['x', 'y', 'z']) # column indexed outp = DataFrame(ujson.decode(ujson.encode(df))) self.assertTrue((df == outp).values.all()) assert_array_equal(df.columns, outp.columns) assert_array_equal(df.index, outp.index) dec = _clean_dict(ujson.decode(ujson.encode(df, orient="split"))) outp = DataFrame(**dec) self.assertTrue((df == outp).values.all()) assert_array_equal(df.columns, outp.columns) assert_array_equal(df.index, outp.index) outp = DataFrame(ujson.decode(ujson.encode(df, orient="records"))) outp.index = df.index self.assertTrue((df == outp).values.all()) assert_array_equal(df.columns, outp.columns) outp = DataFrame(ujson.decode(ujson.encode(df, orient="values"))) outp.index = df.index self.assertTrue((df.values == outp.values).all()) outp = DataFrame(ujson.decode(ujson.encode(df, orient="index"))) self.assertTrue((df.transpose() == outp).values.all()) assert_array_equal(df.transpose().columns, outp.columns) assert_array_equal(df.transpose().index, outp.index)
def testIndex(self): i = Index([23, 45, 18, 98, 43, 11], name="index") # column indexed outp = Index(ujson.decode(ujson.encode(i))) assert_array_equal(i, outp) outp = Index(ujson.decode(ujson.encode(i), numpy=True)) assert_array_equal(i, outp) outp = Index(**ujson.decode(ujson.encode(i, orient="split"))) assert_array_equal(i, outp) self.assertTrue(i.name == outp.name) outp = Index(**ujson.decode(ujson.encode(i, orient="split"), numpy=True)) assert_array_equal(i, outp) self.assertTrue(i.name == outp.name) outp = Index(ujson.decode(ujson.encode(i, orient="values"))) assert_array_equal(i, outp) outp = Index(ujson.decode(ujson.encode(i, orient="values"), numpy=True)) assert_array_equal(i, outp) outp = Index(ujson.decode(ujson.encode(i, orient="records"))) assert_array_equal(i, outp) outp = Index(ujson.decode(ujson.encode(i, orient="records"), numpy=True)) assert_array_equal(i, outp) outp = Index(ujson.decode(ujson.encode(i, orient="index"))) assert_array_equal(i, outp) outp = Index(ujson.decode(ujson.encode(i, orient="index"), numpy=True)) assert_array_equal(i, outp)
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) input = { u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1" } output = ujson.encode(input) pass
def testFloatMax(self): num = np.float(np.finfo(np.float).max/10) assert_approx_equal(np.float(ujson.decode(ujson.encode(num))), num, 15) num = np.float32(np.finfo(np.float32).max/10) assert_approx_equal(np.float32(ujson.decode(ujson.encode(num))), num, 15) num = np.float64(np.finfo(np.float64).max/10) assert_approx_equal(np.float64(ujson.decode(ujson.encode(num))), num, 15)
def testFloat(self): num = np.float(256.2013) self.assertEqual(np.float(ujson.decode(ujson.encode(num))), num) num = np.float32(256.2013) self.assertEqual(np.float32(ujson.decode(ujson.encode(num))), num) num = np.float64(256.2013) self.assertEqual(np.float64(ujson.decode(ujson.encode(num))), num)
def testInt(self): num = np.int(2562010) self.assertEqual(np.int(ujson.decode(ujson.encode(num))), num) num = np.int8(127) self.assertEqual(np.int8(ujson.decode(ujson.encode(num))), num) num = np.int16(2562010) self.assertEqual(np.int16(ujson.decode(ujson.encode(num))), num) num = np.int32(2562010) self.assertEqual(np.int32(ujson.decode(ujson.encode(num))), num) num = np.int64(2562010) self.assertEqual(np.int64(ujson.decode(ujson.encode(num))), num) num = np.uint8(255) self.assertEqual(np.uint8(ujson.decode(ujson.encode(num))), num) num = np.uint16(2562010) self.assertEqual(np.uint16(ujson.decode(ujson.encode(num))), num) num = np.uint32(2562010) self.assertEqual(np.uint32(ujson.decode(ujson.encode(num))), num) num = np.uint64(2562010) self.assertEqual(np.uint64(ujson.decode(ujson.encode(num))), num)
def testIntMax(self): num = np.int(np.iinfo(np.int).max) self.assertEqual(np.int(ujson.decode(ujson.encode(num))), num) num = np.int8(np.iinfo(np.int8).max) self.assertEqual(np.int8(ujson.decode(ujson.encode(num))), num) num = np.int16(np.iinfo(np.int16).max) self.assertEqual(np.int16(ujson.decode(ujson.encode(num))), num) num = np.int32(np.iinfo(np.int32).max) self.assertEqual(np.int32(ujson.decode(ujson.encode(num))), num) num = np.uint8(np.iinfo(np.uint8).max) self.assertEqual(np.uint8(ujson.decode(ujson.encode(num))), num) num = np.uint16(np.iinfo(np.uint16).max) self.assertEqual(np.uint16(ujson.decode(ujson.encode(num))), num) num = np.uint32(np.iinfo(np.uint32).max) self.assertEqual(np.uint32(ujson.decode(ujson.encode(num))), num) if platform.architecture()[0] != '32bit': num = np.int64(np.iinfo(np.int64).max) self.assertEqual(np.int64(ujson.decode(ujson.encode(num))), num) # uint64 max will always overflow as it's encoded to signed num = np.uint64(np.iinfo(np.int64).max) self.assertEqual(np.uint64(ujson.decode(ujson.encode(num))), num)
def testFloatArray(self): arr = np.arange(12.5, 185.72, 1.7322, dtype=np.float) dtypes = (np.float, np.float32, np.float64) for dtype in dtypes: inpt = arr.astype(dtype) outp = np.array(ujson.decode(ujson.encode(inpt, double_precision=15)), dtype=dtype) assert_array_almost_equal_nulp(inpt, outp) inpt = np.arange(1.5, 21.5, 0.2, dtype=np.float16) outp = np.array(ujson.decode(ujson.encode(inpt)), dtype=np.float16) assert_array_almost_equal_nulp(inpt, outp)
def test_encodeNumericOverflowNested(self): for n in xrange(0, 100): class Nested: x = 12839128391289382193812939 nested = Nested() try: ujson.encode(nested) except OverflowError: pass else: assert False, "expected OverflowError"
def test_datetimeindex(self): from pandas.tseries.index import date_range, DatetimeIndex rng = date_range('1/1/2000', periods=20) encoded = ujson.encode(rng) decoded = DatetimeIndex(np.array(ujson.decode(encoded))) self.assert_(rng.equals(decoded)) ts = Series(np.random.randn(len(rng)), index=rng) 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)
def test_encodeNullCharacter(self): input = "31337 \x00 1337" output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) input = "\x00" output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) self.assertEquals('" \\u0000\\r\\n "', ujson.dumps(u" \u0000\r\n ")) pass
def test_encodeUnicodeSurrogatePair(self): input = "\xf0\x90\x8d\x86" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEquals(enc, json.dumps(input, encoding="utf-8")) self.assertEquals(dec, json.loads(enc))
def test_encodeToUTF8(self): _skip_if_python_ver(2, 5) input = "\xe6\x97\xa5\xd1\x88" enc = ujson.encode(input, ensure_ascii=False) dec = ujson.decode(enc) self.assertEquals(enc, json_unicode(input, ensure_ascii=False)) self.assertEquals(dec, json.loads(enc))
def test_encodeFalseConversion(self): input = False output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) pass
def test_encodeDictConversion(self): input = { "k1": 1, "k2": 2, "k3": 3, "k4": 4 } output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(input, ujson.decode(output)) self.assertEquals(input, ujson.decode(output)) pass
def test_encodeListConversion(self): input = [ 1, 2, 3, 4 ] output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(input, ujson.decode(output)) assert_array_equal(np.array(input), ujson.decode(output, numpy=True)) pass
def test_encodeIntNegConversion(self): input = -31337 output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) pass
def test_encodeStringConversion(self): input = "A string \\ / \b \f \n \r \t" output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, '"A string \\\\ \\/ \\b \\f \\n \\r \\t"') self.assertEquals(input, ujson.decode(output)) pass
def test_encodeUnicode4BytesUTF8Fail(self): input = "\xfd\xbf\xbf\xbf\xbf\xbf" try: enc = ujson.encode(input) assert False, "Expected exception" except OverflowError: pass
def test_invalidDoublePrecision(self): input = 30.12345678901234567890 output = ujson.encode(input, double_precision = 20) # should snap to the max, which is 15 self.assertEquals(round(input, 15), json.loads(output)) self.assertEquals(round(input, 15), ujson.decode(output)) output = ujson.encode(input, double_precision = -1) # also should snap to the max, which is 15 self.assertEquals(round(input, 15), json.loads(output)) self.assertEquals(round(input, 15), ujson.decode(output)) # will throw typeError self.assertRaises(TypeError, ujson.encode, input, double_precision = '9') # will throw typeError self.assertRaises(TypeError, ujson.encode, input, double_precision = None)
def testIntArray(self): arr = np.arange(100, dtype=np.int) dtypes = (np.int, np.int8, np.int16, np.int32, np.int64, np.uint, np.uint8, np.uint16, np.uint32, np.uint64) for dtype in dtypes: inpt = arr.astype(dtype) outp = np.array(ujson.decode(ujson.encode(inpt)), dtype=dtype) assert_array_equal(inpt, outp)
def test_encodeLongConversion(self): input = 9223372036854775807 output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) pass
def test_encodeUnicode4BytesUTF8(self): input = "\xf0\x91\x80\xb0TRAILINGNORMAL" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEquals(enc, json.dumps(input, encoding="utf-8")) self.assertEquals(dec, json.loads(enc))
def test_encodeArrayOfDoubles(self): input = [ 31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10 output = ujson.encode(input) self.assertEquals(input, json.loads(output)) #self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) assert_array_equal(np.array(input), ujson.decode(output, numpy=True))
def testFloatArray(self): arr = np.arange(12.5, 185.72, 1.7322, dtype=np.float) dtypes = (np.float, np.float32, np.float64) for dtype in dtypes: inpt = arr.astype(dtype) outp = np.array(ujson.decode(ujson.encode(inpt, double_precision=15)), dtype=dtype) assert_array_almost_equal_nulp(inpt, outp)
def test_doublePrecisionTest(self): input = 30.012345678901234 output = ujson.encode(input, double_precision = 15) self.assertEquals(input, json.loads(output)) self.assertEquals(input, ujson.decode(output)) output = ujson.encode(input, double_precision = 9) self.assertEquals(round(input, 9), json.loads(output)) self.assertEquals(round(input, 9), ujson.decode(output)) output = ujson.encode(input, double_precision = 3) 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_encodeBigEscape(self): for x in xrange(10): if py3compat.PY3: base = '\u00e5'.encode('utf-8') else: base = "\xc3\xa5" input = base * 1024 * 1024 * 2 output = ujson.encode(input)
def test_encodeUnicode4BytesUTF8Fail(self): _skip_if_python_ver(3) input = "\xfd\xbf\xbf\xbf\xbf\xbf" try: enc = ujson.encode(input) assert False, "Expected exception" except OverflowError: pass
def test_encodeDatetimeConversion(self): ts = time.time() input = datetime.datetime.fromtimestamp(ts) output = ujson.encode(input) expected = calendar.timegm(input.utctimetuple()) self.assertEquals(int(expected), json.loads(output)) self.assertEquals(int(expected), ujson.decode(output)) pass
def test_encodeArrayOfNestedArrays(self): input = [[[[]]]] * 20 output = ujson.encode(input) self.assertEquals(input, json.loads(output)) #self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) input = np.array(input) assert_array_equal(input, ujson.decode(output, numpy=True, dtype=input.dtype))
def test_encodeUnicodeSurrogatePair(self): _skip_if_python_ver(2, 5) _skip_if_python_ver(2, 6) input = "\xf0\x90\x8d\x86" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEquals(enc, json_unicode(input)) self.assertEquals(dec, json.loads(enc))
def test_encodeUnicode4BytesUTF8(self): _skip_if_python_ver(2, 5) _skip_if_python_ver(2, 6) input = "\xf0\x91\x80\xb0TRAILINGNORMAL" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEquals(enc, json_unicode(input)) self.assertEquals(dec, json.loads(enc))
def test_encodeArrayInArray(self): input = [[[[]]]] output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(output, json.dumps(input)) self.assertEquals(input, ujson.decode(output)) assert_array_equal(np.array(input), ujson.decode(output, numpy=True)) pass
def test_datetime_nanosecond_unit(self): from datetime import datetime from pandas.lib import Timestamp val = datetime.now() stamp = Timestamp(val) roundtrip = ujson.decode(ujson.encode(val)) self.assert_(roundtrip == stamp.value)
def test_encodeListLongConversion(self): input = [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807 ] output = ujson.encode(input) self.assertEquals(input, json.loads(output)) self.assertEquals(input, ujson.decode(output)) assert_array_equal(np.array(input), ujson.decode(output, numpy=True, dtype=np.int64)) pass