| 291 | |
| 292 | |
| 293 | def parse_json(path): |
| 294 | header_files = [] |
| 295 | |
| 296 | with open(path, encoding='utf-8') as stream: |
| 297 | # Remove comments before loading the JSON. |
| 298 | data = json.loads(re.sub(r'//.*\n', '', stream.read())) |
| 299 | |
| 300 | if not isinstance(data, list): |
| 301 | data = [data] |
| 302 | |
| 303 | for item in data: |
| 304 | for key in item: |
| 305 | if key not in {'file', 'defines', 'structs'}: |
| 306 | raise 'Unexpected key in json file: %s' % key |
| 307 | |
| 308 | header = {'name': item['file'], 'structs': {}, 'defines': {}} |
| 309 | for name, data in item.get('structs', {}).items(): |
| 310 | if name in header['structs']: |
| 311 | show('WARN: Description of struct "' + name + '" in file "' + item['file'] + '" replaces an existing description!') |
| 312 | |
| 313 | header['structs'][name] = data |
| 314 | |
| 315 | for part in item.get('defines', []): |
| 316 | if not isinstance(part, list): |
| 317 | # If no type is specified, assume integer. |
| 318 | part = ['i', part] |
| 319 | |
| 320 | if part[1] in header['defines']: |
| 321 | show('WARN: Description of define "' + part[1] + '" in file "' + item['file'] + '" replaces an existing description!') |
| 322 | |
| 323 | header['defines'][part[1]] = part[0] |
| 324 | |
| 325 | header_files.append(header) |
| 326 | |
| 327 | return header_files |
| 328 | |
| 329 | |
| 330 | def output_json(obj, stream): |