diff --git a/JSONReindent.sublime-settings b/JSONReindent.sublime-settings index e0e9565..b3ccc3d 100644 --- a/JSONReindent.sublime-settings +++ b/JSONReindent.sublime-settings @@ -1,4 +1,3 @@ { - "sort_keys": false, - "strict_decoder": false + "sort_keys": false } diff --git a/json_reindent/__init__.py b/json_reindent/__init__.py index 6aff385..72bd544 100644 --- a/json_reindent/__init__.py +++ b/json_reindent/__init__.py @@ -1,13 +1,9 @@ -import yaml import json -import logging from .utils import ordered_load -logger = logging.getLogger(__name__) - def parse_input(content, *args, **kwargs): - return ordered_load(content, yaml.SafeLoader, *args, **kwargs) + return ordered_load(content, *args, **kwargs) def format_output(data, indent=2, separators=[',', ': '], sort_keys=False, ensure_ascii=False): diff --git a/json_reindent/utils.py b/json_reindent/utils.py index 23b03d2..f1c30e2 100644 --- a/json_reindent/utils.py +++ b/json_reindent/utils.py @@ -1,13 +1,33 @@ +import json import yaml import collections +import logging +logger = logging.getLogger(__name__) -def ordered_load(stream, Loader=yaml.Loader, use_collections=True): + +def ordered_load(stream, use_collections=True): if use_collections and hasattr(collections, 'OrderedDict'): object_pairs_hook = collections.OrderedDict else: object_pairs_hook = dict + try: + return yaml_ordered_load(stream, object_pairs_hook) + except Exception as e: + logger.error(e, exc_info=True) + + return json_ordered_load(stream, object_pairs_hook) + + +def json_ordered_load(stream, object_pairs_hook): + return json.loads( + stream, + strict=False, + object_pairs_hook=object_pairs_hook) + + +def yaml_ordered_load(stream, object_pairs_hook, Loader=yaml.Loader): class OrderedLoader(Loader): pass diff --git a/tests/static/fail_yaml/result.json b/tests/static/fail_yaml/result.json new file mode 100644 index 0000000..562a08c --- /dev/null +++ b/tests/static/fail_yaml/result.json @@ -0,0 +1,4 @@ +{ + "deviceID": "99d5ce418b134705", + "_useragent": "android\/app\/4.4" +} \ No newline at end of file diff --git a/tests/static/fail_yaml/test.json b/tests/static/fail_yaml/test.json new file mode 100644 index 0000000..ee979fd --- /dev/null +++ b/tests/static/fail_yaml/test.json @@ -0,0 +1 @@ +{"deviceID":"99d5ce418b134705","_useragent":"android\/app\/4.4"} \ No newline at end of file diff --git a/tests/test_base.py b/tests/test_base.py index 8bcca27..356b82c 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -33,3 +33,9 @@ def test_no_collections(): data = parse_input(get_static_content('base/test.json'), use_collections=False) output = format_output(data) assert json.loads(output) == json.loads(get_static_content('base/result.json')) + + +def test_fail_yaml(): + data = parse_input(get_static_content('fail_yaml/test.json')) + output = format_output(data) + assert json.loads(output) == json.loads(get_static_content('fail_yaml/result.json'))