| 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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 static final int E_SMALL = 101; // 'e'.charCodeAt(0) | 136 static final int E_SMALL = 101; // 'e'.charCodeAt(0) |
| 137 static final int F_SMALL = 102; // 'f'.charCodeAt(0) | 137 static final int F_SMALL = 102; // 'f'.charCodeAt(0) |
| 138 static final int N_SMALL = 110; // 'n'.charCodeAt(0) | 138 static final int N_SMALL = 110; // 'n'.charCodeAt(0) |
| 139 static final int R_SMALL = 114; // 'r'.charCodeAt(0) | 139 static final int R_SMALL = 114; // 'r'.charCodeAt(0) |
| 140 static final int T_SMALL = 116; // 't'.charCodeAt(0) | 140 static final int T_SMALL = 116; // 't'.charCodeAt(0) |
| 141 static final int U_SMALL = 117; // 'u'.charCodeAt(0) | 141 static final int U_SMALL = 117; // 'u'.charCodeAt(0) |
| 142 static final int Z_SMALL = 122; // 'z'.charCodeAt(0) | 142 static final int Z_SMALL = 122; // 'z'.charCodeAt(0) |
| 143 static final int LBRACE = 123; // '{'.charCodeAt(0) | 143 static final int LBRACE = 123; // '{'.charCodeAt(0) |
| 144 static final int RBRACE = 125; // '}'.charCodeAt(0) | 144 static final int RBRACE = 125; // '}'.charCodeAt(0) |
| 145 | 145 |
| 146 JsonTokenizer(String s) : _s = s + ' ', _pos = 0, _len = s.length + 1 {} | 146 JsonTokenizer(String s) : _s = '${s} ', _pos = 0, _len = s.length + 1; |
| 147 | 147 |
| 148 /** | 148 /** |
| 149 * Fetches next token or [:null:] if the stream has been exhausted. | 149 * Fetches next token or [:null:] if the stream has been exhausted. |
| 150 */ | 150 */ |
| 151 JsonToken next() { | 151 JsonToken next() { |
| 152 while (_pos < _len && isWhitespace(_s.charCodeAt(_pos))) { | 152 while (_pos < _len && isWhitespace(_s.charCodeAt(_pos))) { |
| 153 _pos++; | 153 _pos++; |
| 154 } | 154 } |
| 155 if (_pos == _len) { | 155 if (_pos == _len) { |
| 156 return null; | 156 return null; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 c = CARRIAGE_RETURN; | 192 c = CARRIAGE_RETURN; |
| 193 break; | 193 break; |
| 194 case 'f': | 194 case 'f': |
| 195 c = FORM_FEED; | 195 c = FORM_FEED; |
| 196 break; | 196 break; |
| 197 case 't': | 197 case 't': |
| 198 c = TAB; | 198 c = TAB; |
| 199 break; | 199 break; |
| 200 case 'u': | 200 case 'u': |
| 201 if (_pos + 5 > _len) { | 201 if (_pos + 5 > _len) { |
| 202 throw 'Invalid unicode esacape sequence: \\' + | 202 throw 'Invalid unicode esacape sequence:' |
| 203 _s.substring(_pos, _len); | 203 '\\${_s.substring(_pos, _len)}'; |
| 204 } | 204 } |
| 205 final codeString = _s.substring(_pos + 1, _pos + 5); | 205 final codeString = _s.substring(_pos + 1, _pos + 5); |
| 206 c = Math.parseInt('0x' + codeString); | 206 c = Math.parseInt('0x${codeString}'); |
| 207 if (c >= 128) { | 207 if (c >= 128) { |
| 208 // TODO(jmessery): the VM doesn't support 2-byte strings yet | 208 // TODO(jmessery): the VM doesn't support 2-byte strings yet |
| 209 // see runtime/lib/string.cc:49 | 209 // see runtime/lib/string.cc:49 |
| 210 // So instead we replace these characters with '?' | 210 // So instead we replace these characters with '?' |
| 211 c = '?'.charCodeAt(0); | 211 c = '?'.charCodeAt(0); |
| 212 } | 212 } |
| 213 _pos += 4; | 213 _pos += 4; |
| 214 break; | 214 break; |
| 215 default: | 215 default: |
| 216 throw 'Invalid esacape sequence: \\' + _s[_pos]; | 216 throw 'Invalid esacape sequence: \\${_s[_pos]}'; |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 charCodes.add(c); | 219 charCodes.add(c); |
| 220 _pos++; | 220 _pos++; |
| 221 } | 221 } |
| 222 if (_pos == _len) { | 222 if (_pos == _len) { |
| 223 throw 'Unmatched quote'; | 223 throw 'Unmatched quote'; |
| 224 } | 224 } |
| 225 | 225 |
| 226 final String body = new String.fromCharCodes(charCodes); | 226 final String body = new String.fromCharCodes(charCodes); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 }); | 613 }); |
| 614 _sb.add('}'); | 614 _sb.add('}'); |
| 615 _seen.removeLast(); | 615 _seen.removeLast(); |
| 616 return; | 616 return; |
| 617 | 617 |
| 618 default: | 618 default: |
| 619 throw const JsonUnsupportedObjectType(); | 619 throw const JsonUnsupportedObjectType(); |
| 620 } | 620 } |
| 621 } | 621 } |
| 622 } | 622 } |
| OLD | NEW |