lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 4 | """Generate JSON/compact JSON/Hjson from register JSON tree |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import hjson |
| 8 | |
| 9 | |
| 10 | def gen_json(obj, outfile, format): |
| 11 | if format == 'json': |
Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 12 | hjson.dumpJSON(obj, |
| 13 | outfile, |
| 14 | ensure_ascii=False, |
| 15 | use_decimal=True, |
| 16 | indent=' ', |
| 17 | for_json=True) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 18 | elif format == 'compact': |
Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 19 | hjson.dumpJSON(obj, |
| 20 | outfile, |
| 21 | ensure_ascii=False, |
| 22 | for_json=True, |
| 23 | use_decimal=True, |
| 24 | separators=(',', ':')) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 25 | elif format == 'hjson': |
Weicai Yang | 53b0d4d | 2020-11-30 15:28:33 -0800 | [diff] [blame] | 26 | hjson.dump(obj, |
| 27 | outfile, |
| 28 | ensure_ascii=False, |
| 29 | for_json=True, |
| 30 | use_decimal=True) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 31 | else: |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 32 | raise ValueError('Invalid JSON format ' + format) |
Rupert Swarbrick | 6880e21 | 2021-02-10 16:10:00 +0000 | [diff] [blame] | 33 | |
| 34 | return 0 |