Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: client/json/dart_json.dart

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

Powered by Google App Engine
This is Rietveld 408576698