diff options
Diffstat (limited to 'tests/yaml2json.py')
-rw-r--r-- | tests/yaml2json.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/yaml2json.py b/tests/yaml2json.py new file mode 100644 index 0000000..e97b472 --- /dev/null +++ b/tests/yaml2json.py @@ -0,0 +1,19 @@ +import sys +import yaml +import json +from collections import OrderedDict + +def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict): + class OrderedLoader(Loader): + pass + def construct_mapping(loader, node): + loader.flatten_mapping(node) + return object_pairs_hook(loader.construct_pairs(node)) + OrderedLoader.add_constructor( + yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, + construct_mapping) + return yaml.load(stream, OrderedLoader) + +#obj = json.load(sys.stdin, object_pairs_hook=OrderedDict) +obj = ordered_load(sys.stdin, yaml.SafeLoader) +print(json.dumps(obj, indent=4, sort_keys=False)) |