OILS / devtools / yaml2json.py View on Github | oilshell.org

38 lines, 20 significant
1#!/usr/bin/env python2
2"""
3yaml2json.py
4
5Note: this tool is incorrect! See 'bug' in yaml2json.sh.
6
7https://john-millikin.com/json-is-not-a-yaml-subset
8"""
9from __future__ import print_function
10
11import json
12import sys
13
14import yaml
15
16
17def main(argv):
18 argv = argv[1:]
19
20 if len(argv) == 0:
21 obj = yaml.safe_load(sys.stdin)
22
23 elif len(argv) == 1:
24 with open(argv[0]) as f:
25 obj = yaml.safe_load(f)
26
27 else:
28 raise RuntimeError('Too many args')
29
30 json.dump(obj, sys.stdout, indent=2)
31
32
33if __name__ == '__main__':
34 try:
35 main(sys.argv)
36 except RuntimeError as e:
37 print('FATAL: %s' % e, file=sys.stderr)
38 sys.exit(1)