Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: sdk/lib/codec/json.dart

Issue 19000006: First version of Codecs and Converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/codec/encoding.dart ('k') | sdk/lib/convert/convert.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 if (reviver == null) reviver = _reviver;
47 return new JsonDecoder(reviver).convert(str);
48 }
49
50 JsonDecoder get decoder => new JsonDecoder(_reviver);
51 }
OLDNEW
« no previous file with comments | « sdk/lib/codec/encoding.dart ('k') | sdk/lib/convert/convert.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698