summaryrefslogtreecommitdiffstats
path: root/tests/yaml2json.py
blob: e97b4721025b10f513381ea5f2ac1f0f16bdc115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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))