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

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

Issue 52333002: Update type signature of JSON codec. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month 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 | « no previous file | no next file » | 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. 8 * Error thrown by JSON serialization if an object cannot be serialized.
9 * 9 *
10 * The [unsupportedObject] field holds that object that failed to be serialized. 10 * The [unsupportedObject] field holds that object that failed to be serialized.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 /** 79 /**
80 * Parses the string and returns the resulting Json object. 80 * Parses the string and returns the resulting Json object.
81 * 81 *
82 * The optional [reviver] function is called once for each object or list 82 * The optional [reviver] function is called once for each object or list
83 * property that has been parsed during decoding. The `key` argument is either 83 * property that has been parsed during decoding. The `key` argument is either
84 * the integer list index for a list property, the map string for object 84 * the integer list index for a list property, the map string for object
85 * properties, or `null` for the final result. 85 * properties, or `null` for the final result.
86 * 86 *
87 * The default [reviver] (when not provided) is the identity function. 87 * The default [reviver] (when not provided) is the identity function.
88 */ 88 */
89 Object decode(String source, {reviver(var key, var value)}) { 89 dynamic decode(String source, {reviver(var key, var value)}) {
90 if (reviver == null) return decoder.convert(source); 90 if (reviver == null) return decoder.convert(source);
91 return new JsonDecoder(reviver).convert(source); 91 return new JsonDecoder(reviver).convert(source);
92 } 92 }
93 93
94 /** 94 /**
95 * Converts [value] to a JSON string. 95 * Converts [value] to a JSON string.
96 * 96 *
97 * If value contains objects that are not directly encodable to a JSON 97 * If value contains objects that are not directly encodable to a JSON
98 * string (a value that is not a number, boolean, string, null, list or a map 98 * string (a value that is not a number, boolean, string, null, list or a map
99 * with string keys), the [toEncodable] function is used to convert it to an 99 * with string keys), the [toEncodable] function is used to convert it to an
100 * object that must be directly encodable. 100 * object that must be directly encodable.
101 * 101 *
102 * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the 102 * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the
103 * unencodable object. 103 * unencodable object.
104 */ 104 */
105 Object encode(Object value, {toEncodable(var object)}) { 105 String encode(Object value, {toEncodable(var object)}) {
106 if (toEncodable == null) return encoder.convert(value); 106 if (toEncodable == null) return encoder.convert(value);
107 return new JsonEncoder(toEncodable).convert(value); 107 return new JsonEncoder(toEncodable).convert(value);
108 } 108 }
109 109
110 JsonEncoder get encoder => const JsonEncoder(); 110 JsonEncoder get encoder => const JsonEncoder();
111 JsonDecoder get decoder => const JsonDecoder(null); 111 JsonDecoder get decoder => const JsonDecoder(null);
112 } 112 }
113 113
114 typedef _Reviver(var key, var value); 114 typedef _Reviver(var key, var value);
115 115
116 class _ReviverJsonCodec extends JsonCodec { 116 class _ReviverJsonCodec extends JsonCodec {
117 final _Reviver _reviver; 117 final _Reviver _reviver;
118 _ReviverJsonCodec(this._reviver); 118 _ReviverJsonCodec(this._reviver);
119 119
120 Object decode(String source, {reviver(var key, var value)}) { 120 dynamic decode(String source, {reviver(var key, var value)}) {
121 if (reviver == null) reviver = _reviver; 121 if (reviver == null) reviver = _reviver;
122 return new JsonDecoder(reviver).convert(source); 122 return new JsonDecoder(reviver).convert(source);
123 } 123 }
124 124
125 JsonDecoder get decoder => new JsonDecoder(_reviver); 125 JsonDecoder get decoder => new JsonDecoder(_reviver);
126 } 126 }
127 127
128 /** 128 /**
129 * This class converts JSON objects to strings. 129 * This class converts JSON objects to strings.
130 */ 130 */
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 * JSON values. 249 * JSON values.
250 * 250 *
251 * If `this` was initialized with a reviver, then the parsing operation 251 * If `this` was initialized with a reviver, then the parsing operation
252 * invokes the reviver on every object or list property that has been parsed. 252 * invokes the reviver on every object or list property that has been parsed.
253 * The arguments are the property name ([String]) or list index ([int]), and 253 * The arguments are the property name ([String]) or list index ([int]), and
254 * the value is the parsed value. The return value of the reviver is used as 254 * the value is the parsed value. The return value of the reviver is used as
255 * the value of that property instead the parsed value. 255 * the value of that property instead the parsed value.
256 * 256 *
257 * Throws [FormatException] if the input is not valid JSON text. 257 * Throws [FormatException] if the input is not valid JSON text.
258 */ 258 */
259 Object convert(String input) => _parseJson(input, _reviver); 259 dynamic convert(String input) => _parseJson(input, _reviver);
260 260
261 /** 261 /**
262 * Starts a conversion from a chunked JSON string to its corresponding 262 * Starts a conversion from a chunked JSON string to its corresponding
263 * object. 263 * object.
264 * 264 *
265 * The output [sink] receives exactly one decoded element through `add`. 265 * The output [sink] receives exactly one decoded element through `add`.
266 */ 266 */
267 StringConversionSink startChunkedConversion( 267 StringConversionSink startChunkedConversion(
268 ChunkedConversionSink<Object> sink) { 268 ChunkedConversionSink<Object> sink) {
269 return new _JsonDecoderSink(_reviver, sink); 269 return new _JsonDecoderSink(_reviver, sink);
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 first = false; 475 first = false;
476 }); 476 });
477 sink.write('}'); 477 sink.write('}');
478 seen.remove(object); 478 seen.remove(object);
479 return true; 479 return true;
480 } else { 480 } else {
481 return false; 481 return false;
482 } 482 }
483 } 483 }
484 } 484 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698