Example #1
0
    def test_json_encoder_literals(self):
        self.encoder_test(None,  'null')

        self.encoder_test(True,  'true')
        self.encoder_test(False, 'false')

        self.encoder_test('foo', '"foo"')
        self.encoder_test('f"o"o', '"f\\"o\\"o"')
        self.encoder_test('foo\nbar', '"foo\\nbar"')
        self.encoder_test('\tf\\oo\n ', '"\\tf\\\\oo\\n "')

        self.encoder_test('\x00', '"\\u0000"')
        self.encoder_test('a\x19b', '"a\\u0019b"')
        self.encoder_test('a\x20b', '"a b"')

        self.encoder_test('\x00a\nbcعمان', '"\\u0000a\\nbc\\u0639\\u0645\\u0627\\u0646"')

        self.encoder_test('/\\"\n\r\t\b\f', '"/\\\\\\"\\n\\r\\t\\b\\f"')

        # HTML special chars are encoded to Unicode sequences by us, but not std json
        self.encoder_test('<a href="test?a&b">', r'"\u003ca href=\"test?a\u0026b\"\u003e"',
                          matches_default_encoder=False)

        # once a bug was introduced in the c version which passed all other tests but not
        # this one (for characted \u0638) - so need to keep this long UTF string as a
        # "more complicated" utf sample
        long_utf_string = "عالم " * 50
        assert std_dumps(long_utf_string, separators=(',',':')) == self.dumps(long_utf_string)

        self.encoder_test('a/b', '"a/b"')
Example #2
0
    def encoder_test(self, obj, expected, convertable_back = True, matches_default_encoder = True):
        """Tests that our dumps behaves exactly like the default json module
        Should not be used for cases where we don't like the default module behavior
        """
        if expected is None:
            expected = expected_b = {None}
        else:
            if isinstance(expected, str):
                expected = {expected}
            else:
                expected = set(expected)

        encoded = self.dumps(obj)
        assert encoded in expected, \
               'encoded {} not in expected {}'.format(encoded, expected)

        expected_b = {e.encode('ascii') for e in expected}
        encoded_b = self.dumpb(obj)
        assert encoded_b in expected_b

        if convertable_back:
            # test decode: not always possible, e.g. for Decimals or integers larger than Java-max
            assert std_loads(self.dumps(obj)) == obj

        if matches_default_encoder:
            # test compliance with standard encoder; use compact encoding (set
            #  separators) - ow lists are expected with extra spaces between items
            assert std_dumps(obj, separators=(',',':')) == self.dumps(obj)
Example #3
0
    def test_json_encoder_literals(self):
        self.encoder_test(None, 'null')

        self.encoder_test(True, 'true')
        self.encoder_test(False, 'false')

        self.encoder_test('foo', '"foo"')
        self.encoder_test('f"o"o', '"f\\"o\\"o"')
        self.encoder_test('foo\nbar', '"foo\\nbar"')
        self.encoder_test('\tf\\oo\n ', '"\\tf\\\\oo\\n "')

        self.encoder_test('\x00', '"\\u0000"')
        self.encoder_test('a\x19b', '"a\\u0019b"')
        self.encoder_test('a\x20b', '"a b"')

        self.encoder_test('\x00a\nbcعمان',
                          '"\\u0000a\\nbc\\u0639\\u0645\\u0627\\u0646"')

        self.encoder_test('/\\"\n\r\t\b\f', '"/\\\\\\"\\n\\r\\t\\b\\f"')

        # HTML special chars are encoded to Unicode sequences by us, but not std json
        self.encoder_test('<a href="test?a&b">',
                          r'"\u003ca href=\"test?a\u0026b\"\u003e"',
                          matches_default_encoder=False)

        # once a bug was introduced in the c version which passed all other tests but not
        # this one (for characted \u0638) - so need to keep this long UTF string as a
        # "more complicated" utf sample
        long_utf_string = "عالم " * 50
        assert std_dumps(long_utf_string,
                         separators=(',', ':')) == self.dumps(long_utf_string)

        self.encoder_test('a/b', '"a/b"')
Example #4
0
    def encoder_test(self,
                     obj,
                     expected,
                     convertable_back=True,
                     matches_default_encoder=True):
        """Tests that our dumps behaves exactly like the default json module
        Should not be used for cases where we don't like the default module behavior
        """
        if expected is None:
            expected = expected_b = {None}
        else:
            if isinstance(expected, str):
                expected = {expected}
            else:
                expected = set(expected)

        encoded = self.dumps(obj)
        assert encoded in expected, \
               'encoded {} not in expected {}'.format(encoded, expected)

        expected_b = {e.encode('ascii') for e in expected}
        encoded_b = self.dumpb(obj)
        assert encoded_b in expected_b

        if convertable_back:
            # test decode: not always possible, e.g. for Decimals or integers larger than Java-max
            assert std_loads(self.dumps(obj)) == obj

        if matches_default_encoder:
            # test compliance with standard encoder; use compact encoding (set
            #  separators) - ow lists are expected with extra spaces between items
            assert std_dumps(obj, separators=(',', ':')) == self.dumps(obj)
Example #5
0
 def test_json_integer_output(self):
     for _ in range(5000):
         x = random.randint(-9007199254740992, 9007199254740992)
         assert std_dumps(x) == self.dumps(x)
 def encode(self, obj):
     return std_dumps(obj)
Example #7
0
 def test_json_integer_output(self):
     for _ in range(5000):
         x = random.randint(-9007199254740992, 9007199254740992)
         assert std_dumps(x) == self.dumps(x)
Example #8
0
 def encode(self, obj):
     return std_dumps(obj)