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

Unified Diff: sdk/lib/convert/json.dart

Issue 19941002: Remove dart:codec and move classes into dart:convert. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/convert/encoding.dart ('k') | sdk/lib/convert/utf.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/json.dart
diff --git a/sdk/lib/convert/json.dart b/sdk/lib/convert/json.dart
index 8baf9970bbfc9b630f71897779c686ba6dafd4cb..f29517eb5d04f074561c7af98cffece8ba80ea26 100644
--- a/sdk/lib/convert/json.dart
+++ b/sdk/lib/convert/json.dart
@@ -4,6 +4,56 @@
part of dart.convert;
+final JSON = new JsonCodec();
+
+/**
+ * A [JsonCodec] encodes JSON objects to strings and decodes strings to
+ * JSON objects.
+ */
+class JsonCodec extends Codec<Object, String> {
+ const JsonCodec();
+
+ /**
+ * Creates a `JsonCodec` with the given reviver.
+ *
+ * The [reviver] function is called once for each object or list property
+ * that has been parsed during decoding. The `key` argument is either the
+ * integer list index for a list property, the map string for object
+ * properties, or `null` for the final result.
+ */
+ factory JsonCodec.withReviver(reviver(var key, var value)) =
+ _ReviverJsonCodec;
+
+ /**
+ * Parses the string and returns the resulting Json object.
+ *
+ * The optional [reviver] function is called once for each object or list
+ * property that has been parsed during decoding. The `key` argument is either
+ * the integer list index for a list property, the map string for object
+ * properties, or `null` for the final result.
+ *
+ * The default [reviver] (when not provided) is the identity function.
+ */
+ 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)}) {
+ if (reviver == null) reviver = _reviver;
+ return new JsonDecoder(reviver).convert(str);
+ }
+
+ JsonDecoder get decoder => new JsonDecoder(_reviver);
+}
+
/**
* A [JsonEncoder] converts JSON objects to strings.
*/
« no previous file with comments | « sdk/lib/convert/encoding.dart ('k') | sdk/lib/convert/utf.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698