Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * Utilities for encoding and decoding JSON (JavaScript Object Notation) data. | 6 * Utilities for encoding and decoding JSON (JavaScript Object Notation) data. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 library dart.json; | 9 library dart.json; |
| 10 | 10 |
| 11 import "dart:convert"; | |
| 12 | |
| 11 // JSON parsing and serialization. | 13 // JSON parsing and serialization. |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * Error thrown by JSON serialization if an object cannot be serialized. | 16 * Error thrown by JSON serialization if an object cannot be serialized. |
| 15 * | 17 * |
| 16 * The [unsupportedObject] field holds that object that failed to be serialized. | 18 * The [unsupportedObject] field holds that object that failed to be serialized. |
| 17 * | 19 * |
| 18 * If an object isn't directly serializable, the serializer calls the 'toJson' | 20 * If an object isn't directly serializable, the serializer calls the 'toJson' |
| 19 * method on the object. If that call fails, the error will be stored in the | 21 * method on the object. If that call fails, the error will be stored in the |
| 20 * [cause] field. If the call returns an object that isn't directly | 22 * [cause] field. If the call returns an object that isn't directly |
| 21 * serializable, the [cause] will be null. | 23 * serializable, the [cause] will be null. |
| 22 */ | 24 */ |
| 23 class JsonUnsupportedObjectError extends Error { | 25 class JsonUnsupportedObjectError extends Error { |
|
floitsch
2013/08/27 13:27:50
Didn't you want to reexport these?
Otherwise a use
Lasse Reichstein Nielsen
2013/08/28 08:02:59
Absolutely. Wonder where the export statement got
| |
| 24 /** The object that could not be serialized. */ | 26 /** The object that could not be serialized. */ |
| 25 final unsupportedObject; | 27 final unsupportedObject; |
| 26 /** The exception thrown by object's [:toJson:] method, if any. */ | 28 /** The exception thrown by object's [:toJson:] method, if any. */ |
| 27 final cause; | 29 final cause; |
| 28 | 30 |
| 29 JsonUnsupportedObjectError(this.unsupportedObject, { this.cause }); | 31 JsonUnsupportedObjectError(this.unsupportedObject, { this.cause }); |
| 30 | 32 |
| 31 String toString() { | 33 String toString() { |
| 32 if (cause != null) { | 34 if (cause != null) { |
| 33 return "Calling toJson method on object failed."; | 35 return "Calling toJson method on object failed."; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 59 * JSON values. | 61 * JSON values. |
| 60 * | 62 * |
| 61 * The optional [reviver] function, if provided, is called once for each | 63 * The optional [reviver] function, if provided, is called once for each |
| 62 * object or list property parsed. The arguments are the property name | 64 * object or list property parsed. The arguments are the property name |
| 63 * ([String]) or list index ([int]), and the value is the parsed value. | 65 * ([String]) or list index ([int]), and the value is the parsed value. |
| 64 * The return value of the reviver will be used as the value of that property | 66 * The return value of the reviver will be used as the value of that property |
| 65 * instead the parsed value. | 67 * instead the parsed value. |
| 66 * | 68 * |
| 67 * Throws [FormatException] if the input is not valid JSON text. | 69 * Throws [FormatException] if the input is not valid JSON text. |
| 68 */ | 70 */ |
| 69 external parse(String json, [reviver(var key, var value)]); | 71 parse(String json, [reviver(var key, var value)]) { |
| 70 | 72 if (reviver != null) { |
| 71 _parse(String json, reviver(var key, var value)) { | 73 var original = reviver; |
| 72 BuildJsonListener listener; | 74 reviver = (key, value) => original(key == null ? "" : key, value); |
|
floitsch
2013/08/27 13:25:40
Again: let's make this in a separate CL.
Lasse Reichstein Nielsen
2013/08/28 08:02:59
Done.
| |
| 73 if (reviver == null) { | |
| 74 listener = new BuildJsonListener(); | |
| 75 } else { | |
| 76 listener = new ReviverJsonListener(reviver); | |
| 77 } | 75 } |
| 78 new JsonParser(json, listener).parse(); | 76 return JSON.decode(json, reviver: reviver); |
| 79 return listener.result; | |
| 80 } | 77 } |
| 81 | 78 |
| 82 /** | 79 /** |
| 83 * Serializes [object] into a JSON string. | 80 * Serializes [object] into a JSON string. |
| 84 * | 81 * |
| 85 * Directly serializable values are [num], [String], [bool], and [Null], as well | 82 * Directly serializable values are [num], [String], [bool], and [Null], as well |
| 86 * as some [List] and [Map] values. | 83 * as some [List] and [Map] values. |
| 87 * For [List], the elements must all be serializable. | 84 * For [List], the elements must all be serializable. |
| 88 * For [Map], the keys must be [String] and the values must be serializable. | 85 * For [Map], the keys must be [String] and the values must be serializable. |
| 89 * | 86 * |
| (...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 821 first = false; | 818 first = false; |
| 822 }); | 819 }); |
| 823 sink.write('}'); | 820 sink.write('}'); |
| 824 seen.removeLast(); | 821 seen.removeLast(); |
| 825 return true; | 822 return true; |
| 826 } else { | 823 } else { |
| 827 return false; | 824 return false; |
| 828 } | 825 } |
| 829 } | 826 } |
| 830 } | 827 } |
| OLD | NEW |