OLD | NEW |
---|---|
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 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 case x is double: | 486 case x is double: |
487 return x.toString(); | 487 return x.toString(); |
488 | 488 |
489 default: | 489 default: |
490 return x.toDouble().toString(); | 490 return x.toDouble().toString(); |
491 } | 491 } |
492 } | 492 } |
493 | 493 |
494 // TODO: add others. | 494 // TODO: add others. |
495 static bool _needsEscape(int charCode) { | 495 static bool _needsEscape(int charCode) { |
496 return JsonTokenizer.QUOTE == charCode || JsonTokenizer.BACKSLASH == charCod e; | 496 return JsonTokenizer.QUOTE == charCode || JsonTokenizer.BACKSLASH == charCod e |
497 || JsonTokenizer.NEW_LINE == charCode || JsonTokenizer.LINE_FEED == charCo de; | |
497 } | 498 } |
498 | 499 |
499 static void _escape(StringBuffer sb, String s) { | 500 static void _escape(StringBuffer sb, String s) { |
500 // TODO: support \u code points. | 501 // TODO: support \u code points. |
501 // TODO: use writeCodePoint when implemented. | 502 // TODO: use writeCodePoint when implemented. |
502 // TODO: use for each if implemented. | 503 // TODO: use for each if implemented. |
503 final int length = s.length; | 504 final int length = s.length; |
504 bool needsEscape = false; | 505 bool needsEscape = false; |
505 final charCodes = new List<int>(); | 506 final charCodes = new List<int>(); |
506 for (int i = 0; i < length; i++) { | 507 for (int i = 0; i < length; i++) { |
507 final int charCode = s.charCodeAt(i); | 508 int charCode = s.charCodeAt(i); |
508 if (_needsEscape(charCode)) { | 509 if (_needsEscape(charCode)) { |
509 charCodes.add(JsonTokenizer.BACKSLASH); | 510 charCodes.add(JsonTokenizer.BACKSLASH); |
510 needsEscape = true; | 511 needsEscape = true; |
512 | |
513 if (JsonTokenizer.NEW_LINE == charCode) { | |
514 charCode = 110; // 'n' | |
Anton Muhin
2012/01/12 13:23:00
as an option see A_SMALL above. Ditto for r
devoncarew
2012/01/12 21:28:11
Done.
| |
515 } else if (JsonTokenizer.LINE_FEED == charCode) { | |
516 charCode = 114; // 'r' | |
517 } | |
511 } | 518 } |
512 charCodes.add(charCode); | 519 charCodes.add(charCode); |
513 } | 520 } |
514 sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s); | 521 sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s); |
515 } | 522 } |
516 | 523 |
517 void _checkCycle(final object) { | 524 void _checkCycle(final object) { |
518 // TODO: use Iterables. | 525 // TODO: use Iterables. |
519 for (int i = 0; i < _seen.length; i++) { | 526 for (int i = 0; i < _seen.length; i++) { |
520 if (_seen[i] === object) { | 527 if (_seen[i] === object) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 }); | 590 }); |
584 _sb.add('}'); | 591 _sb.add('}'); |
585 _seen.removeLast(); | 592 _seen.removeLast(); |
586 return; | 593 return; |
587 | 594 |
588 default: | 595 default: |
589 throw const JsonUnsupportedObjectType(); | 596 throw const JsonUnsupportedObjectType(); |
590 } | 597 } |
591 } | 598 } |
592 } | 599 } |
OLD | NEW |