| 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. |
| 11 */ | 11 */ |
| 12 class JSON { | 12 class JSON { |
| 13 /** | 13 /** |
| 14 * Parses [:json:] and build the corresponding object. | 14 * Parses [:json:] and build the corresponding object. |
| 15 */ | 15 */ |
| 16 static parse(String json) { | 16 static parse(String json) { |
| 17 return _JsonParser.parse(json); | 17 return _JsonParser.parse(json); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Serializes [:object:] into JSON string. | 21 * Serializes [:object:] into JSON string. |
| 22 */ | 22 */ |
| 23 static String stringify(Object object) { | 23 static String stringify(Object object) { |
| 24 return JsonStringifier.stringify(object); | 24 return JsonStringifier.stringify(object); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 //// Implementation /////////////////////////////////////////////////////////// | 28 //// Implementation /////////////////////////////////////////////////////////// |
| 29 | 29 |
| 30 // TODO(ajohnsen): Introduce when we have a common exception interface for json. |
| 30 class JSONParseException { | 31 class JSONParseException { |
| 31 JSONParseException(int position, String message) : | 32 JSONParseException(int position, String message) : |
| 32 position = position, | 33 position = position, |
| 33 message = 'JSONParseException: $message, at offset $position'; | 34 message = 'JSONParseException: $message, at offset $position'; |
| 34 | 35 |
| 35 String toString() => message; | 36 String toString() => message; |
| 36 | 37 |
| 37 final String message; | 38 final String message; |
| 38 final int position; | 39 final int position; |
| 39 } | 40 } |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 if (token === WHITESPACE) { | 343 if (token === WHITESPACE) { |
| 343 position++; | 344 position++; |
| 344 continue; | 345 continue; |
| 345 } | 346 } |
| 346 if (token === null) _error("Invalid JSON token"); | 347 if (token === null) _error("Invalid JSON token"); |
| 347 return token; | 348 return token; |
| 348 } | 349 } |
| 349 } | 350 } |
| 350 | 351 |
| 351 void _error(String message) { | 352 void _error(String message) { |
| 352 throw new JSONParseException(position, message); | 353 throw message; |
| 353 } | 354 } |
| 354 | 355 |
| 355 final String json; | 356 final String json; |
| 356 int position = 0; | 357 int position = 0; |
| 357 static List<int> tokens; | 358 static List<int> tokens; |
| 358 } | 359 } |
| 359 | 360 |
| 360 // TODO: proper base class. | 361 // TODO: proper base class. |
| 361 class JsonUnsupportedObjectType { | 362 class JsonUnsupportedObjectType { |
| 362 const JsonUnsupportedObjectType(); | 363 const JsonUnsupportedObjectType(); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 }); | 508 }); |
| 508 _sb.add('}'); | 509 _sb.add('}'); |
| 509 _seen.removeLast(); | 510 _seen.removeLast(); |
| 510 return; | 511 return; |
| 511 | 512 |
| 512 default: | 513 default: |
| 513 throw const JsonUnsupportedObjectType(); | 514 throw const JsonUnsupportedObjectType(); |
| 514 } | 515 } |
| 515 } | 516 } |
| 516 } | 517 } |
| OLD | NEW |