-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.io
287 lines (242 loc) · 7.35 KB
/
json.io
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
true asJson := method(buffer,
buffer ifNilEval(Sequence clone) appendSeq("true")
)
false asJson := method(buffer,
buffer ifNilEval(Sequence clone) appendSeq("false")
)
nil asJson := method(buffer,
buffer ifNilEval(Sequence clone) appendSeq("null")
)
Sequence asJson := method(buffer,
buffer ifNilEval(Sequence clone) appendSeq("\"", self asMutable escape, "\"")
)
Number asJson := method(buffer,
buffer ifNilEval(Sequence clone) appendSeq(self)
)
List asJson := method(buffer,
buffer = buffer ifNilEval(Sequence clone) appendSeq("[")
first := true
self foreach(x, if(first, first = false, buffer appendSeq(", ")); x asJson(buffer))
buffer appendSeq("]")
)
Object asJson := method(buffer,
buffer = buffer ifNilEval(Sequence clone) appendSeq("{")
first := true
self slotNames sort foreach(name,
if (first, first = false , buffer appendSeq(", "))
name asJson(buffer)
buffer appendSeq(":")
self getSlot(name) asJson(buffer)
)
buffer appendSeq("}")
)
Map asJson := method(buffer,
buffer = buffer ifNilEval(Sequence clone) appendSeq("{")
first := true
self keys sort foreach(name,
if (first, first = false , buffer appendSeq(", "))
name asJson(buffer)
buffer appendSeq(":")
self at(name) asJson(buffer)
)
buffer appendSeq("}")
)
JsonObject := Object clone do(
type := "JsonObject"
protected := method(
self protected = Map clone
)
addSlot := method(name, value,
if(protectedSlotNames contains(name),
protected atPut(name, value)
,
self setSlot(name, value)
)
self
)
asJson := method(buffer,
buffer = buffer ifNilEval(Sequence clone) appendSeq("{")
first := true
keys sort foreach(name,
if (first, first = false , buffer appendSeq(", "))
name asJson(buffer)
buffer appendSeq(":")
protected at(name) ifNilEval(self getSlot(name)) asJson(buffer)
)
buffer appendSeq("}")
)
at := method(name,
if (protectedSlotNames contains(name),
protected at(name)
,
self getSlot(name)
)
)
atPut := method(name, value,
if (protectedSlotNames contains(name),
protected atPut(name, value)
,
self setSlot(name, value)
)
)
keys := method(
names := self slotNames selectInPlace(n, protectedSlotNames contains(n) not)
names appendSeq(protected keys)
)
protectedSlotNames := slotNames append("protectedSlotNames", "protected", "protos", "slotNames", "getSlot", "setSlot", "", "type")
)
JsonParser := Object clone do (
type := "JsonParser"
newSlot("index", 0)
newSlot("buffer")
parseJson := method(
whitespace
object
)
current := method(buffer at(index))
currentIsDigit := method(current isDigit)
currentIs := method(string,
string foreach(i, char, if(buffer at(index + i) != char, return false))
true
)
inc := method(amount, index = index + (amount ifNilEval(1)))
whitespace := method(
while(index < buffer size and current isSpace, inc)
)
useLiteralIfPresent := method(string,
if(currentIs(string), inc(string size) true, false)
)
optionalLiteral := method(string,
if(useLiteralIfPresent(string),
whitespace
true
,
false
)
)
literal := method(string, error,
optionalLiteral(string) ifFalse(
Exception raise ("JsonParser " .. error)
)
)
object := method(
result := JsonObject clone
literal("{", "Expected a '{'.")
if(isString,
result addSlot(string, literal(":", "Expected a ':'."); value)
while(optionalLiteral(","),
result addSlot(string, literal(":", "Expected a ':'."); value)
}
literal("}", "Expected a '}', or ','.")
,
literal("}", "Expected a '}', or quoted string.")
)
result
)
array := method(
result := List clone
literal("[", "Expected a '['.");
if(isValue,
result append(value)
while(optionalLiteral(","),
result append(value)
)
literal("]", "Expected a ']', or ','.");
,
literal("]", "Expected a ']', or a value.");
)
result
)
isObject := method(currentIs("{"))
isArray := method(currentIs("["))
isString := method(currentIs("\""))
isNumber := method(currentIs("-") or current isDigit)
isTrue := method(currentIs("true"))
isFalse := method(currentIs("false"))
isNull := method(currentIs("null"))
isValue := method(
isObject or isArray or isString or isNumber or isTrue or isFalse or isNull
)
value := method(
if(isObject, return object)
if(isArray, return array)
if(isString, return string)
if(isNumber, return number)
if(optionalLiteral("true"), return true)
if(optionalLiteral("false"), return false)
if(optionalLiteral("null"), return nil)
Exception raise("JsonParser Expected a value and instead found '" .. current asCharacter .. "'. A value is an object('{'), array('['), string('\"'), number(-,digit), true, false or null. " .. buffer exSlice(index))
)
string := method(
result := Sequence clone
quote := "\""
slash := "\\"
if(currentIs(quote),
inc
,
Expected raise("JsonParser Expected a '\"'.")
)
while(currentIs(quote) not,
if(currentIs(slash),
result append(current)
inc
if(currentIs("u"),
inc(2)
result append(buffer exSlice(index, index + 2) fromBase(16))
inc(2)
)
)
result append(current)
inc
)
if(currentIs(quote),
inc
,
Expected raise("JsonParser Expected a '\"'.")
)
whitespace
result unescape
result
)
number := method(
buf := Sequence clone
start := index
useLiteralIfPresent("-")
if(currentIs("0"),
inc
,
while(currentIsDigit, inc)
)
if(useLiteralIfPresent("."),
while(currentIsDigit, inc)
)
if(useLiteralIfPresent("e") or useLiteralIfPresent("E"),
useLiteralIfPresent("-") or useLiteralIfPresent("+")
while(currentIsDigit, inc)
)
result := buffer exSlice(start, index) asNumber
whitespace
result
)
)
Sequence parseJson := method(JsonParser clone setBuffer(self) parseJson)
File parseJson := method(asBuffer parseJson)
#x := Object clone
#x a := 1
#x b := 2
#x list := list(1,2,3,4)
#x t := true
#x f := false
#x n := nil
#x s := "seq"
#x o := Object clone do( x := 13; y := 19 )
#x m := Map clone atPut("x", 13) atPut("y", 19)
#x mut := "\"" asMutable
#x hex := 19 asCharacter # Doesn't work as the character is stripped out becuase Io doesn't support escaping.
#x jobj := JsonObject clone addSlot("setSlot", 13) addSlot("addSlot", "hehe")
#
#json := x asJson
#writeln(json)
#
#y := json parseJson
#writeln(y asJson)