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 | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Json-objects -> JSON objects
floitsch
2013/07/12 16:09:15
Done.
| |
11 * Json-objects. | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
JSON objects.
floitsch
2013/07/12 16:09:15
Done.
| |
12 */ | |
13 class JsonCodec extends Codec<Object, String> { | |
14 const JsonCodec(); | |
15 | |
16 /** | |
17 * Creates a JsonCodec with the given reviver. | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
`JsonCodec`
floitsch
2013/07/12 16:09:15
Done.
| |
18 * The [reviver] function is called once for each object or list property | |
19 * that has been parsed. | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
... has been parsed during decoding.
The key passe
floitsch
2013/07/12 16:09:15
Done.
| |
20 */ | |
21 factory JsonCodec.withReviver(reviver(var key, var value)) = | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
The reviver is only for the decoder.
The encoder s
floitsch
2013/07/12 16:09:15
Yes. But the current JSON implementation doesn't p
| |
22 _ReviverJsonCodec; | |
23 | |
24 // TODO(floitsch): should we add "stringify" and "parse" for convenience as | |
25 // aliases for encode/decode? | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
It would be redundant, and we would probably want
floitsch
2013/07/12 16:09:15
Done.
| |
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; | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Parentheses around "reviver == null"
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 |