Exemple #1
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)
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
 def testReadEscapedHorizontalTab(self):
     obj = chjson.decode(r'"\t"')
     self.assertEqual("\t", obj)
Exemple #6
0
 def testReadEscapedNewline(self):
     obj = chjson.decode(r'"\n"')
     self.assertEqual("\n", obj)
Exemple #7
0
 def testReadEscapedBackspace(self):
     obj = chjson.decode(r'"\b"')
     self.assertEqual("\b", obj)
Exemple #8
0
 def testReadEscapedReverseSolidus(self):
     obj = chjson.decode(r'"\\"')
     self.assertEqual('\\', obj)
Exemple #9
0
 def _testSingleLineCommentAndLineContinuation_2(self):
     obj = chjson.decode('{"SQL Statement": "SELECT foo; -- A comment. \rSELECT bar;",}')
Exemple #10
0
 def testReadEscapedQuotationMark(self):
     obj = chjson.decode(r'"\""')
     self.assertEqual(r'"', obj)
Exemple #11
0
 def _testDecodeObjectWithTrailingOnelineComment(self):
     obj = chjson.decode('{"string": "blah blah more blahs "} // nothing', strict=True)
Exemple #12
0
 def testObjectBasicString_01(self):
     obj = chjson.decode(r'"\\"')
     self.assertEqual('\\', obj)
     obj = chjson.decode("\"\\\\\"")
     self.assertEqual('\\', obj)
Exemple #13
0
 def _testDecodeWithNewLinesStrict(self):
     chjson.decode('{"string": "blah blah \n more blahs \r\n"}', strict=True)
Exemple #14
0
 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)
Exemple #15
0
 def _testDecodeWithNewLinesLoose_01(self):
     chjson.decode('{"string": "blah blah \n more blahs \r\n",} // nothing')
Exemple #16
0
 def testDecodeStringEscapedSolidusAndNoTrailingComma(self):
     obj = chjson.decode('{"string": "hello\/goodbye"}', strict=True)
     self.assertEqual({"string": "hello/goodbye",}, obj)
Exemple #17
0
 def testObjectBasicString_02(self):
     obj = chjson.decode('"\"')
     # This reduces to the empty string because '"\"' is interpreted
     # by Python as '""'.
     self.assertEqual('', obj)
Exemple #18
0
 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)
Exemple #19
0
 def _testObjectBasicString_03(self):
     chjson.decode('"\\"')
Exemple #20
0
 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)
Exemple #21
0
 def testDecodeBasicList(self):
     obj = chjson.decode(" [ true, false,null] ")
     self.assertEqual([True, False, None], obj)
Exemple #22
0
 def testReadEscapedSolidus(self):
     obj = chjson.decode(r'"\/"')
     self.assertEqual(r'/', obj)
Exemple #23
0
 def testDecodeSolidus_01(self):
     obj = chjson.decode('{"string": "\/",}')
     self.assertEqual({'string': '/'}, obj)
Exemple #24
0
 def _testReadEscapedEndingQuote(self):
     chjson.decode('"\\"')
Exemple #25
0
 def testDecodeSolidus_02(self):
     obj = chjson.decode('{"string": "o\/g",}')
     self.assertEqual({'string': 'o/g'}, obj)
Exemple #26
0
 def testReadEscapedFormfeed(self):
     obj = chjson.decode(r'"\f"')
     self.assertEqual("\f", obj)
Exemple #27
0
 def testReadStringValue(self):
     obj = chjson.decode('{ "name" : "Patrick" }')
     self.assertEqual({ "name" : "Patrick" }, obj)
Exemple #28
0
 def testReadEscapedCarriageReturn(self):
     obj = chjson.decode(r'"\r"')
     self.assertEqual("\r", obj)
Exemple #29
0
 def testDecodeSolidus_04(self):
     obj = chjson.decode('{"string": "hello/goodbye",}')
     self.assertEqual({'string': 'hello/goodbye'}, obj)
Exemple #30
0
 def testReadEscapedHexCharacter(self):
     obj = chjson.decode(r'"\u000A"')
     self.assertEqual("\n", obj)
     obj = chjson.decode(r'"\u1001"')
     self.assertEqual(u'\u1001', obj)
Exemple #31
0
 def _testDecodeStringEscapedSolidusAndTrailingComma(self):
     obj = chjson.decode('{"string": "hello\/goodbye",}', strict=True)