-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.py
73 lines (55 loc) · 1.42 KB
/
json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from bench import helpers
from textx import metamodel_from_str
def compile():
class Object(dict):
def __init__(self, members, parent=None):
self.update((m.key, m.value) for m in members)
class Array(list):
def __init__(self, values, parent=None):
self.extend(values)
json_mm = metamodel_from_str(
r"""/*
A grammar for JSON data-interchange format.
See: http://www.json.org/
*/
File:
Value
;
Array:
"[" values*=Value[','] "]"
;
Value:
STRING | FLOAT | BOOL | Object | Array | NULL
;
STRING:
/"([ !#-\[\]-\U0010ffff]+|\\(["\/\\bfnrt]|u[0-9A-Fa-f]{4}))*"/
;
FLOAT:
/-?(0|[1-9][0-9]*)(\.[0-9]+)?([Ee][+-]?[0-9]+)?/
;
BOOL:
/\b(true|false)\b/
;
NULL:
/\bnull\b/
;
Object:
"{" members*=Member[','] "}"
;
Member:
key=STRING ':' value=Value
;
""",
debug=False,
classes=[Object, Array],
)
json_mm.register_obj_processors(
{
"NULL": lambda _: None,
"STRING": helpers.json_unescape,
"FLOAT": float,
"BOOL": lambda x: x == "true",
}
)
return lambda s: json_mm.model_from_str(s)
parse = compile()