Ejemplo n.º 1
0
 def testEncodeStringEscapes(self):
     # FIXME: How do you account for ordering?
     #  obj = chjson.encode({"a\"b": 'zz', "22": (1,2),})
     # because it's one of these:
     #  self.assertEqual('{"a\\"b": "zz", "22": [1, 2]}', obj)
     #  self.assertEqual('{"22": [1, 2], "a\\"b": "zz"}', obj)
     obj = chjson.encode({"a\"b": 'zz'})
     self.assertEqual('{"a\\"b": "zz"}', obj)
     obj = chjson.encode({"22": (1,2),})
     self.assertEqual('{"22": [1, 2]}', obj)
Ejemplo n.º 2
0
 def testReadWriteCopies(self):
     orig_obj = {'a':' " '}
     json_str = chjson.encode(orig_obj)
     copy_obj = chjson.decode(json_str)
     self.assertEqual(orig_obj, copy_obj)
     self.assertEqual(True, orig_obj == copy_obj)
     self.assertEqual(False, orig_obj is copy_obj)
Ejemplo n.º 3
0
 def testStringEncoding(self):
     s = chjson.encode([1, 2, 3])
     if sys.version_info[0] >= 3:
         encoded = "[1,2,3]"
     else:
         encoded = unicode("[1,2,3]", "utf-8")
     self.assertEqual(encoded, _removeWhitespace(s))
Ejemplo n.º 4
0
 def testWriteEscapedHexCharacter(self):
     s = chjson.encode(u'\u1001')
     if sys.version_info[0] >= 3:
         self.assertEqual(r'"ခ"', _removeWhitespace(s))
     else:
         #self.assertEqual(r'"\u1001"', _removeWhitespace(s))
         self.assertEqual(r'u"\u1001"', _removeWhitespace(s))
Ejemplo n.º 5
0
 def testEncodeSolidus_02(self):
     obj = chjson.encode("{'string': 'hello\/goodbye'}")
     # NOTE: This might look wrong and you might think it should be: 'hello\\/goodbye'
     #       But [lb]'s understanding of the spec. is that \/ is for
     #       decoding: it's stripped on input, so if \/ is being encoded,
     #       it's a backslash which we need to escape, followed by a
     #       solidus which also needs to be escaped.
     self.assertEqual('"{\'string\': \'hello\\\\\\/goodbye\'}"', obj)
Ejemplo n.º 6
0
 def testWriteSmallObject(self):
     s = chjson.encode({ "name" : "Patrick", "age": 44 })
     # HA! This is a hack.
     self.assertTrue(
         _removeWhitespace(s) in [
             '{"name":"Patrick","age":44}',
             '{"age":44,"name":"Patrick"}',
         ]
     )
Ejemplo n.º 7
0
 def testWriteEscapedFormfeed(self):
     if sys.version_info[0] >= 3:
         # Hrmm. Return gets interrupted as KeyboardInterrupt:
         #    File "test_chjson.py", line 111, in testWriteEscapedFormfeed
         #      s = chjson.encode("\f")
         #  KeyboardInterrupt
         pass
     else:
         s = chjson.encode("\f")
         self.assertEqual(r'"\f"', _removeWhitespace(s))
Ejemplo n.º 8
0
 def app_cfg_dump(self):
     try:
         with open(self.cfg_path, 'w') as f_out:
             #f_out.write(json.dumps(self.cfg_data))
             f_out.write(chjson.encode(self.cfg_data))
     except Exception as err:
         warning('Failed: Could not write app config to: %s [%s]' % (
             self.cfg_path,
             str(err),
         ))
Ejemplo n.º 9
0
 def testWriteComplexArray(self):
     obj = [{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None},
            "used","abused","confused",
            1,2,[3,4,5]]
     # HA! This is a hack: Programmatically generate the list of
     # acceptable answers, since order is not predictable.
     kvals = [
         '"age":44',
         '"Female?":false',
         '"name":"Patrick"',
         '"Employed?":true',
         '"grandchildren":null',
     ]
     acceptable_answers = set([
         ('[{%s},"used","abused","confused",1,2,[3,4,5]]' % ','.join(x))
         for x in itertools.permutations(kvals)
     ])
     self.assertTrue(_removeWhitespace(chjson.encode(obj)) in acceptable_answers)
