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

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

Issue 133923004: Improve Json constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 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 | « no previous file | tests/lib/convert/json_toEncodable_reviver_test.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 16001e69d0b798a0135691ce468167ce02fae08b..37f5db684e4a46332546212be3578d84e7594921 100644
--- a/sdk/lib/convert/json.dart
+++ b/sdk/lib/convert/json.dart
@@ -58,12 +58,41 @@ class JsonCyclicError extends JsonUnsupportedObjectError {
*/
const JsonCodec JSON = const JsonCodec();
+typedef _Reviver(var key, var value);
+typedef _ToEncodable(var o);
+
+
/**
* A [JsonCodec] encodes JSON objects to strings and decodes strings to
* JSON objects.
*/
class JsonCodec extends Codec<Object, String> {
- const JsonCodec();
+ final _Reviver _reviver;
+ final _ToEncodable _toEncodable;
+
+ /**
+ * Creates a `JsonCodec` with the given reviver and encoding function.
+ *
+ * The [reviver] function is called during decoding. It is invoked
+ * once for each object or list property that has been parsed.
+ * The `key` argument is either the
+ * integer list index for a list property, the map string for object
Lasse Reichstein Nielsen 2014/02/10 07:08:30 map string -> string map key
floitsch 2014/02/10 15:11:30 Done.
+ * properties, or `null` for the final result.
+ *
+ * If [reviver] is omitted, it defaults to returning the value.
Lasse Reichstein Nielsen 2014/02/10 07:08:30 ... it defaults to a function returning its argume
floitsch 2014/02/10 15:11:30 it defaults to returning the value argument. (sinc
+ *
+ * The [toEncodable] function is used during encoding. It is invoked for
+ * values that are not directly encodable to a JSON
+ * string (a value that is not a number, boolean, string, null, list or a map
+ * with string keys). The function must return an object that is directly
+ * encodable.
+ *
+ * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the
Lasse Reichstein Nielsen 2014/02/10 07:08:30 ... it defaults to a function that returns the res
floitsch 2014/02/10 15:11:30 Done.
+ * unencodable object.
+ */
+ const JsonCodec({reviver(var key, var value), toEncodable(var object)})
+ : _reviver = reviver,
+ _toEncodable = toEncodable;
/**
* Creates a `JsonCodec` with the given reviver.
@@ -73,8 +102,7 @@ class JsonCodec extends Codec<Object, String> {
* 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;
+ JsonCodec.withReviver(reviver(var key, var value)) : this(reviver: reviver);
/**
* Parses the string and returns the resulting Json object.
@@ -87,6 +115,7 @@ class JsonCodec extends Codec<Object, String> {
* The default [reviver] (when not provided) is the identity function.
*/
dynamic decode(String source, {reviver(var key, var value)}) {
+ if (reviver == null) reviver = _reviver;
if (reviver == null) return decoder.convert(source);
return new JsonDecoder(reviver).convert(source);
}
@@ -103,26 +132,19 @@ class JsonCodec extends Codec<Object, String> {
* unencodable object.
*/
String encode(Object value, {toEncodable(var object)}) {
+ if (toEncodable == null) toEncodable = _toEncodable;
if (toEncodable == null) return encoder.convert(value);
return new JsonEncoder(toEncodable).convert(value);
}
- JsonEncoder get encoder => const JsonEncoder();
- JsonDecoder get decoder => const JsonDecoder(null);
-}
-
-typedef _Reviver(var key, var value);
-
-class _ReviverJsonCodec extends JsonCodec {
- final _Reviver _reviver;
- _ReviverJsonCodec(this._reviver);
-
- dynamic decode(String source, {reviver(var key, var value)}) {
- if (reviver == null) reviver = _reviver;
- return new JsonDecoder(reviver).convert(source);
+ JsonEncoder get encoder {
+ if (_toEncodable == null) return const JsonEncoder();
+ return new JsonEncoder(_toEncodable);
+ }
Lasse Reichstein Nielsen 2014/02/10 07:08:30 Insert newline.
floitsch 2014/02/10 15:11:30 Done.
+ JsonDecoder get decoder {
+ if (_reviver == null) return const JsonDecoder();
+ return new JsonDecoder(_reviver);
}
-
- JsonDecoder get decoder => new JsonDecoder(_reviver);
}
/**
@@ -239,7 +261,7 @@ class JsonDecoder extends Converter<String, Object> {
*
* The [reviver] may be `null`.
*/
- const JsonDecoder(reviver(var key, var value)) : this._reviver = reviver;
+ const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver;
/**
* Converts the given JSON-string [input] to its corresponding object.
« no previous file with comments | « no previous file | tests/lib/convert/json_toEncodable_reviver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698