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 final JSON = new JsonCodec(); | |
| 8 | |
| 9 /** | |
| 10 * A [JsonCodec] encodes JSON objects to strings and decodes strings to | |
| 11 * JSON objects. | |
| 12 */ | |
| 13 class JsonCodec extends Codec<Object, String> { | |
| 14 const JsonCodec(); | |
| 15 | |
| 16 /** | |
| 17 * Creates a `JsonCodec` with the given reviver. | |
| 18 * | |
| 19 * The [reviver] function is called once for each object or list property | |
| 20 * that has been parsed during decoding. The `key` argument is either the | |
| 21 * integer list index for a list property, the map string for object | |
| 22 * properties, or `null` for the final result. | |
| 23 */ | |
| 24 factory JsonCodec.withReviver(reviver(var key, var value)) = | |
| 25 _ReviverJsonCodec; | |
| 26 | |
| 27 /** | |
| 28 * Parses the string and returns the resulting Json object. | |
| 29 * | |
| 30 * The optional [reviver] function, if provided, is called once for each | |
| 31 * object or list property parsed. | |
|
Søren Gjesse
2013/07/22 12:04:42
Repeat the documentation on the type of 'key' from
floitsch
2013/07/22 12:24:58
Done.
| |
| 32 */ | |
| 33 Object decode(String str, {reviver(var key, var value)}) { | |
| 34 return new JsonDecoder(reviver).convert(str); | |
| 35 } | |
| 36 | |
| 37 JsonEncoder get encoder => new JsonEncoder(); | |
| 38 JsonDecoder get decoder => new JsonDecoder(null); | |
| 39 } | |
| 40 | |
| 41 class _ReviverJsonCodec extends JsonCodec { | |
| 42 final Function _reviver; | |
| 43 _ReviverJsonCodec(this._reviver); | |
| 44 | |
| 45 Object decode(String str, {reviver(var key, var value)}) { | |
| 46 if (reviver == null) reviver = _reviver; | |
| 47 return new JsonDecoder(reviver).convert(str); | |
| 48 } | |
| 49 | |
| 50 JsonDecoder get decoder => new JsonDecoder(_reviver); | |
| 51 } | |
| 52 | |
| 7 /** | 53 /** |
| 8 * A [JsonEncoder] converts JSON objects to strings. | 54 * A [JsonEncoder] converts JSON objects to strings. |
| 9 */ | 55 */ |
| 10 class JsonEncoder extends Converter<Object, String> { | 56 class JsonEncoder extends Converter<Object, String> { |
| 11 JsonEncoder(); | 57 JsonEncoder(); |
| 12 | 58 |
| 13 /** | 59 /** |
| 14 * Converts the given object [o] to its JSON representation. | 60 * Converts the given object [o] to its JSON representation. |
| 15 * | 61 * |
| 16 * Directly serializable values are [num], [String], [bool], and [Null], as | 62 * Directly serializable values are [num], [String], [bool], and [Null], as |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 * If `this` was initialized with a reviver, then the parsing operation | 111 * If `this` was initialized with a reviver, then the parsing operation |
| 66 * invokes the reviver on every object or list property that has been parsed. | 112 * invokes the reviver on every object or list property that has been parsed. |
| 67 * The arguments are the property name ([String]) or list index ([int]), and | 113 * The arguments are the property name ([String]) or list index ([int]), and |
| 68 * the value is the parsed value. The return value of the reviver is used as | 114 * the value is the parsed value. The return value of the reviver is used as |
| 69 * the value of that property instead the parsed value. | 115 * the value of that property instead the parsed value. |
| 70 * | 116 * |
| 71 * Throws [FormatException] if the input is not valid JSON text. | 117 * Throws [FormatException] if the input is not valid JSON text. |
| 72 */ | 118 */ |
| 73 Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver); | 119 Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver); |
| 74 } | 120 } |
| OLD | NEW |