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

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

Issue 23554004: Made old dart:json library use convert to parse JSON. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addres review comments. Created 7 years, 3 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/_internal/libraries.dart ('k') | sdk/lib/json/json.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.convert; 5 part of dart.convert;
6 6
7 /** 7 /**
8 * Error thrown by JSON serialization if an object cannot be serialized.
9 *
10 * The [unsupportedObject] field holds that object that failed to be serialized.
11 *
12 * If an object isn't directly serializable, the serializer calls the 'toJson'
13 * method on the object. If that call fails, the error will be stored in the
14 * [cause] field. If the call returns an object that isn't directly
15 * serializable, the [cause] is be null.
16 */
17 class JsonUnsupportedObjectError extends Error {
18 /** The object that could not be serialized. */
19 final unsupportedObject;
20 /** The exception thrown by object's [:toJson:] method, if any. */
21 final cause;
22
23 JsonUnsupportedObjectError(this.unsupportedObject, { this.cause });
24
25 String toString() {
26 if (cause != null) {
27 return "Calling toJson method on object failed.";
28 } else {
29 return "Object toJson method returns non-serializable value.";
30 }
31 }
32 }
33
34
35 /**
36 * Reports that an object could not be stringified due to cyclic references.
37 *
38 * An object that references itself cannot be serialized by [stringify].
39 * When the cycle is detected, a [JsonCyclicError] is thrown.
40 */
41 class JsonCyclicError extends JsonUnsupportedObjectError {
42 /** The first object that was detected as part of a cycle. */
43 JsonCyclicError(Object object): super(object);
44 String toString() => "Cyclic error in JSON stringify";
45 }
46
47
48 /**
8 * An instance of the default implementation of the [JsonCodec]. 49 * An instance of the default implementation of the [JsonCodec].
9 * 50 *
10 * This instance provides a convenient access to the most common JSON 51 * This instance provides a convenient access to the most common JSON
11 * use cases. 52 * use cases.
12 * 53 *
13 * Examples: 54 * Examples:
14 * 55 *
15 * var encoded = JSON.encode([1, 2, { "a": null }]); 56 * var encoded = JSON.encode([1, 2, { "a": null }]);
16 * var decoded = JSON.decode('["foo", { "bar": 499 }]'); 57 * var decoded = JSON.decode('["foo", { "bar": 499 }]');
17 */ 58 */
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 * JSON values. 215 * JSON values.
175 * 216 *
176 * If `this` was initialized with a reviver, then the parsing operation 217 * If `this` was initialized with a reviver, then the parsing operation
177 * invokes the reviver on every object or list property that has been parsed. 218 * invokes the reviver on every object or list property that has been parsed.
178 * The arguments are the property name ([String]) or list index ([int]), and 219 * The arguments are the property name ([String]) or list index ([int]), and
179 * the value is the parsed value. The return value of the reviver is used as 220 * the value is the parsed value. The return value of the reviver is used as
180 * the value of that property instead the parsed value. 221 * the value of that property instead the parsed value.
181 * 222 *
182 * Throws [FormatException] if the input is not valid JSON text. 223 * Throws [FormatException] if the input is not valid JSON text.
183 */ 224 */
184 Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver); 225 Object convert(String input) => _parseJson(input, _reviver);
185 226
186 /** 227 /**
187 * Starts a conversion from a chunked JSON string to its corresponding 228 * Starts a conversion from a chunked JSON string to its corresponding
188 * object. 229 * object.
189 * 230 *
190 * The output [sink] receives exactly one decoded element through `add`. 231 * The output [sink] receives exactly one decoded element through `add`.
191 */ 232 */
192 StringConversionSink startChunkedConversion( 233 StringConversionSink startChunkedConversion(
193 ChunkedConversionSink<Object> sink) { 234 ChunkedConversionSink<Object> sink) {
194 return new _JsonDecoderSink(_reviver, sink); 235 return new _JsonDecoderSink(_reviver, sink);
(...skipping 15 matching lines...) Expand all
210 final ChunkedConversionSink<Object> _chunkedSink; 251 final ChunkedConversionSink<Object> _chunkedSink;
211 252
212 _JsonDecoderSink(this._reviver, this._chunkedSink) 253 _JsonDecoderSink(this._reviver, this._chunkedSink)
213 : super(new StringBuffer()); 254 : super(new StringBuffer());
214 255
215 void close() { 256 void close() {
216 super.close(); 257 super.close();
217 StringBuffer buffer = _stringSink; 258 StringBuffer buffer = _stringSink;
218 String accumulated = buffer.toString(); 259 String accumulated = buffer.toString();
219 buffer.clear(); 260 buffer.clear();
220 Object decoded = OLD_JSON_LIB.parse(accumulated, _reviver); 261 Object decoded = _parseJson(accumulated, _reviver);
221 _chunkedSink.add(decoded); 262 _chunkedSink.add(decoded);
222 _chunkedSink.close(); 263 _chunkedSink.close();
223 } 264 }
224 } 265 }
266
267 // Internal optimized JSON parsing implementation.
268 external _parseJson(String source, reviver(key, value));
OLDNEW
« no previous file with comments | « sdk/lib/_internal/libraries.dart ('k') | sdk/lib/json/json.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698