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

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

Issue 14289002: Make JSON stringify throw a JsonUnsupportedObjectError on cycles. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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/json/json_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/json/json.dart
diff --git a/sdk/lib/json/json.dart b/sdk/lib/json/json.dart
index 8568d13c9b5d98182ace7482766eabbbb8310d16..e30dc57cfd479d4afb2f0682de530abe121d718f 100644
--- a/sdk/lib/json/json.dart
+++ b/sdk/lib/json/json.dart
@@ -21,8 +21,8 @@ class JsonUnsupportedObjectError implements Error {
final unsupportedObject;
/** The exception thrown by object's [:toJson:] method, if any. */
final cause;
- JsonUnsupportedObjectError(this.unsupportedObject) : cause = null;
- JsonUnsupportedObjectError.withCause(this.unsupportedObject, this.cause);
+
+ JsonUnsupportedObjectError(this.unsupportedObject, { this.cause });
String toString() {
if (cause != null) {
@@ -35,6 +35,19 @@ class JsonUnsupportedObjectError implements Error {
/**
+ * Reports that an object could not be stringified due to cyclic references.
+ *
+ * An object that references itself cannot be serialized by [stringify].
+ * When the cycle is detected, a [JsonCyclicError] is thrown.
+ */
+class JsonCyclicError extends JsonUnsupportedObjectError {
+ /** The first object that was detected as part of a cycle. */
+ JsonCyclicError(Object object): super(object);
+ String toString() => "Cyclic error in JSON stringify";
+}
+
+
+/**
* Parses [json] and build the corresponding parsed JSON value.
*
* Parsed JSON values are of the types [num], [String], [bool], [Null],
@@ -65,20 +78,26 @@ _parse(String json, reviver(var key, var value)) {
/**
* Serializes [object] into a JSON string.
*
- * Directly serializable types are [num], [String], [bool], [Null], [List]
- * and [Map].
+ * Directly serializable values are [num], [String], [bool], and [Null], as well
+ * as some [List] and [Map] values.
* For [List], the elements must all be serializable.
* For [Map], the keys must be [String] and the values must be serializable.
+ *
* If a value is any other type is attempted serialized, a "toJson()" method
* is invoked on the object and the result, which must be a directly
- * serializable type, is serialized instead of the original value.
+ * serializable value, is serialized instead of the original value.
+ *
* If the object does not support this method, throws, or returns a
* value that is not directly serializable, a [JsonUnsupportedObjectError]
* exception is thrown. If the call throws (including the case where there
* is no nullary "toJson" method, the error is caught and stored in the
* [JsonUnsupportedObjectError]'s [:cause:] field.
- *Json
- * Objects should not change during serialization.
+ *
+ * If a [List] or [Map] contains a reference to itself, directly or through
+ * other lists or maps, it cannot be serialized and a [JsonCyclicError] is
+ * thrown.
+ *
+ * Json Objects should not change during serialization.
* If an object is serialized more than once, [stringify] is allowed to cache
* the JSON text for it. I.e., if an object changes after it is first
* serialized, the new values may or may not be reflected in the result.
@@ -717,7 +736,7 @@ class _JsonStringifier {
// TODO: use Iterables.
for (int i = 0; i < seen.length; i++) {
if (identical(seen[i], object)) {
- throw 'Cyclic structure';
+ throw new JsonCyclicError(object);
}
}
seen.add(object);
@@ -736,7 +755,7 @@ class _JsonStringifier {
}
seen.removeLast();
} catch (e) {
- throw new JsonUnsupportedObjectError.withCause(object, e);
+ throw new JsonUnsupportedObjectError(object, cause: e);
}
}
}
« no previous file with comments | « no previous file | tests/lib/json/json_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698