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)
def testReadLongArray(self): src = '''[ "used", "abused", "confused", true, false, null, 1, 2, [3, 4, 5]] ''' obj = chjson.decode(src) self.assertEqual(["used","abused","confused", True, False, None, 1,2,[3,4,5]], obj)
def testReadComplexArray(self): src = ''' [ { "name": "Patrick", "age" : 44, "Employed?" : true, "Female?" : false, "grandchildren":null }, "used", "abused", "confused", 1, 2, [3, 4, 5] ] ''' obj = chjson.decode(src) self.assertEqual([{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None}, "used","abused","confused", 1,2,[3,4,5]], obj)
def app_cfg_load(self, cfg_filename, cfg_default): # See: # http://pyxdg.readthedocs.io/en/latest/_modules/xdg/BaseDirectory.html # This (`mkdir -p`s and) returns a path to the application config, # e.g., '/home/${USER}/.config/check\xe2\x9c\x93++' cfg_base = BaseDirectory.save_config_path(application_resource) self.cfg_path = os.path.join(cfg_base, cfg_filename) self.cfg_data = None if os.path.isfile(self.cfg_path): with open(self.cfg_path, 'r') as f_in: try: raw_data = f_in.read() if raw_data: self.cfg_data = chjson.decode(raw_data) # else, leave self.cfg_data = None and we'll create it. except chjson.DecodeError as err: #raise User_Exception() print( "Whoopsie! What's wrong with the app config? It doesn't look like JSON: %s" % (cfg_path, )) print('Overwriting app config and starting fresh, sorry!') if self.cfg_data is None: self.cfg_data = copy.deepcopy(cfg_default)
def testReadEscapedHorizontalTab(self): obj = chjson.decode(r'"\t"') self.assertEqual("\t", obj)
def testReadEscapedNewline(self): obj = chjson.decode(r'"\n"') self.assertEqual("\n", obj)
def testReadEscapedBackspace(self): obj = chjson.decode(r'"\b"') self.assertEqual("\b", obj)
def testReadEscapedReverseSolidus(self): obj = chjson.decode(r'"\\"') self.assertEqual('\\', obj)
def _testSingleLineCommentAndLineContinuation_2(self): obj = chjson.decode('{"SQL Statement": "SELECT foo; -- A comment. \rSELECT bar;",}')
def testReadEscapedQuotationMark(self): obj = chjson.decode(r'"\""') self.assertEqual(r'"', obj)
def _testDecodeObjectWithTrailingOnelineComment(self): obj = chjson.decode('{"string": "blah blah more blahs "} // nothing', strict=True)
def testObjectBasicString_01(self): obj = chjson.decode(r'"\\"') self.assertEqual('\\', obj) obj = chjson.decode("\"\\\\\"") self.assertEqual('\\', obj)
def _testDecodeWithNewLinesStrict(self): chjson.decode('{"string": "blah blah \n more blahs \r\n"}', strict=True)
def testDecodeWithNewLinesLoose_02(self): # chjson accects newlines but they have to be escaped # (you can't just hit Enter in the middle of typing a string). obj = chjson.decode('{"string": "blah blah \\n more blahs \\r\\n",} // nothing') self.assertEqual({"string": "blah blah \n more blahs \r\n",}, obj)
def _testDecodeWithNewLinesLoose_01(self): chjson.decode('{"string": "blah blah \n more blahs \r\n",} // nothing')
def testDecodeStringEscapedSolidusAndNoTrailingComma(self): obj = chjson.decode('{"string": "hello\/goodbye"}', strict=True) self.assertEqual({"string": "hello/goodbye",}, obj)
def testObjectBasicString_02(self): obj = chjson.decode('"\"') # This reduces to the empty string because '"\"' is interpreted # by Python as '""'. self.assertEqual('', obj)
def testDecodeLineContinuationsAndOtherEscapes(self): obj = chjson.decode('{"x\t\\\/": "a green \\\r cow \t mooed \f oh heavens \b\b\b",}') self.assertEqual({'x\t\\/': 'a green \r cow \t mooed \x0c oh heavens \x08\x08\x08'}, obj)
def _testObjectBasicString_03(self): chjson.decode('"\\"')
def testSingleLineCommentAndLineContinuation_1(self): obj = chjson.decode('{"SQL Statement": "SELECT foo; -- A comment. \\\rSELECT bar;",}') self.assertEqual({'SQL Statement': 'SELECT foo; -- A comment. \rSELECT bar;'}, obj)
def testDecodeBasicList(self): obj = chjson.decode(" [ true, false,null] ") self.assertEqual([True, False, None], obj)
def testReadEscapedSolidus(self): obj = chjson.decode(r'"\/"') self.assertEqual(r'/', obj)
def testDecodeSolidus_01(self): obj = chjson.decode('{"string": "\/",}') self.assertEqual({'string': '/'}, obj)
def _testReadEscapedEndingQuote(self): chjson.decode('"\\"')
def testDecodeSolidus_02(self): obj = chjson.decode('{"string": "o\/g",}') self.assertEqual({'string': 'o/g'}, obj)
def testReadEscapedFormfeed(self): obj = chjson.decode(r'"\f"') self.assertEqual("\f", obj)
def testReadStringValue(self): obj = chjson.decode('{ "name" : "Patrick" }') self.assertEqual({ "name" : "Patrick" }, obj)
def testReadEscapedCarriageReturn(self): obj = chjson.decode(r'"\r"') self.assertEqual("\r", obj)
def testDecodeSolidus_04(self): obj = chjson.decode('{"string": "hello/goodbye",}') self.assertEqual({'string': 'hello/goodbye'}, obj)
def testReadEscapedHexCharacter(self): obj = chjson.decode(r'"\u000A"') self.assertEqual("\n", obj) obj = chjson.decode(r'"\u1001"') self.assertEqual(u'\u1001', obj)
def _testDecodeStringEscapedSolidusAndTrailingComma(self): obj = chjson.decode('{"string": "hello\/goodbye",}', strict=True)