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

Side by Side Diff: lib/json/json.dart

Issue 10409003: Eliminate switches that are not on const ints or strings (as required in spec 0.09). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 7 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 | samples/ui_lib/layout/GridLayout.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library("json"); 5 #library("json");
6 6
7 // Pure Dart implementation of JSON protocol. 7 // Pure Dart implementation of JSON protocol.
8 8
9 /** 9 /**
10 * Utility class to parse JSON and serialize objects to JSON. 10 * Utility class to parse JSON and serialize objects to JSON.
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 408
409 JsonStringifier._internal() 409 JsonStringifier._internal()
410 : _sb = new StringBuffer(), _seen = new List<Object>() {} 410 : _sb = new StringBuffer(), _seen = new List<Object>() {}
411 StringBuffer _sb; 411 StringBuffer _sb;
412 List<Object> _seen; // TODO: that should be identity set. 412 List<Object> _seen; // TODO: that should be identity set.
413 String get _result() { return _sb.toString(); } 413 String get _result() { return _sb.toString(); }
414 414
415 static String _numberToString(num x) { 415 static String _numberToString(num x) {
416 // TODO: need some more investigation what to do with precision 416 // TODO: need some more investigation what to do with precision
417 // of double values. 417 // of double values.
418 switch (true) { 418 if (x is int) {
419 case x is int: 419 return x.toString();
420 return x.toString(); 420 } else if (x is double) {
421 421 return x.toString();
422 case x is double: 422 } else {
423 return x.toString(); 423 return x.toDouble().toString();
424
425 default:
426 return x.toDouble().toString();
427 } 424 }
428 } 425 }
429 426
430 // ('0' + x) or ('a' + x - 10) 427 // ('0' + x) or ('a' + x - 10)
431 static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x; 428 static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x;
432 429
433 static void _escape(StringBuffer sb, String s) { 430 static void _escape(StringBuffer sb, String s) {
434 final int length = s.length; 431 final int length = s.length;
435 bool needsEscape = false; 432 bool needsEscape = false;
436 final charCodes = new List<int>(); 433 final charCodes = new List<int>();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 // TODO: use Iterables. 476 // TODO: use Iterables.
480 for (int i = 0; i < _seen.length; i++) { 477 for (int i = 0; i < _seen.length; i++) {
481 if (_seen[i] === object) { 478 if (_seen[i] === object) {
482 throw 'Cyclic structure'; 479 throw 'Cyclic structure';
483 } 480 }
484 } 481 }
485 _seen.add(object); 482 _seen.add(object);
486 } 483 }
487 484
488 void _stringify(final object) { 485 void _stringify(final object) {
489 switch (true) { 486 if (object is num) {
490 case object is num: 487 // TODO: use writeOn.
491 // TODO: use writeOn. 488 _sb.add(_numberToString(object));
492 _sb.add(_numberToString(object)); 489 return;
493 return; 490 } else if (object === true) {
494 491 _sb.add('true');
495 case object === true: 492 return;
496 _sb.add('true'); 493 } else if (object === false) {
497 return; 494 _sb.add('false');
498 495 return;
499 case object === false: 496 } else if (object === null) {
500 _sb.add('false'); 497 _sb.add('null');
501 return; 498 return;
502 499 } else if (object is String) {
503 case object === null: 500 _sb.add('"');
504 _sb.add('null'); 501 _escape(_sb, object);
505 return; 502 _sb.add('"');
506 503 return;
507 case object is String: 504 } else if (object is List) {
508 _sb.add('"'); 505 _checkCycle(object);
509 _escape(_sb, object); 506 List a = object;
510 _sb.add('"'); 507 _sb.add('[');
511 return; 508 if (a.length > 0) {
512 509 _stringify(a[0]);
513 case object is List: 510 // TODO: switch to Iterables.
514 _checkCycle(object); 511 for (int i = 1; i < a.length; i++) {
515 List a = object; 512 _sb.add(',');
516 _sb.add('['); 513 _stringify(a[i]);
517 if (a.length > 0) {
518 _stringify(a[0]);
519 // TODO: switch to Iterables.
520 for (int i = 1; i < a.length; i++) {
521 _sb.add(',');
522 _stringify(a[i]);
523 }
524 } 514 }
525 _sb.add(']'); 515 }
526 _seen.removeLast(); 516 _sb.add(']');
527 return; 517 _seen.removeLast();
528 518 return;
529 case object is Map: 519 } else if (object is Map) {
530 _checkCycle(object); 520 _checkCycle(object);
531 Map<String, Object> m = object; 521 Map<String, Object> m = object;
532 _sb.add('{'); 522 _sb.add('{');
Siggi Cherem (dart-lang) 2012/05/17 18:38:51 strange... why is this line marked as different?
dgrove 2012/05/17 20:05:30 no idea. everything about the line looks right to
533 bool first = true; 523 bool first = true;
534 m.forEach((String key, Object value) { 524 m.forEach((String key, Object value) {
535 if (!first) { 525 if (!first) {
536 _sb.add(',"'); 526 _sb.add(',"');
537 } else { 527 } else {
538 _sb.add('"'); 528 _sb.add('"');
539 } 529 }
540 _escape(_sb, key); 530 _escape(_sb, key);
541 _sb.add('":'); 531 _sb.add('":');
542 _stringify(value); 532 _stringify(value);
543 first = false; 533 first = false;
544 }); 534 });
545 _sb.add('}'); 535 _sb.add('}');
546 _seen.removeLast(); 536 _seen.removeLast();
547 return; 537 return;
548 538 } else {
549 default: 539 throw const JsonUnsupportedObjectType();
550 throw const JsonUnsupportedObjectType();
551 } 540 }
552 } 541 }
553 } 542 }
OLDNEW
« no previous file with comments | « no previous file | samples/ui_lib/layout/GridLayout.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698