Chromium Code Reviews| Index: sdk/lib/codec/json.dart |
| diff --git a/sdk/lib/codec/json.dart b/sdk/lib/codec/json.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..160ce5ba0065878086382ca9e1b03fb6e02909cd |
| --- /dev/null |
| +++ b/sdk/lib/codec/json.dart |
| @@ -0,0 +1,51 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of dart.codec; |
| + |
| +final JSON = new JsonCodec(); |
| + |
| +/** |
| + * 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.
|
| + * Json-objects. |
|
Lasse Reichstein Nielsen
2013/07/12 11:50:47
JSON objects.
floitsch
2013/07/12 16:09:15
Done.
|
| + */ |
| +class JsonCodec extends Codec<Object, String> { |
| + const JsonCodec(); |
| + |
| + /** |
| + * 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.
|
| + * The [reviver] function is called once for each object or list property |
| + * 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.
|
| + */ |
| + 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
|
| + _ReviverJsonCodec; |
| + |
| + // TODO(floitsch): should we add "stringify" and "parse" for convenience as |
| + // 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.
|
| + |
| + /** |
| + * Parses the string and returns the resulting Json object. |
| + * |
| + * The optional [reviver] function, if provided, is called once for each |
| + * object or list property parsed. |
| + */ |
| + Object decode(String str, {reviver(var key, var value)}) { |
| + return new JsonDecoder(reviver).convert(str); |
| + } |
| + |
| + JsonEncoder get encoder => new JsonEncoder(); |
| + JsonDecoder get decoder => new JsonDecoder(null); |
| +} |
| + |
| +class _ReviverJsonCodec extends JsonCodec { |
| + final Function _reviver; |
| + _ReviverJsonCodec(this._reviver); |
| + |
| + Object decode(String str, {reviver(var key, var value)}) { |
| + 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.
|
| + return new JsonDecoder(reviver).convert(str); |
| + } |
| + |
| + JsonDecoder get decoder => new JsonDecoder(_reviver); |
| +} |