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 // TODO(devoncarew): this file is a copy of client/json/dart_json.dart and shoul
d be deleted as | 7 // TODO(devoncarew): this file is a copy of client/json/dart_json.dart and shoul
d be deleted as |
8 // soon as it is no longer referenced from frog_server.dart. | 8 // soon as it is no longer referenced from frog_server.dart. |
9 | 9 |
10 // Pure Dart implementation of JSON protocol. | 10 // Pure Dart implementation of JSON protocol. |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 case x is double: | 489 case x is double: |
490 return x.toString(); | 490 return x.toString(); |
491 | 491 |
492 default: | 492 default: |
493 return x.toDouble().toString(); | 493 return x.toDouble().toString(); |
494 } | 494 } |
495 } | 495 } |
496 | 496 |
497 // TODO: add others. | 497 // TODO: add others. |
498 static bool _needsEscape(int charCode) { | 498 static bool _needsEscape(int charCode) { |
499 return JsonTokenizer.QUOTE == charCode || JsonTokenizer.BACKSLASH == charCod
e; | 499 return JsonTokenizer.QUOTE == charCode || JsonTokenizer.BACKSLASH == charCod
e |
| 500 || JsonTokenizer.NEW_LINE == charCode || JsonTokenizer.LINE_FEED == charCo
de; |
500 } | 501 } |
501 | 502 |
502 static void _escape(StringBuffer sb, String s) { | 503 static void _escape(StringBuffer sb, String s) { |
503 // TODO: support \u code points. | 504 // TODO: support \u code points. |
504 // TODO: use writeCodePoint when implemented. | 505 // TODO: use writeCodePoint when implemented. |
505 // TODO: use for each if implemented. | 506 // TODO: use for each if implemented. |
506 final int length = s.length; | 507 final int length = s.length; |
507 bool needsEscape = false; | 508 bool needsEscape = false; |
508 final charCodes = new List<int>(); | 509 final charCodes = new List<int>(); |
509 for (int i = 0; i < length; i++) { | 510 for (int i = 0; i < length; i++) { |
510 final int charCode = s.charCodeAt(i); | 511 int charCode = s.charCodeAt(i); |
511 if (_needsEscape(charCode)) { | 512 if (_needsEscape(charCode)) { |
512 charCodes.add(JsonTokenizer.BACKSLASH); | 513 charCodes.add(JsonTokenizer.BACKSLASH); |
513 needsEscape = true; | 514 needsEscape = true; |
| 515 |
| 516 if (JsonTokenizer.NEW_LINE == charCode) { |
| 517 charCode = 110; // 'n' |
| 518 } else if (JsonTokenizer.LINE_FEED == charCode) { |
| 519 charCode = 114; // 'r' |
| 520 } |
514 } | 521 } |
515 charCodes.add(charCode); | 522 charCodes.add(charCode); |
516 } | 523 } |
517 sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s); | 524 sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s); |
518 } | 525 } |
519 | 526 |
520 void _checkCycle(final object) { | 527 void _checkCycle(final object) { |
521 // TODO: use Iterables. | 528 // TODO: use Iterables. |
522 for (int i = 0; i < _seen.length; i++) { | 529 for (int i = 0; i < _seen.length; i++) { |
523 if (_seen[i] === object) { | 530 if (_seen[i] === object) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 }); | 593 }); |
587 _sb.add('}'); | 594 _sb.add('}'); |
588 _seen.removeLast(); | 595 _seen.removeLast(); |
589 return; | 596 return; |
590 | 597 |
591 default: | 598 default: |
592 throw const JsonUnsupportedObjectType(); | 599 throw const JsonUnsupportedObjectType(); |
593 } | 600 } |
594 } | 601 } |
595 } | 602 } |
OLD | NEW |