| 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 * Checks validity of JSON source in [:str:] and returns its text | 21 * Checks validity of JSON source in [str] and returns its text |
| 22 * length. Returns 0 if [:str:] does not begin with a valid JSON | 22 * length. Returns 0 if [str] does not begin with a valid JSON |
| 23 * object. | 23 * object. |
| 24 */ | 24 */ |
| 25 static int length(String str) { | 25 static int length(String str) { |
| 26 return _JsonParser.objectLength(str); | 26 return _JsonParser.objectLength(str); |
| 27 } | 27 } |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Serializes [:object:] into JSON string. | 30 * Serializes [object] into JSON string. |
| 31 */ | 31 */ |
| 32 static String stringify(Object object) { | 32 static String stringify(Object object) { |
| 33 return JsonStringifier.stringify(object); | 33 return JsonStringifier.stringify(object); |
| 34 } | 34 } |
| 35 | 35 |
| 36 /** | 36 /** |
| 37 * Serializes [:object:] into [:output:] stream. | 37 * Serializes [object] into [output] stream. |
| 38 */ | 38 */ |
| 39 static void printOn(Object object, StringBuffer output) { | 39 static void printOn(Object object, StringBuffer output) { |
| 40 return JsonStringifier.printOn(object, output); | 40 return JsonStringifier.printOn(object, output); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 //// Implementation /////////////////////////////////////////////////////////// | 44 //// Implementation /////////////////////////////////////////////////////////// |
| 45 | 45 |
| 46 // TODO(ajohnsen): Introduce when we have a common exception interface for json. | 46 // TODO(ajohnsen): Introduce when we have a common exception interface for json. |
| 47 class JSONParseException { | 47 class JSONParseException { |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 first = false; | 546 first = false; |
| 547 }); | 547 }); |
| 548 _sb.add('}'); | 548 _sb.add('}'); |
| 549 _seen.removeLast(); | 549 _seen.removeLast(); |
| 550 return; | 550 return; |
| 551 } else { | 551 } else { |
| 552 throw const JsonUnsupportedObjectType(); | 552 throw const JsonUnsupportedObjectType(); |
| 553 } | 553 } |
| 554 } | 554 } |
| 555 } | 555 } |
| OLD | NEW |