diff options
Diffstat (limited to 'scripts/jsonmodify.py')
-rwxr-xr-x | scripts/jsonmodify.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/scripts/jsonmodify.py b/scripts/jsonmodify.py index 96ff8cacd..8bd8d4556 100755 --- a/scripts/jsonmodify.py +++ b/scripts/jsonmodify.py @@ -82,6 +82,14 @@ class AppendList(str): raise ValueError(self + " should be a array.") cur[key].extend(args) +# A JSONDecoder that supports line comments start with // +class JSONWithCommentsDecoder(json.JSONDecoder): + def __init__(self, **kw): + super().__init__(**kw) + + def decode(self, s: str): + s = '\n'.join(l for l in s.split('\n') if not l.lstrip(' ').startswith('//')) + return super().decode(s) def main(): parser = argparse.ArgumentParser() @@ -115,9 +123,9 @@ def main(): if args.input: with open(args.input) as f: - obj = json.load(f, object_pairs_hook=collections.OrderedDict) + obj = json.load(f, object_pairs_hook=collections.OrderedDict, cls=JSONWithCommentsDecoder) else: - obj = json.load(sys.stdin, object_pairs_hook=collections.OrderedDict) + obj = json.load(sys.stdin, object_pairs_hook=collections.OrderedDict, cls=JSONWithCommentsDecoder) for p in args.patch: p[0].apply(obj, *p[1:]) |