Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.convert; | 5 part of dart.convert; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Error thrown by JSON serialization if an object cannot be serialized. | 8 * Error thrown by JSON serialization if an object cannot be serialized. |
| 9 * | 9 * |
| 10 * The [unsupportedObject] field holds that object that failed to be serialized. | 10 * The [unsupportedObject] field holds that object that failed to be serialized. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 * This instance provides a convenient access to the most common JSON | 51 * This instance provides a convenient access to the most common JSON |
| 52 * use cases. | 52 * use cases. |
| 53 * | 53 * |
| 54 * Examples: | 54 * Examples: |
| 55 * | 55 * |
| 56 * var encoded = JSON.encode([1, 2, { "a": null }]); | 56 * var encoded = JSON.encode([1, 2, { "a": null }]); |
| 57 * var decoded = JSON.decode('["foo", { "bar": 499 }]'); | 57 * var decoded = JSON.decode('["foo", { "bar": 499 }]'); |
| 58 */ | 58 */ |
| 59 const JsonCodec JSON = const JsonCodec(); | 59 const JsonCodec JSON = const JsonCodec(); |
| 60 | 60 |
| 61 typedef _Reviver(var key, var value); | |
| 62 typedef _ToEncodable(var o); | |
| 63 | |
| 64 | |
| 61 /** | 65 /** |
| 62 * A [JsonCodec] encodes JSON objects to strings and decodes strings to | 66 * A [JsonCodec] encodes JSON objects to strings and decodes strings to |
| 63 * JSON objects. | 67 * JSON objects. |
| 64 */ | 68 */ |
| 65 class JsonCodec extends Codec<Object, String> { | 69 class JsonCodec extends Codec<Object, String> { |
| 66 const JsonCodec(); | 70 final _Reviver _reviver; |
| 71 final _ToEncodable _toEncodable; | |
| 72 | |
| 73 /** | |
| 74 * Creates a `JsonCodec` with the given reviver and encoding function. | |
| 75 * | |
| 76 * The [reviver] function is called during decoding. It is invoked | |
| 77 * once for each object or list property that has been parsed. | |
| 78 * The `key` argument is either the | |
| 79 * integer list index for a list property, the map string for object | |
|
Lasse Reichstein Nielsen
2014/02/10 07:08:30
map string -> string map key
floitsch
2014/02/10 15:11:30
Done.
| |
| 80 * properties, or `null` for the final result. | |
| 81 * | |
| 82 * If [reviver] is omitted, it defaults to returning the value. | |
|
Lasse Reichstein Nielsen
2014/02/10 07:08:30
... it defaults to a function returning its argume
floitsch
2014/02/10 15:11:30
it defaults to returning the value argument. (sinc
| |
| 83 * | |
| 84 * The [toEncodable] function is used during encoding. It is invoked for | |
| 85 * values that are not directly encodable to a JSON | |
| 86 * string (a value that is not a number, boolean, string, null, list or a map | |
| 87 * with string keys). The function must return an object that is directly | |
| 88 * encodable. | |
| 89 * | |
| 90 * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the | |
|
Lasse Reichstein Nielsen
2014/02/10 07:08:30
... it defaults to a function that returns the res
floitsch
2014/02/10 15:11:30
Done.
| |
| 91 * unencodable object. | |
| 92 */ | |
| 93 const JsonCodec({reviver(var key, var value), toEncodable(var object)}) | |
| 94 : _reviver = reviver, | |
| 95 _toEncodable = toEncodable; | |
| 67 | 96 |
| 68 /** | 97 /** |
| 69 * Creates a `JsonCodec` with the given reviver. | 98 * Creates a `JsonCodec` with the given reviver. |
| 70 * | 99 * |
| 71 * The [reviver] function is called once for each object or list property | 100 * The [reviver] function is called once for each object or list property |
| 72 * that has been parsed during decoding. The `key` argument is either the | 101 * that has been parsed during decoding. The `key` argument is either the |
| 73 * integer list index for a list property, the map string for object | 102 * integer list index for a list property, the map string for object |
| 74 * properties, or `null` for the final result. | 103 * properties, or `null` for the final result. |
| 75 */ | 104 */ |
| 76 factory JsonCodec.withReviver(reviver(var key, var value)) = | 105 JsonCodec.withReviver(reviver(var key, var value)) : this(reviver: reviver); |
| 77 _ReviverJsonCodec; | |
| 78 | 106 |
| 79 /** | 107 /** |
| 80 * Parses the string and returns the resulting Json object. | 108 * Parses the string and returns the resulting Json object. |
| 81 * | 109 * |
| 82 * The optional [reviver] function is called once for each object or list | 110 * The optional [reviver] function is called once for each object or list |
| 83 * property that has been parsed during decoding. The `key` argument is either | 111 * property that has been parsed during decoding. The `key` argument is either |
| 84 * the integer list index for a list property, the map string for object | 112 * the integer list index for a list property, the map string for object |
| 85 * properties, or `null` for the final result. | 113 * properties, or `null` for the final result. |
| 86 * | 114 * |
| 87 * The default [reviver] (when not provided) is the identity function. | 115 * The default [reviver] (when not provided) is the identity function. |
| 88 */ | 116 */ |
| 89 dynamic decode(String source, {reviver(var key, var value)}) { | 117 dynamic decode(String source, {reviver(var key, var value)}) { |
| 118 if (reviver == null) reviver = _reviver; | |
| 90 if (reviver == null) return decoder.convert(source); | 119 if (reviver == null) return decoder.convert(source); |
| 91 return new JsonDecoder(reviver).convert(source); | 120 return new JsonDecoder(reviver).convert(source); |
| 92 } | 121 } |
| 93 | 122 |
| 94 /** | 123 /** |
| 95 * Converts [value] to a JSON string. | 124 * Converts [value] to a JSON string. |
| 96 * | 125 * |
| 97 * If value contains objects that are not directly encodable to a JSON | 126 * If value contains objects that are not directly encodable to a JSON |
| 98 * string (a value that is not a number, boolean, string, null, list or a map | 127 * string (a value that is not a number, boolean, string, null, list or a map |
| 99 * with string keys), the [toEncodable] function is used to convert it to an | 128 * with string keys), the [toEncodable] function is used to convert it to an |
| 100 * object that must be directly encodable. | 129 * object that must be directly encodable. |
| 101 * | 130 * |
| 102 * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the | 131 * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the |
| 103 * unencodable object. | 132 * unencodable object. |
| 104 */ | 133 */ |
| 105 String encode(Object value, {toEncodable(var object)}) { | 134 String encode(Object value, {toEncodable(var object)}) { |
| 135 if (toEncodable == null) toEncodable = _toEncodable; | |
| 106 if (toEncodable == null) return encoder.convert(value); | 136 if (toEncodable == null) return encoder.convert(value); |
| 107 return new JsonEncoder(toEncodable).convert(value); | 137 return new JsonEncoder(toEncodable).convert(value); |
| 108 } | 138 } |
| 109 | 139 |
| 110 JsonEncoder get encoder => const JsonEncoder(); | 140 JsonEncoder get encoder { |
| 111 JsonDecoder get decoder => const JsonDecoder(null); | 141 if (_toEncodable == null) return const JsonEncoder(); |
| 112 } | 142 return new JsonEncoder(_toEncodable); |
| 113 | |
| 114 typedef _Reviver(var key, var value); | |
| 115 | |
| 116 class _ReviverJsonCodec extends JsonCodec { | |
| 117 final _Reviver _reviver; | |
| 118 _ReviverJsonCodec(this._reviver); | |
| 119 | |
| 120 dynamic decode(String source, {reviver(var key, var value)}) { | |
| 121 if (reviver == null) reviver = _reviver; | |
| 122 return new JsonDecoder(reviver).convert(source); | |
| 123 } | 143 } |
|
Lasse Reichstein Nielsen
2014/02/10 07:08:30
Insert newline.
floitsch
2014/02/10 15:11:30
Done.
| |
| 124 | 144 JsonDecoder get decoder { |
| 125 JsonDecoder get decoder => new JsonDecoder(_reviver); | 145 if (_reviver == null) return const JsonDecoder(); |
| 146 return new JsonDecoder(_reviver); | |
| 147 } | |
| 126 } | 148 } |
| 127 | 149 |
| 128 /** | 150 /** |
| 129 * This class converts JSON objects to strings. | 151 * This class converts JSON objects to strings. |
| 130 */ | 152 */ |
| 131 class JsonEncoder extends Converter<Object, String> { | 153 class JsonEncoder extends Converter<Object, String> { |
| 132 final _toEncodableFunction; | 154 final _toEncodableFunction; |
| 133 | 155 |
| 134 /** | 156 /** |
| 135 * Creates a JSON encoder. | 157 * Creates a JSON encoder. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 /** | 254 /** |
| 233 * This class parses JSON strings and builds the corresponding objects. | 255 * This class parses JSON strings and builds the corresponding objects. |
| 234 */ | 256 */ |
| 235 class JsonDecoder extends Converter<String, Object> { | 257 class JsonDecoder extends Converter<String, Object> { |
| 236 final _Reviver _reviver; | 258 final _Reviver _reviver; |
| 237 /** | 259 /** |
| 238 * Constructs a new JsonDecoder. | 260 * Constructs a new JsonDecoder. |
| 239 * | 261 * |
| 240 * The [reviver] may be `null`. | 262 * The [reviver] may be `null`. |
| 241 */ | 263 */ |
| 242 const JsonDecoder(reviver(var key, var value)) : this._reviver = reviver; | 264 const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver; |
| 243 | 265 |
| 244 /** | 266 /** |
| 245 * Converts the given JSON-string [input] to its corresponding object. | 267 * Converts the given JSON-string [input] to its corresponding object. |
| 246 * | 268 * |
| 247 * Parsed JSON values are of the types [num], [String], [bool], [Null], | 269 * Parsed JSON values are of the types [num], [String], [bool], [Null], |
| 248 * [List]s of parsed JSON values or [Map]s from [String] to parsed | 270 * [List]s of parsed JSON values or [Map]s from [String] to parsed |
| 249 * JSON values. | 271 * JSON values. |
| 250 * | 272 * |
| 251 * If `this` was initialized with a reviver, then the parsing operation | 273 * If `this` was initialized with a reviver, then the parsing operation |
| 252 * invokes the reviver on every object or list property that has been parsed. | 274 * invokes the reviver on every object or list property that has been parsed. |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 first = false; | 496 first = false; |
| 475 }); | 497 }); |
| 476 sink.write('}'); | 498 sink.write('}'); |
| 477 seen.remove(object); | 499 seen.remove(object); |
| 478 return true; | 500 return true; |
| 479 } else { | 501 } else { |
| 480 return false; | 502 return false; |
| 481 } | 503 } |
| 482 } | 504 } |
| 483 } | 505 } |
| OLD | NEW |