blob: c593cc1ca15eb64ccd079ea3616feb066aebc201 [file] [log] [blame]
lowRISC Contributors802543a2019-08-31 12:12:56 +01001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
Philipp Wagner14a3fee2019-11-21 10:07:02 +00004"""Generate JSON/compact JSON/Hjson from register JSON tree
lowRISC Contributors802543a2019-08-31 12:12:56 +01005"""
6
7import hjson
8
9
10def gen_json(obj, outfile, format):
11 if format == 'json':
Weicai Yang53b0d4d2020-11-30 15:28:33 -080012 hjson.dumpJSON(obj,
13 outfile,
14 ensure_ascii=False,
15 use_decimal=True,
16 indent=' ',
17 for_json=True)
lowRISC Contributors802543a2019-08-31 12:12:56 +010018 elif format == 'compact':
Weicai Yang53b0d4d2020-11-30 15:28:33 -080019 hjson.dumpJSON(obj,
20 outfile,
21 ensure_ascii=False,
22 for_json=True,
23 use_decimal=True,
24 separators=(',', ':'))
lowRISC Contributors802543a2019-08-31 12:12:56 +010025 elif format == 'hjson':
Weicai Yang53b0d4d2020-11-30 15:28:33 -080026 hjson.dump(obj,
27 outfile,
28 ensure_ascii=False,
29 for_json=True,
30 use_decimal=True)
lowRISC Contributors802543a2019-08-31 12:12:56 +010031 else:
Philipp Wagner14a3fee2019-11-21 10:07:02 +000032 raise ValueError('Invalid JSON format ' + format)
Rupert Swarbrick6880e212021-02-10 16:10:00 +000033
34 return 0