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.codec; | |
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. | |
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 reviver = (reviver == null) ? _reviver : reviver; | |
srdjan
2013/07/12 16:03:16
Why not:
if (reviver == null) {
reviver = revive
floitsch
2013/07/12 16:09:15
Done.
| |
47 return new JsonDecoder(reviver).convert(str); | |
48 } | |
49 | |
50 JsonDecoder get decoder => new JsonDecoder(_reviver); | |
51 } | |
OLD | NEW |