OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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("dart:json"); | 5 #library("dart:json"); |
6 | 6 |
| 7 #import('dart:math'); |
| 8 |
7 // Pure Dart implementation of JSON protocol. | 9 // Pure Dart implementation of JSON protocol. |
8 | 10 |
9 /** | 11 /** |
10 * Utility class to parse JSON and serialize objects to JSON. | 12 * Utility class to parse JSON and serialize objects to JSON. |
11 */ | 13 */ |
12 class JSON { | 14 class JSON { |
13 /** | 15 /** |
14 * Parses [json] and build the corresponding object. | 16 * Parses [json] and build the corresponding object. |
15 */ | 17 */ |
16 static parse(String json) { | 18 static parse(String json) { |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 break; | 284 break; |
283 case CHAR_T: | 285 case CHAR_T: |
284 c = TAB; | 286 c = TAB; |
285 break; | 287 break; |
286 case CHAR_U: | 288 case CHAR_U: |
287 if (position + 5 > length) { | 289 if (position + 5 > length) { |
288 _error('Invalid unicode esacape sequence'); | 290 _error('Invalid unicode esacape sequence'); |
289 } | 291 } |
290 final codeString = json.substring(position + 1, position + 5); | 292 final codeString = json.substring(position + 1, position + 5); |
291 try { | 293 try { |
292 c = Math.parseInt('0x${codeString}'); | 294 c = parseInt('0x${codeString}'); |
293 } catch (var e) { | 295 } catch (var e) { |
294 _error('Invalid unicode esacape sequence'); | 296 _error('Invalid unicode esacape sequence'); |
295 } | 297 } |
296 position += 4; | 298 position += 4; |
297 break; | 299 break; |
298 default: | 300 default: |
299 _error('Invalid esacape sequence in string literal'); | 301 _error('Invalid esacape sequence in string literal'); |
300 } | 302 } |
301 } | 303 } |
302 charCodes.add(c); | 304 charCodes.add(c); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 char = _nextChar(); | 342 char = _nextChar(); |
341 isInt = false; | 343 isInt = false; |
342 while (_isDigit(char)) char = _nextChar(); | 344 while (_isDigit(char)) char = _nextChar(); |
343 } else { | 345 } else { |
344 _error('Expected digit following \'e\' or \'E\''); | 346 _error('Expected digit following \'e\' or \'E\''); |
345 } | 347 } |
346 } | 348 } |
347 | 349 |
348 String number = json.substring(startPos, position); | 350 String number = json.substring(startPos, position); |
349 if (isInt) { | 351 if (isInt) { |
350 return Math.parseInt(number); | 352 return parseInt(number); |
351 } else { | 353 } else { |
352 return Math.parseDouble(number); | 354 return parseDouble(number); |
353 } | 355 } |
354 } | 356 } |
355 | 357 |
356 bool _isChar(int char) { | 358 bool _isChar(int char) { |
357 if (position >= length) return false; | 359 if (position >= length) return false; |
358 return json.charCodeAt(position) == char; | 360 return json.charCodeAt(position) == char; |
359 } | 361 } |
360 | 362 |
361 bool _isDigit(int char) { | 363 bool _isDigit(int char) { |
362 return char >= CHAR_0 && char <= CHAR_9; | 364 return char >= CHAR_0 && char <= CHAR_9; |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 first = false; | 548 first = false; |
547 }); | 549 }); |
548 _sb.add('}'); | 550 _sb.add('}'); |
549 _seen.removeLast(); | 551 _seen.removeLast(); |
550 return; | 552 return; |
551 } else { | 553 } else { |
552 throw const JsonUnsupportedObjectType(); | 554 throw const JsonUnsupportedObjectType(); |
553 } | 555 } |
554 } | 556 } |
555 } | 557 } |
OLD | NEW |