def test_encodeBigEscape(self): for x in range(10): if six.PY3: base = '\u00e5'.encode('utf-8') else: base = "\xc3\xa5" input = base * 1024 * 1024 * 2 ujson.encode(input)
def test_doublePrecisionTest(self): input = 30.012345678901234 output = ujson.encode(input, double_precision=15) self.assertEqual(input, json.loads(output)) self.assertEqual(input, ujson.decode(output)) output = ujson.encode(input, double_precision=9) self.assertEqual(round(input, 9), json.loads(output)) self.assertEqual(round(input, 9), ujson.decode(output)) output = ujson.encode(input, double_precision=3) self.assertEqual(round(input, 3), json.loads(output)) self.assertEqual(round(input, 3), ujson.decode(output))
def test_encodeNullCharacter(self): input = "31337 \x00 1337" output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output)) input = "\x00" output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output)) self.assertEqual('" \\u0000\\r\\n "', ujson.dumps(" \u0000\r\n "))
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_encodeUnicode4BytesUTF8Highest(self): input = "\xf3\xbf\xbf\xbfTRAILINGNORMAL" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, json.loads(enc))
def test_encodeArrayInArray(self): input = [[[[]]]] output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_encodeUnicode4BytesUTF8(self): input = "\xf0\x91\x80\xb0TRAILINGNORMAL" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, json.loads(enc))
def test_encodeUnicodeSurrogatePair(self): input = "\xf0\x90\x8d\x86" enc = ujson.encode(input) dec = ujson.decode(enc) self.assertEqual(enc, json_unicode(input)) self.assertEqual(dec, json.loads(enc))
def test_encodeLongUnsignedConversion(self): input = 18446744073709551615 output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_encodeDatetimeConversion(self): ts = time.time() input = datetime.datetime.fromtimestamp(ts) output = ujson.encode(input) expected = calendar.timegm(input.utctimetuple()) self.assertEqual(int(expected), json.loads(output)) self.assertEqual(int(expected), ujson.decode(output))
def test_doubleLongDecimalIssue(self): sut = {'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_encodeToUTF8(self): input = b"\xe6\x97\xa5\xd1\x88" if six.PY3: input = input.decode('utf-8') enc = ujson.encode(input, ensure_ascii=False) dec = ujson.decode(enc) self.assertEqual(enc, json.dumps(input, ensure_ascii=False)) self.assertEqual(dec, json.loads(enc))
def test_doubleLongIssue(self): sut = {'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_encodeListLongConversion(self): input = [ 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807 ] output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(input, ujson.decode(output))
def test_object_default(self): # An object without toDict or __json__ defined should be serialized # as an empty dict. class ObjectTest: pass output = ujson.encode(ObjectTest()) dec = ujson.decode(output) self.assertEquals(dec, {})
def test_invalidDoublePrecision(self): input = 30.12345678901234567890 output = ujson.encode(input, double_precision=20) # should snap to the max, which is 15 self.assertEqual(round(input, 15), json.loads(output)) self.assertEqual(round(input, 15), ujson.decode(output)) output = ujson.encode(input, double_precision=-1) # also should snap to the max, which is 15 self.assertEqual(round(input, 15), json.loads(output)) self.assertEqual(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 test_encodeLongNegConversion(self): input = -9223372036854775808 output = ujson.encode(input) json.loads(output) ujson.decode(output) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_encodeDateConversion(self): ts = time.time() input = datetime.date.fromtimestamp(ts) output = ujson.encode(input) tup = (input.year, input.month, input.day, 0, 0, 0) expected = calendar.timegm(tup) self.assertEqual(int(expected), json.loads(output)) self.assertEqual(int(expected), ujson.decode(output))
def test_encodeDictWithUnicodeKeys(self): input = { "key1": "value1", "key1": "value1", "key1": "value1", "key1": "value1", "key1": "value1", "key1": "value1" } ujson.encode(input) input = { "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1", "بن": "value1" } ujson.encode(input)
def test_object_with_complex_json(self): # If __json__ returns a string, then that string # will be used as a raw JSON snippet in the object. obj = {u'foo': [u'bar', u'baz']} class JSONTest: def __json__(self): return ujson.encode(obj) d = {u'key': JSONTest()} output = ujson.encode(d) dec = ujson.decode(output) self.assertEquals(dec, {u'key': obj})
def test_object_with_json_unicode(self): # If __json__ returns a string, then that string # will be used as a raw JSON snippet in the object. output_text = u'this is the correct output' class JSONTest: def __json__(self): return u'"' + output_text + u'"' d = {u'key': JSONTest()} output = ujson.encode(d) dec = ujson.decode(output) self.assertEquals(dec, {u'key': output_text})
def test_toDict(self): d = {"key": 31337} class DictTest: def toDict(self): return d def __json__(self): return '"json defined"' # Fallback and shouldn't be called. o = DictTest() output = ujson.encode(o) dec = ujson.decode(output) self.assertEqual(dec, d)
def test_encodeWithTimezone(self): start_timestamp = 1383647400 a = datetime.datetime.utcfromtimestamp(start_timestamp) a = a.replace(tzinfo=pytz.utc) b = a.astimezone(pytz.timezone('America/New_York')) c = b.astimezone(pytz.utc) d = {'a': a, 'b': b, 'c': c} json_string = ujson.encode(d) json_dict = ujson.decode(json_string) self.assertEqual(json_dict.get('a'), start_timestamp) self.assertEqual(json_dict.get('b'), start_timestamp) self.assertEqual(json_dict.get('c'), start_timestamp)
def test_decimalDecodeTestPrecise(self): sut = {'a': 4.56} encoded = ujson.encode(sut) decoded = ujson.decode(encoded, precise_float=True) self.assertEqual(sut, decoded)
def test_encodeFalseConversion(self): input = False output = ujson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ujson.decode(output))
def test_decimalDecodeTest(self): sut = {'a': 4.56} encoded = ujson.encode(sut) decoded = ujson.decode(encoded) self.assertAlmostEqual(sut[u'a'], decoded[u'a'])
def test_encodeEmptySet(self): s = set() self.assertEqual("[]", ujson.encode(s))
def test_encodeBigSet(self): s = set() for x in range(0, 100000): s.add(x) ujson.encode(s)
def __json__(self): return ujson.encode(obj)
def helper(expected_output, **encode_kwargs): output = ujson.encode(input, **encode_kwargs) self.assertEqual(output, expected_output) if encode_kwargs.get('escape_forward_slashes', True): self.assertEqual(input, json.loads(output)) self.assertEqual(input, ujson.decode(output))