def test_various_optional_anys(self): j = JSGPython('doc {x: .? }') for v in ['null', '17', '-22', '-22.0', 'false', '"A string"', '{"x": null}']: json = f'{{"x": {v}}}' j2 = loads(json, j.module) self.assertEqual(json, as_json(j2, indent=None)) json = '{"x": -17395e-2}' self.assertEqual('{"x": -173.95}', as_json(loads(json, j.module), indent=None))
def test_optional_any(self): j = JSGPython('doc { x: .? }', print_python=False) rslt = j.conforms('{"x": null}') if not rslt.success: print(str(rslt)) self.assertTrue(rslt.success) jclass = getattr(j.module, 'doc') j2 = jclass() self.assertEqual('{}', as_json(j2)) self.assertTrue(is_valid(j2)) j2.x = None self.assertEqual('{"x": null}', as_json(j2, indent=None)) self.assertTrue(is_valid(j2)) j2.x = Empty self.assertEqual('{}', as_json(j2)) self.assertTrue(is_valid(j2))
def test_required_null(self): j = JSGPython('doc { x: @null }', print_python=False) rslt = j.conforms('{"x": null}') if not rslt.success: print(str(rslt)) self.assertTrue(rslt.success) jclass = getattr(j.module, 'doc') j2 = jclass() self.assertEqual('{}', as_json(j2)) self.assertFalse(is_valid(j2)) j2.x = None self.assertEqual('{"x": null}', as_json(j2, indent=None)) self.assertTrue(is_valid(j2)) j2.x = Empty self.assertEqual('{}', as_json(j2)) self.assertFalse(is_valid(j2)) with self.assertRaises(ValueError): j2.x = 17
def test_example(self): pyobj = jsonasobj.loads(str(test_json)) self.assertEqual('Markus Lanthaler', pyobj.name) self.assertEqual(pyobj.name, pyobj['name']) self.assertEqual('Dave Longley', pyobj.knows[0].name) self.assertEqual('http://xmlns.com/foaf/0.1/name', pyobj['@context'].name) self.assertEqual(json.loads(test_json), json.loads(pyobj._as_json)) self.assertEqual(json.loads(pyobj._as_json), json.loads(as_json(pyobj))) self.assertTrue(compare_dicts(test_data, pyobj._as_dict)) self.assertTrue(compare_dicts(test_data, as_dict(pyobj)))
def test_basic_map(self): class IntObjectMap(JSGObjectMap): _name_filter = HEX _value_type = ArrayFactory('', _CONTEXT, Integer, 0, None) def __init__(self, **_kwargs): super().__init__(_CONTEXT, **_kwargs) x = IntObjectMap() x.E = [1,2,3] self.assertTrue(x._is_valid()) self.assertEqual(as_json(x), as_json(jsonloads('{"E":[1,2,3]}'))) with self.assertRaises(ValueError): x.G = [1, 2, 4] with self.assertRaises(ValueError): x.C = 1 with self.assertRaises(ValueError): x = IntObjectMap(aa=[1])
def validate_shexj_json(json_str: str, input_fname: str, parser: JSGPython) -> bool: """Validate json_str against ShEx Schema :param json_str: String to validate :param input_fname: Name of source file for error reporting :param parser: JSGPython parser :return: True if pass """ rslt = parser.conforms(json_str, input_fname) if not rslt.success: print("File: {} - ".format(input_fname)) print(str(rslt.fail_reason)) return False else: log = StringIO() if not compare_json(json_str, as_json(parser.json_obj), cast(TextIO, log)): print("File: {} - ".format(input_fname)) print(log.getvalue()) print(as_json(parser.json_obj)) return False return True
def test_facet(self): file_loc = os.path.join(os.path.dirname(__file__), 'data', '1bnodeLength.json') with open(file_loc) as f: text = f.read() facets = loads(text, ShExJ) # print(as_json(facets)) log = Logger() val = is_valid(facets, log) # print(log.getvalue()) self.assertTrue(val) d1 = jao_loads(text) d2 = jao_loads(as_json(facets)) self.assertTrue(compare_dicts(as_dict(d1), as_dict(d2)))
def test_any_value(self): class IRIKey(JSGObjectMap): _name_filter = IRI def __init__(self, **_kwargs): super().__init__(_CONTEXT, **_kwargs) x = IRIKey(**{"http://example.org": 42, "http://ex.org?id=1": None}) self.assertEqual('{"http://example.org": 42, "http://ex.org?id=1": null}', as_json(x, indent=None)) self.assertTrue(x._is_valid()) self.assertEqual(42, x["http://example.org"]) self.assertIsNone(x["http://ex.org?id=1"])
def _update_index(self) -> None: """ Update the disk index from the memory file """ with open(self._cache_directory_index, 'w') as f: f.write(as_json(self._cache))
"name": "Dave Longley", "menu": "something", "modelDate" : "01/01/2015" } ] }""" py_obj = jsonasobj.loads(test_json) py_obj.knows[0].extra = {'age': 17} py_obj.knows.append(dict(name='Barack Obama')) del py_obj.knows[0]['menu'] print(py_obj.name) print(py_obj['name']) print(py_obj.knows[0].name) print(py_obj['@context'].name) print(as_json(py_obj)) print(as_dict(py_obj)) ''' Result: Markus Lanthaler Markus Lanthaler Dave Longley http://xmlns.com/foaf/0.1/name { "@id": "http://me.markus-lanthaler.com/", "knows": [ { "extra": { "age": 17 },
def test_as_json(self): """ Test the JSON serialization """ py_obj = jsonasobj.loads(test_json) self.assertEqual(json.loads(test_json), json.loads(py_obj._as_json)) self.assertEqual(json.loads(test_json), json.loads(as_json(py_obj)))