30 lines
570 B
Python
30 lines
570 B
Python
import lark
|
|
import pprint
|
|
|
|
class JsonTransformer(lark.Transformer):
|
|
def string(self, el):
|
|
(el,) = el
|
|
return el[1:-1]
|
|
|
|
def number(self, el):
|
|
(el, ) = el
|
|
return float(el)
|
|
|
|
|
|
list = list
|
|
dict = dict
|
|
pair = tuple
|
|
|
|
null = lambda self, _: None
|
|
true = lambda self, _: True
|
|
false = lambda self, _: False
|
|
|
|
json = '{"key": ["item0", "item1", 3.14, true]}'
|
|
|
|
with open("json.lark") as f:
|
|
json_parser = lark.Lark(f, start="value", parser="lalr", transformer=JsonTransformer())
|
|
|
|
t = json_parser.parse(json)
|
|
print(t)
|
|
|