OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Pure Dart implementation of JSON protocol. | |
6 | |
7 /** | |
8 * Utility class to parse JSON and serialize objects to JSON. | |
9 */ | |
10 class JSON { | |
11 /** | |
12 * Parses [:json:] and build the corresponding object. | |
13 */ | |
14 static parse(String json) { | |
15 return JsonParser.parse(json); | |
16 } | |
17 | |
18 /** | |
19 * Serializes [:object:] into JSON string. | |
20 */ | |
21 static String stringify(Object object) { | |
22 return JsonStringifier.stringify(object); | |
23 } | |
24 } | |
25 | |
26 //// Implementation /////////////////////////////////////////////////////////// | |
27 | |
28 /** | |
29 * Union-like class for JSON tokens. | |
30 */ | |
31 class JsonToken { | |
32 static final int STRING = 0; | |
33 static final int NUMBER = 1; | |
34 static final int NULL = 2; | |
35 static final int FALSE = 3; | |
36 static final int TRUE = 4; | |
37 static final int RBRACKET = 5; | |
38 static final int LBRACKET = 6; | |
39 static final int RBRACE = 7; | |
40 static final int LBRACE = 8; | |
41 static final int COLON = 9; | |
42 static final int COMMA = 10; | |
43 | |
44 final int kind; | |
45 final String _s; | |
46 final num _n; | |
47 | |
48 String get str() { | |
49 assert(kind == STRING); | |
50 return _s; | |
51 } | |
52 | |
53 num get number() { | |
54 assert(kind == NUMBER); | |
55 return _n; | |
56 } | |
57 | |
58 const JsonToken._internal(this.kind, this._s, this._n); | |
59 | |
60 factory JsonToken.string(String s) { | |
61 return new JsonToken._internal(STRING, s, 0); | |
62 } | |
63 factory JsonToken.number(num n) { | |
64 return new JsonToken._internal(NUMBER, '', n); | |
65 } | |
66 factory JsonToken.atom(int kind) { | |
67 return new JsonToken._internal(kind, '', 0); | |
68 } | |
69 | |
70 String toString() { | |
71 switch (kind) { | |
72 case STRING: | |
73 return 'STRING(${_s})'; | |
74 | |
75 case NUMBER: | |
76 return 'NUMBER(${_n})'; | |
77 | |
78 case NULL: | |
79 return 'ATOM(null)'; | |
80 | |
81 case FALSE: | |
82 return 'ATOM(false)'; | |
83 | |
84 case TRUE: | |
85 return 'ATOM(true)'; | |
86 | |
87 case RBRACKET: | |
88 return 'ATOM(])'; | |
89 | |
90 case LBRACKET: | |
91 return 'ATOM([)'; | |
92 | |
93 case RBRACE: | |
94 return 'ATOM(})'; | |
95 | |
96 case LBRACE: | |
97 return 'ATOM({)'; | |
98 | |
99 case COLON: | |
100 return 'ATOM(:)'; | |
101 | |
102 case COMMA: | |
103 return 'ATOM(,)'; | |
104 } | |
105 } | |
106 } | |
107 | |
108 typedef bool Predicate(int c); | |
109 | |
110 class JsonTokenizer { | |
111 static final int BACKSPACE = 8; // '\b'.charCodeAt(0) | |
112 static final int TAB = 9; // '\t'.charCodeAt(0) | |
113 static final int NEW_LINE = 10; // '\n'.charCodeAt(0) | |
114 static final int FORM_FEED = 12; // '\f'.charCodeAt(0) | |
115 static final int LINE_FEED = 13; // '\r'.charCodeAt(0) | |
116 static final int SPACE = 32; // ' '.charCodeAt(0) | |
117 static final int QUOTE = 34; // '"'.charCodeAt(0) | |
118 static final int PLUS = 43; // '+'.charCodeAt(0) | |
119 static final int COMMA = 44; // ','.charCodeAt(0) | |
120 static final int MINUS = 45; // '-'.charCodeAt(0) | |
121 static final int DOT = 46; // '.'.charCodeAt(0) | |
122 static final int BACKSLASH = 47; // '/'.charCodeAt(0) | |
123 static final int ZERO = 48; // '0'.charCodeAt(0) | |
124 static final int NINE = 57; // '9'.charCodeAt(0) | |
125 static final int COLON = 58; // ':'.charCodeAt(0) | |
126 static final int A_BIG = 65; // 'A'.charCodeAt(0) | |
127 static final int E_BIG = 69; // 'E'.charCodeAt(0) | |
128 static final int Z_BIG = 90; // 'Z'.charCodeAt(0) | |
129 static final int LBRACKET = 91; // '['.charCodeAt(0) | |
130 static final int SLASH = 92; // '\\'.charCodeAt(0) | |
131 static final int RBRACKET = 93; // ']'.charCodeAt(0) | |
132 static final int A_SMALL = 97; // 'a'.charCodeAt(0) | |
133 static final int B_SMALL = 98; // 'b'.charCodeAt(0) | |
134 static final int E_SMALL = 101; // 'e'.charCodeAt(0) | |
135 static final int Z_SMALL = 122; // 'z'.charCodeAt(0) | |
136 static final int LBRACE = 123; // '{'.charCodeAt(0) | |
137 static final int RBRACE = 125; // '}'.charCodeAt(0) | |
138 | |
139 JsonTokenizer(String s) : _s = s + ' ', _pos = 0, _len = s.length + 1 {} | |
140 | |
141 /** | |
142 * Fetches next token or [:null:] if the stream has been exhausted. | |
143 */ | |
144 JsonToken next() { | |
145 while (_pos < _len && isWhitespace(_s.charCodeAt(_pos))) { | |
146 _pos++; | |
147 } | |
148 if (_pos == _len) { | |
149 return null; | |
150 } | |
151 | |
152 final int cur = _s.charCodeAt(_pos); | |
153 switch (true) { | |
154 case cur == QUOTE: | |
155 _pos++; | |
156 | |
157 Array<int> charCodes = new Array<int>(); | |
158 while (_pos < _len) { | |
159 int c = _s.charCodeAt(_pos); | |
160 if (c == QUOTE) { | |
161 break; | |
162 } | |
163 if (c == SLASH) { | |
164 _pos++; | |
165 if (_pos == _len) { | |
166 throw '\\ at the end'; | |
167 } | |
168 | |
169 switch (_s[_pos]) { | |
170 case '"': | |
171 c = QUOTE; | |
172 break; | |
173 case '\\': | |
174 c = SLASH; | |
175 break; | |
176 case '/': | |
177 c = BACKSLASH; | |
178 break; | |
179 case 'b': | |
180 c = BACKSPACE; | |
181 break; | |
182 case 'n': | |
183 c = NEW_LINE; | |
184 break; | |
185 case 'r': | |
186 c = LINE_FEED; | |
187 break; | |
188 case 'f': | |
189 c = FORM_FEED; | |
190 break; | |
191 case 't': | |
192 c = TAB; | |
193 break; | |
194 case 'u': | |
195 if (_pos + 5 > _len) { | |
196 throw 'Invalid unicode esacape sequence: \\' + | |
197 _s.substring(_pos, _len); | |
198 } | |
199 final codeString = _s.substring(_pos + 1, _pos + 5); | |
200 c = Math.parseInt('0x' + codeString); | |
201 if (c >= 128) { | |
202 // TODO(jmessery): the VM doesn't support 2-byte strings yet | |
203 // see runtime/lib/string.cc:49 | |
204 // So instead we replace these characters with '?' | |
205 c = '?'.charCodeAt(0); | |
206 } | |
207 _pos += 4; | |
208 break; | |
209 default: | |
210 throw 'Invalid esacape sequence: \\' + _s[_pos]; | |
211 } | |
212 } | |
213 charCodes.add(c); | |
214 _pos++; | |
215 } | |
216 if (_pos == _len) { | |
217 throw 'Unmatched quote'; | |
218 } | |
219 | |
220 final String body = new String.fromCharCodes(charCodes); | |
221 _pos++; | |
222 return new JsonToken.string(body); | |
223 | |
224 case cur == MINUS || isDigit(cur): | |
225 skipDigits() { | |
226 _scanWhile((int c) => isDigit(c), 'Invalid number'); | |
227 } | |
228 | |
229 final int startPos = _pos; | |
230 bool isInteger = true; | |
231 _pos++; | |
232 skipDigits(); | |
233 | |
234 int c = _s.charCodeAt(_pos); | |
235 if (c == DOT) { | |
236 isInteger = false; | |
237 _pos++; | |
238 skipDigits(); | |
239 c = _s.charCodeAt(_pos); | |
240 } | |
241 | |
242 if (c == E_SMALL || c == E_BIG) { | |
243 // TODO: consider keeping E+ as an integer. | |
244 isInteger = false; | |
245 _pos++; | |
246 c = _s.charCodeAt(_pos); | |
247 if (c == PLUS || c == MINUS) { | |
248 _pos++; | |
249 } | |
250 skipDigits(); | |
251 } | |
252 | |
253 final String body = _s.substring(startPos, _pos); | |
254 return new JsonToken.number( | |
255 isInteger ? Math.parseInt(body) : Math.parseDouble(body)); | |
256 | |
257 case cur == LBRACE: | |
258 _pos++; | |
259 return new JsonToken.atom(JsonToken.LBRACE); | |
260 | |
261 case cur == RBRACE: | |
262 _pos++; | |
263 return new JsonToken.atom(JsonToken.RBRACE); | |
264 | |
265 case cur == LBRACKET: | |
266 _pos++; | |
267 return new JsonToken.atom(JsonToken.LBRACKET); | |
268 | |
269 case cur == RBRACKET: | |
270 _pos++; | |
271 return new JsonToken.atom(JsonToken.RBRACKET); | |
272 | |
273 case cur == COMMA: | |
274 _pos++; | |
275 return new JsonToken.atom(JsonToken.COMMA); | |
276 | |
277 case cur == COLON: | |
278 _pos++; | |
279 return new JsonToken.atom(JsonToken.COLON); | |
280 | |
281 case isLetter(cur): | |
282 final int startPos = _pos; | |
283 _pos++; | |
284 while (_pos < _len && isLetter(_s.charCodeAt(_pos))) { | |
285 _pos++; | |
286 } | |
287 final String body = _s.substring(startPos, _pos); | |
288 switch (body) { | |
289 case 'null': | |
290 return new JsonToken.atom(JsonToken.NULL); | |
291 | |
292 case 'false': | |
293 return new JsonToken.atom(JsonToken.FALSE); | |
294 | |
295 case 'true': | |
296 return new JsonToken.atom(JsonToken.TRUE); | |
297 | |
298 default: | |
299 throw 'Unexpected sequence ${body}'; | |
300 } | |
301 // TODO: Bogous, to please DartVM. | |
302 return null; | |
303 | |
304 default: | |
305 throw 'Invalid token'; | |
306 } | |
307 } | |
308 | |
309 final String _s; | |
310 int _pos; | |
311 final int _len; | |
312 | |
313 void _scanWhile(Predicate predicate, String errorMsg) { | |
314 while (_pos < _len && predicate(_s.charCodeAt(_pos))) { | |
315 _pos++; | |
316 } | |
317 if (_pos == _len) { | |
318 throw errorMsg; | |
319 } | |
320 } | |
321 | |
322 // TODO other kind of whitespace. | |
323 static bool isWhitespace(int c) { | |
324 return c == SPACE || c == TAB || c == NEW_LINE || c == LINE_FEED; | |
325 } | |
326 static bool isDigit(int c) { | |
327 return (ZERO <= c) && (c <= NINE); | |
328 } | |
329 static bool isLetter(int c) { | |
330 return ((A_SMALL <= c) && (c <= Z_SMALL)) || ((A_BIG <= c) && (c <= Z_BIG))
; | |
331 } | |
332 } | |
333 | |
334 class JsonParser { | |
335 static parse(String json) { | |
336 return new JsonParser._internal(json)._parseToplevel(); | |
337 } | |
338 | |
339 final JsonTokenizer _tokenizer; | |
340 | |
341 JsonParser._internal(String json) : _tokenizer = new JsonTokenizer(json) {} | |
342 | |
343 _parseToplevel() { | |
344 JsonToken token = _tokenizer.next(); | |
345 final result = _parseValue(token); | |
346 token = _tokenizer.next(); | |
347 if (token !== null) { | |
348 throw 'Junk at the end'; | |
349 } | |
350 return result; | |
351 } | |
352 | |
353 _parseValue(final JsonToken token) { | |
354 if (token === null) { | |
355 throw 'Nothing to parse'; | |
356 } | |
357 switch (token.kind) { | |
358 case JsonToken.STRING: | |
359 return token.str; | |
360 | |
361 case JsonToken.NUMBER: | |
362 return token.number; | |
363 | |
364 case JsonToken.NULL: | |
365 return null; | |
366 | |
367 case JsonToken.FALSE: | |
368 return false; | |
369 | |
370 case JsonToken.TRUE: | |
371 return true; | |
372 | |
373 case JsonToken.LBRACE: | |
374 return _parseObject(); | |
375 | |
376 case JsonToken.LBRACKET: | |
377 return _parseArray(); | |
378 | |
379 default: | |
380 throw 'Unexpected token: ${token}'; | |
381 } | |
382 } | |
383 | |
384 _parseObject() { | |
385 Map<String, Object> object = new Map<String, Object>(); | |
386 | |
387 _parseSequence(JsonToken.RBRACE, (JsonToken token) { | |
388 _assertTokenKind(token, JsonToken.STRING); | |
389 | |
390 final String key = token.str; | |
391 | |
392 token = _tokenizer.next(); | |
393 _assertTokenKind(token, JsonToken.COLON); | |
394 | |
395 token = _tokenizer.next(); | |
396 final value = _parseValue(token); | |
397 | |
398 object[key] = value; | |
399 }); | |
400 | |
401 return object; | |
402 } | |
403 | |
404 _parseArray() { | |
405 Array<Object> array = new Array<Object>(); | |
406 | |
407 _parseSequence(JsonToken.RBRACKET, (JsonToken token) { | |
408 final value = _parseValue(token); | |
409 array.add(value); | |
410 }); | |
411 | |
412 return array; | |
413 } | |
414 | |
415 void _parseSequence(int endTokenKind, void parseElement(JsonToken token)) { | |
416 JsonToken token = _tokenizer.next(); | |
417 if (token === null) { | |
418 throw 'Unexpected end of stream'; | |
419 } | |
420 if (token.kind == endTokenKind) { | |
421 return; | |
422 } | |
423 | |
424 parseElement(token); | |
425 | |
426 token = _tokenizer.next(); | |
427 if (token === null) { | |
428 throw 'Expected either comma or terminator'; | |
429 } | |
430 while (token.kind != endTokenKind) { | |
431 _assertTokenKind(token, JsonToken.COMMA); | |
432 | |
433 token = _tokenizer.next(); | |
434 parseElement(token); | |
435 | |
436 token = _tokenizer.next(); | |
437 } | |
438 } | |
439 | |
440 void _assertTokenKind(JsonToken token, int kind) { | |
441 if (token === null || token.kind != kind) { | |
442 throw 'Unexpected token kind: token = ${token}, expected kind = ${kind}'; | |
443 } | |
444 } | |
445 | |
446 // TODO: consider factor out error throwing code and build more complicated | |
447 // data structure to provide more info for a caller. | |
448 } | |
449 | |
450 // TODO: proper base class. | |
451 class JsonUnsupportedObjectType { | |
452 const JsonUnsupportedObjectType(); | |
453 } | |
454 | |
455 class JsonStringifier { | |
456 static String stringify(final object) { | |
457 JsonStringifier stringifier = new JsonStringifier._internal(); | |
458 | |
459 stringifier._stringify(object); | |
460 /* | |
461 try { | |
462 stringifier._stringify(object); | |
463 } catch (JsonUnsupportedObjectType e) { | |
464 return null; | |
465 }*/ | |
466 return stringifier._result; | |
467 } | |
468 | |
469 JsonStringifier._internal() | |
470 : _sb = new StringBuffer(), _seen = new Array<Object>() {} | |
471 StringBuffer _sb; | |
472 Array<Object> _seen; // TODO: that should be identity set. | |
473 String get _result() { return _sb.toString(); } | |
474 | |
475 static String _numberToString(num x) { | |
476 // TODO: need some more investigation what to do with precision | |
477 // of double values. | |
478 switch (true) { | |
479 case x is int: | |
480 return x.toString(); | |
481 | |
482 case x is double: | |
483 return x.toString(); | |
484 | |
485 default: | |
486 return x.toDouble().toString(); | |
487 } | |
488 } | |
489 | |
490 // TODO: add others. | |
491 static bool _needsEscape(int charCode) { | |
492 return JsonTokenizer.QUOTE == charCode || JsonTokenizer.SLASH == charCode; | |
493 } | |
494 | |
495 static void _escape(StringBuffer sb, String s) { | |
496 // TODO: support \u code points. | |
497 // TODO: use writeCodePoint when implemented. | |
498 // TODO: use for each if implemented. | |
499 final int length = s.length; | |
500 bool needsEscape = false; | |
501 final charCodes = new Array<int>(); | |
502 for (int i = 0; i < length; i++) { | |
503 final int charCode = s.charCodeAt(i); | |
504 if (_needsEscape(charCode)) { | |
505 charCodes.add(JsonTokenizer.SLASH); | |
506 needsEscape = true; | |
507 } | |
508 charCodes.add(charCode); | |
509 } | |
510 sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s); | |
511 } | |
512 | |
513 void _checkCycle(final object) { | |
514 // TODO: use Iterables. | |
515 for (int i = 0; i < _seen.length; i++) { | |
516 if (_seen[i] === object) { | |
517 throw 'Cyclic structure'; | |
518 } | |
519 } | |
520 _seen.add(object); | |
521 } | |
522 | |
523 void _stringify(final object) { | |
524 switch (true) { | |
525 case object is num: | |
526 // TODO: use writeOn. | |
527 _sb.add(_numberToString(object)); | |
528 return; | |
529 | |
530 case object === true: | |
531 _sb.add('true'); | |
532 return; | |
533 | |
534 case object === false: | |
535 _sb.add('false'); | |
536 return; | |
537 | |
538 case object === null: | |
539 _sb.add('null'); | |
540 return; | |
541 | |
542 case object is String: | |
543 _sb.add('"'); | |
544 _escape(_sb, object); | |
545 _sb.add('"'); | |
546 return; | |
547 | |
548 case object is Array: | |
549 _checkCycle(object); | |
550 Array a = object; | |
551 _sb.add('['); | |
552 if (a.length > 0) { | |
553 _stringify(a[0]); | |
554 } | |
555 // TODO: switch to Iterables. | |
556 for (int i = 1; i < a.length; i++) { | |
557 _sb.add(','); | |
558 _stringify(a[i]); | |
559 } | |
560 _sb.add(']'); | |
561 return; | |
562 | |
563 case object is Map: | |
564 _checkCycle(object); | |
565 Map<String, Object> m = object; | |
566 _sb.add('{'); | |
567 int counter = m.length; | |
568 m.forEach((String key, Object value) { | |
569 _stringify(key); | |
570 _sb.add(':'); | |
571 _stringify(value); | |
572 counter--; | |
573 if (counter != 0) { | |
574 _sb.add(','); | |
575 } | |
576 }); | |
577 _sb.add('}'); | |
578 return; | |
579 | |
580 default: | |
581 throw const JsonUnsupportedObjectType(); | |
582 } | |
583 } | |
584 } | |
OLD | NEW |