Skip to content

Commit 0134c17

Browse files
committed
fix json printer
1 parent 972b588 commit 0134c17

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

luaparser/ast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def to_xml_str(tree):
8282

8383
class JSONEncoder(json.JSONEncoder):
8484
def default(self, o):
85+
if isinstance(o, bytes):
86+
return o.decode()
87+
8588
try:
8689
to_json = getattr(o, "to_json")
8790
if callable(to_json):

luaparser/tests/test_ast.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,43 @@ def test_cont_int_1(self):
9999
]
100100
for node, exp in zip(nodes, expected_cls):
101101
self.assertIsInstance(node, exp)
102+
103+
def test_to_pretty_json(self):
104+
src = textwrap.dedent(
105+
"""\
106+
local a = "foo"\
107+
"""
108+
)
109+
exp = textwrap.dedent(
110+
"""\
111+
{
112+
"comments": [],
113+
"body": {
114+
"comments": [],
115+
"body": [
116+
{
117+
"comments": [],
118+
"wrapped": false,
119+
"targets": [
120+
{
121+
"comments": [],
122+
"wrapped": false,
123+
"id": "a",
124+
"attribute": null
125+
}
126+
],
127+
"values": [
128+
{
129+
"comments": [],
130+
"wrapped": false,
131+
"s": "foo",
132+
"raw": "foo",
133+
"delimiter": {}
134+
}
135+
]
136+
}
137+
]
138+
}
139+
}"""
140+
)
141+
self.assertEqual(ast.to_pretty_json(ast.parse(src)), exp)

0 commit comments

Comments
 (0)