X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=csit%2Flibraries%2Fnorm_json.py;h=7dde004504513020b8b1c284ffdbea499dfcaaa5;hb=af48a74be55aec608b778b764cc05df1ec7d9ffa;hp=d46d6c78ffe909b4eacd6a5bb553952fba594d7e;hpb=7d2063bdec07fd06d64e56f7924b5583200b8305;p=integration%2Ftest.git diff --git a/csit/libraries/norm_json.py b/csit/libraries/norm_json.py index d46d6c78ff..7dde004504 100644 --- a/csit/libraries/norm_json.py +++ b/csit/libraries/norm_json.py @@ -18,7 +18,7 @@ __license__ = "Eclipse Public License v1.0" __email__ = "vrpolak@cisco.com" -# Internal details. +# Internal details; look down below for Robot Keywords. class _Hsfl(list): @@ -93,23 +93,12 @@ class _Decoder(_json.JSONDecoder): self.scan_once = _json.scanner.py_make_scanner(self) -# Robot Keywords. +# Robot Keywords; look above for internal details. def loads_sorted(text, strict=False): - """ - Return Python object with sorted arrays and dictionary keys. - - If strict is true, raise exception on parse error. - If strict is not true, return string with error message. - """ - try: - object_decoded = _json.loads(text, cls=_Decoder, object_hook=_Hsfod) - except ValueError as err: - if strict: - raise err - else: - return str(err) + '\n' + text + """Return Python object with sorted arrays and dictionary keys.""" + object_decoded = _json.loads(text, cls=_Decoder, object_hook=_Hsfod) return object_decoded @@ -126,7 +115,19 @@ def dumps_indented(obj, indent=1): def normalize_json_text(text, strict=False, indent=1): # pylint likes lowercase - """Return sorted indented JSON string, or an error message string.""" - object_decoded = loads_sorted(text, strict=strict) + """ + Attempt to return sorted indented JSON string. + + If parse error happens: + If strict is true, raise the exception. + If strict is not true, return original text with error message. + """ + try: + object_decoded = loads_sorted(text) + except ValueError as err: + if strict: + raise err + else: + return str(err) + '\n' + text pretty_json = dumps_indented(object_decoded, indent=indent) return pretty_json