OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.convert; |
| 6 |
| 7 /** |
| 8 * A [JsonEncoder] converts JSON objects to strings. |
| 9 */ |
| 10 class JsonEncoder extends Converter<Object, String> { |
| 11 JsonEncoder(); |
| 12 |
| 13 /** |
| 14 * Converts the given object [o] to its JSON representation. |
| 15 * |
| 16 * Directly serializable values are [num], [String], [bool], and [Null], as |
| 17 * well as some [List] and [Map] values. |
| 18 * For [List], the elements must all be serializable. |
| 19 * For [Map], the keys must be [String] and the values must be serializable. |
| 20 * |
| 21 * If a value is any other type is attempted serialized, a "toJson()" method |
| 22 * is invoked on the object and the result, which must be a directly |
| 23 * serializable value, is serialized instead of the original value. |
| 24 * |
| 25 * If the object does not support this method, throws, or returns a |
| 26 * value that is not directly serializable, a [JsonUnsupportedObjectError] |
| 27 * exception is thrown. If the call throws (including the case where there |
| 28 * is no nullary "toJson" method, the error is caught and stored in the |
| 29 * [JsonUnsupportedObjectError]'s [:cause:] field. |
| 30 * |
| 31 * If a [List] or [Map] contains a reference to itself, directly or through |
| 32 * other lists or maps, it cannot be serialized and a [JsonCyclicError] is |
| 33 * thrown. |
| 34 * |
| 35 * Json Objects should not change during serialization. |
| 36 * If an object is serialized more than once, [stringify] is allowed to cache |
| 37 * the JSON text for it. I.e., if an object changes after it is first |
| 38 * serialized, the new values may or may not be reflected in the result. |
| 39 */ |
| 40 String convert(Object o) => OLD_JSON_LIB.stringify(o); |
| 41 } |
| 42 |
| 43 typedef _Reviver(var key, var value); |
| 44 |
| 45 |
| 46 /** |
| 47 * A [JsonDecoder] parses JSON strings and builds the corresponding objects. |
| 48 */ |
| 49 class JsonDecoder extends Converter<String, Object> { |
| 50 final _Reviver _reviver; |
| 51 /** |
| 52 * Constructs a new JsonDecoder. |
| 53 * |
| 54 * The [reviver] may be `null`. |
| 55 */ |
| 56 JsonDecoder(reviver(var key, var value)) : this._reviver = reviver; |
| 57 |
| 58 /** |
| 59 * Converts the given Json-string [input] to its corresponding object. |
| 60 * |
| 61 * Parsed JSON values are of the types [num], [String], [bool], [Null], |
| 62 * [List]s of parsed JSON values or [Map]s from [String] to parsed |
| 63 * JSON values. |
| 64 * |
| 65 * 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. |
| 67 * 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 |
| 69 * the value of that property instead the parsed value. |
| 70 * |
| 71 * Throws [FormatException] if the input is not valid JSON text. |
| 72 */ |
| 73 Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver); |
| 74 } |
OLD | NEW |