Ejemplo n.º 10
0
    def testWriteLongUnicode(self):
        # This test causes a buffer overrun in cjson 1.0.5, on UCS4 builds.
        # The string length is only resized for wide unicode characters if
        # there is less than 12 bytes of space left. Padding with
        # narrow-but-escaped characters prevents string resizing.
        # Note that u'\U0001D11E\u1234' also breaks, but sometimes goes
        # undetected.
        s = chjson.encode(u'\U0001D11E\U0001D11E\U0001D11E\U0001D11E'
                         u'\u1234\u1234\u1234\u1234\u1234\u1234')
        if sys.version_info[0] >= 3:
            # Wha?
# FIXME: This has got to be wrong.......... or is this just unicode output?
            self.assertEqual(
                '"𝄞𝄞𝄞𝄞ሴሴሴሴሴሴ"'
                , s
            )
        else:
            #self.assertEqual(r'"\U0001d11e\U0001d11e\U0001d11e\U0001d11e'
            #                 r'\u1234\u1234\u1234\u1234\u1234\u1234"', s)
            self.assertEqual(r'u"\U0001d11e\U0001d11e\U0001d11e\U0001d11e'
                             r'\u1234\u1234\u1234\u1234\u1234\u1234"', s)
Ejemplo n.º 11
0
 def testWriteEmptyObject(self):
     s = chjson.encode({})
     self.assertEqual("{}", _removeWhitespace(s))
Ejemplo n.º 12
0
 def testWriteLong(self):
     self.assertEqual("12345678901234567890", chjson.encode(12345678901234567890))
Ejemplo n.º 13
0
 def testWriteEscapedReverseSolidus(self):
     s = chjson.encode("\\")
     self.assertEqual(r'"\\"', _removeWhitespace(s))
Ejemplo n.º 14
0
 def testWriteEscapedBackspace(self):
     s = chjson.encode("\b")
     self.assertEqual(r'"\b"', _removeWhitespace(s))
Ejemplo n.º 15
0
 def testWriteStringValue(self):
     s = chjson.encode({ "name" : "Patrick" })
     self.assertEqual('{"name":"Patrick"}', _removeWhitespace(s))
Ejemplo n.º 16
0
 def testWriteNonEscapedSolidus(self):
     s = chjson.encode(r'/')
     self.assertEqual('"\\/"', _removeWhitespace(s))
Ejemplo n.º 17
0
 def testWriteNull(self):
     self.assertEqual("null", _removeWhitespace(chjson.encode(None)))
Ejemplo n.º 18
0
 def testEncodeUnicodeStringLeader(self):
     obj = chjson.encode([u'xx','yy'])
     self.assertEqual('["xx", "yy"]', obj)
Ejemplo n.º 19
0
 def testWriteEscapedNewline(self):
     s = chjson.encode("\n")
     self.assertEqual(r'"\n"', _removeWhitespace(s))
Ejemplo n.º 20
0
 def testWriteEscapedCarriageReturn(self):
     s = chjson.encode("\r")
     self.assertEqual(r'"\r"', _removeWhitespace(s))
Ejemplo n.º 21
0
 def testWriteSmallArray(self):
     self.assertEqual('[1,2,3,4]', _removeWhitespace(chjson.encode([1, 2, 3, 4])))
Ejemplo n.º 22
0
 def testWriteEmptyArray(self):
     self.assertEqual("[]", _removeWhitespace(chjson.encode([])))
Ejemplo n.º 23
0
 def testObjectBasicString_04(self):
     obj = chjson.encode("\f")
     self.assertEqual('"\\f"', obj)
Ejemplo n.º 24
0
 def testWriteArrayOfSymbolsFromTuple(self):
     self.assertEqual("[true,false,null]", _removeWhitespace(chjson.encode((True, False, None))))
Ejemplo n.º 25
0
 def testWriteFloat(self):
     n = 3.44556677
     self.assertEqual(repr(n), _removeWhitespace(chjson.encode(n)))
Ejemplo n.º 26
0
 def testWriteEscapedHorizontalTab(self):
     s = chjson.encode("\t")
     self.assertEqual(r'"\t"', _removeWhitespace(s))
Ejemplo n.º 27
0
 def testEncodeSolidus_01(self):
     obj = chjson.encode("{'string': 'hello/goodbye'}")
     self.assertEqual('"{\'string\': \'hello\\/goodbye\'}"', obj)
Ejemplo n.º 28
0
 def testWriteTrue(self):
     self.assertEqual("true", _removeWhitespace(chjson.encode(True)))
Ejemplo n.º 29
0
 def testWriteEscapedQuotationMark(self):
     s = chjson.encode(r'"')
     self.assertEqual(r'"\""', _removeWhitespace(s))
Ejemplo n.º 30
0
 def testWriteFalse(self):
     self.assertEqual("false", _removeWhitespace(chjson.encode(False)))