[issue39686] add dump_json to ast module

Richard K report at bugs.python.org
Wed Feb 19 01:23:22 EST 2020


New submission from Richard K <qpotizo at gmail.com>:

Currently within the ast module, `dump` generates a string representation of the AST for example,

>>> ast.dump(node)
'Module(body=[], type_ignores=[])'


The proposed enhancement would provide a complementary function, `dump_json` as in a json representation of the ast. 
This would be useful for those who would like to benefit from the utilities of the json module for formatting, pretty-printing, and the like.  
It would also be useful for those who want to serialize the AST or export it in a form that can be consumed in an other programming language.
A simplified example, 


>>> import ast
>>> node = ast.parse('')
>>> ast.dump_json(node)
{'Module': {'body': [], 'type_ignores': []}}


A simplified example of using `ast.dump_json` with the json module,

>>> import json
>>> json.dumps(ast.dump_json(node))
'{"Module": {"body": [], "type_ignores": []}}'

----------
components: Library (Lib)
messages: 362256
nosy: sparverius
priority: normal
severity: normal
status: open
title: add dump_json to ast module
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39686>
_______________________________________


More information about the Python-bugs-list mailing list