Beispiel #1
0
 def test_decodeBigEscape(self):
     for x in range(10):
         if six.PY3:
             base = '\u00e5'.encode('utf-8')
             quote = "\"".encode()
         else:
             base = "\xc3\xa5"
             quote = "\""
         input = quote + (base * 1024 * 1024 * 2) + quote
         ujson.decode(input)
Beispiel #2
0
    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))
Beispiel #3
0
    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))
Beispiel #4
0
    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 "))
Beispiel #5
0
    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)
Beispiel #6
0
 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))
Beispiel #7
0
    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))
Beispiel #8
0
    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))
Beispiel #9
0
    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))
Beispiel #10
0
    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))
Beispiel #11
0
    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))
Beispiel #12
0
 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))
Beispiel #13
0
 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))
Beispiel #14
0
 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)
Beispiel #15
0
 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)
Beispiel #16
0
    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, {})
Beispiel #17
0
    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))
Beispiel #18
0
    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)
Beispiel #19
0
 def test_decodeNumberWith32bitSignBit(self):
     #Test that numbers that fit within 32 bits but would have the
     # sign bit set (2**31 <= x < 2**32) are decoded properly.
     docs = (
         '{"id": 3590016419}',
         '{"id": %s}' % 2**31,
         '{"id": %s}' % 2**32,
         '{"id": %s}' % ((2**32) - 1),
     )
     results = (3590016419, 2**31, 2**32, 2**32 - 1)
     for doc, result in zip(docs, results):
         self.assertEqual(ujson.decode(doc)['id'], result)
Beispiel #20
0
    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})
Beispiel #21
0
    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})
Beispiel #22
0
    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)
Beispiel #23
0
    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)
Beispiel #24
0
 def test_decimalDecodeTestPrecise(self):
     sut = {'a': 4.56}
     encoded = ujson.encode(sut)
     decoded = ujson.decode(encoded, precise_float=True)
     self.assertEqual(sut, decoded)
Beispiel #25
0
 def test_decimalDecodeTest(self):
     sut = {'a': 4.56}
     encoded = ujson.encode(sut)
     decoded = ujson.decode(encoded)
     self.assertAlmostEqual(sut[u'a'], decoded[u'a'])
Beispiel #26
0
 def test_encodeDecodeLongDecimal(self):
     sut = {'a': -528656961.4399388}
     encoded = ujson.dumps(sut, double_precision=15)
     ujson.decode(encoded)
Beispiel #27
0
 def test_decodeWithTrailingWhitespaces(self):
     input = "{}\n\t "
     ujson.decode(input)
Beispiel #28
0
 def test_decodeSmallValue(self):
     input = "-9223372036854775808"
     ujson.decode(input)
Beispiel #29
0
 def test_decodeBigValue(self):
     input = "9223372036854775807"
     ujson.decode(input)
Beispiel #30
0
 def test_decodeLongUnsignedValue(self):
     input = "18446744073709551615"
     ujson.decode(input)