def test_stepwise_encoding(self): params = {'height': 640, 'width': 480} params_literal = JSONstr(json_dumps(params, partially_serialized=True)) rpc = json_rpc('open_window', params_literal) if sys.version_info >= (3, 6): assert rpc == '{"jsonrpc":"2.0","method":"open_window","params":{"height":640,"width":480}}', rpc else: obj = json.loads(rpc) assert obj == json.loads( '{"jsonrpc":"2.0","method":"open_window","params":{"height":640,"width":480}}' )
def test_custom_null_value(self): assert json_dumps(JSONnull) == "null" assert json_dumps(JSONnull, partially_serialized=True) == "null" assert json.loads(json_dumps(JSONnull)) is None null = JSONnull() assert json_dumps(null) == "null" assert json_dumps(null, partially_serialized=True) == "null" assert json.loads(json_dumps(null)) is None
def profile_serializing(): with open(os.path.join(scriptpath, 'data', 'inferus.ausgabe.xml')) as f: data = f.read() tree = parse_xml(data) print('XML inferus') cpu_profile(tree.as_xml, 100) print('S-Expression inferus') cpu_profile(lambda: tree.as_sxpr(compact=True), 100) print('json inferus') cpu_profile(lambda: tree.as_json(indent=None), 100) print('toolkit.json_dumps inferus') cpu_profile(lambda: json_dumps(tree.to_json_obj()), 100) with open(os.path.join(scriptpath, 'data', 'testdoc3.xml')) as f: data = f.read() tree = parse_xml(data) print('XML testdoc3') cpu_profile(tree.as_xml, 100) print('S-Expression testdoc3') cpu_profile(lambda: tree.as_sxpr(compact=True), 100) print('json testdoc3') cpu_profile(lambda: tree.as_json(indent=None), 100) print('toolkit.json_dumps testdoc3') cpu_profile(lambda: json_dumps(tree.to_json_obj()), 100)
def compile_EBNF(text: str, diagnostics_signature: bytes) -> (str, bytes): from DHParser.compile import compile_source from DHParser.ebnf import get_ebnf_preprocessor, get_ebnf_grammar, get_ebnf_transformer, \ get_ebnf_compiler from DHParser.toolkit import json_dumps compiler = get_ebnf_compiler("EBNFServerAnalyse", text) result, messages, _ = compile_source(text, get_ebnf_preprocessor(), get_ebnf_grammar(), get_ebnf_transformer(), compiler) # TODO: return errors as well as (distilled) information about symbols for code propositions signature = b''.join(msg.signature() for msg in messages) if signature != diagnostics_signature: # publish diagnostics only if the same diagnostics have not been reported earlier, already diagnostics = [msg.diagnosticObj() for msg in messages] return json_dumps( diagnostics), signature # TODO: bytes is not a JSON-type proper! else: return '[]', signature
def test_roundtrip(self): if sys.version_info >= (3, 6): obj = json.loads(self.data) jsons = json_dumps(obj, partially_serialized=True) assert jsons == self.data
def test_boundary_cases(self): assert json_dumps([], partially_serialized=True) == "[]" assert json_dumps({}, partially_serialized=True) == "{}" assert json_dumps(None, partially_serialized=True) == "null"
def test_str_escaping(self): data = 'The dragon said "hush"!' jsons = json_dumps(data, partially_serialized=True) assert jsons == r'"The dragon said \"hush\"!"', jsons
def test_bool_and_None(self): data = [True, False, None, 1, 2.0, "abc"] jsons = json_dumps(data, partially_serialized=True) assert jsons == '[true,false,null,1,2.0,"abc"]', jsons