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

Side by Side 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: Address comments. 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 unified diff | Download patch | Annotate | Revision Log
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 * This instance provides a convenient access to the most common JSON 51 * This instance provides a convenient access to the most common JSON
52 * use cases. 52 * use cases.
53 * 53 *
54 * Examples: 54 * Examples:
55 * 55 *
56 * var encoded = JSON.encode([1, 2, { "a": null }]); 56 * var encoded = JSON.encode([1, 2, { "a": null }]);
57 * var decoded = JSON.decode('["foo", { "bar": 499 }]'); 57 * var decoded = JSON.decode('["foo", { "bar": 499 }]');
58 */ 58 */
59 const JsonCodec JSON = const JsonCodec(); 59 const JsonCodec JSON = const JsonCodec();
60 60
61 typedef _Reviver(var key, var value);
62 typedef _ToEncodable(var o);
63
64
61 /** 65 /**
62 * A [JsonCodec] encodes JSON objects to strings and decodes strings to 66 * A [JsonCodec] encodes JSON objects to strings and decodes strings to
63 * JSON objects. 67 * JSON objects.
64 */ 68 */
65 class JsonCodec extends Codec<Object, String> { 69 class JsonCodec extends Codec<Object, String> {
66 const JsonCodec(); 70 final _Reviver _reviver;
71 final _ToEncodable _toEncodable;
72
73 /**
74 * Creates a `JsonCodec` with the given reviver and encoding function.
75 *
76 * The [reviver] function is called during decoding. It is invoked
77 * once for each object or list property that has been parsed.
78 * The `key` argument is either the
79 * integer list index for a list property, the string map key for object
80 * properties, or `null` for the final result.
81 *
82 * If [reviver] is omitted, it defaults to returning the value argument.
83 *
84 * The [toEncodable] function is used during encoding. It is invoked for
85 * values that are not directly encodable to a JSON
86 * string (a value that is not a number, boolean, string, null, list or a map
87 * with string keys). The function must return an object that is directly
88 * encodable.
89 *
90 * If [toEncodable] is omitted, it defaults to a function that returns the
91 * result of calling `.toJson()` on the unencodable object.
92 */
93 const JsonCodec({reviver(var key, var value), toEncodable(var object)})
94 : _reviver = reviver,
95 _toEncodable = toEncodable;
67 96
68 /** 97 /**
69 * Creates a `JsonCodec` with the given reviver. 98 * Creates a `JsonCodec` with the given reviver.
70 * 99 *
71 * The [reviver] function is called once for each object or list property 100 * The [reviver] function is called once for each object or list property
72 * that has been parsed during decoding. The `key` argument is either the 101 * that has been parsed during decoding. The `key` argument is either the
73 * integer list index for a list property, the map string for object 102 * integer list index for a list property, the string map key for object
74 * properties, or `null` for the final result. 103 * properties, or `null` for the final result.
75 */ 104 */
76 factory JsonCodec.withReviver(reviver(var key, var value)) = 105 JsonCodec.withReviver(reviver(var key, var value)) : this(reviver: reviver);
77 _ReviverJsonCodec;
78 106
79 /** 107 /**
80 * Parses the string and returns the resulting Json object. 108 * Parses the string and returns the resulting Json object.
81 * 109 *
82 * The optional [reviver] function is called once for each object or list 110 * 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 111 * 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 112 * the integer list index for a list property, the string map key for object
85 * properties, or `null` for the final result. 113 * properties, or `null` for the final result.
86 * 114 *
87 * The default [reviver] (when not provided) is the identity function. 115 * The default [reviver] (when not provided) is the identity function.
88 */ 116 */
89 dynamic decode(String source, {reviver(var key, var value)}) { 117 dynamic decode(String source, {reviver(var key, var value)}) {
118 if (reviver == null) reviver = _reviver;
90 if (reviver == null) return decoder.convert(source); 119 if (reviver == null) return decoder.convert(source);
91 return new JsonDecoder(reviver).convert(source); 120 return new JsonDecoder(reviver).convert(source);
92 } 121 }
93 122
94 /** 123 /**
95 * Converts [value] to a JSON string. 124 * Converts [value] to a JSON string.
96 * 125 *
97 * If value contains objects that are not directly encodable to a JSON 126 * 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 127 * 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 128 * with string keys), the [toEncodable] function is used to convert it to an
100 * object that must be directly encodable. 129 * object that must be directly encodable.
101 * 130 *
102 * If [toEncodable] is omitted, it defaults to calling `.toJson()` on the 131 * If [toEncodable] is omitted, it defaults to a function that returns the
103 * unencodable object. 132 * result of calling `.toJson()` on the unencodable object.
104 */ 133 */
105 String encode(Object value, {toEncodable(var object)}) { 134 String encode(Object value, {toEncodable(var object)}) {
135 if (toEncodable == null) toEncodable = _toEncodable;
106 if (toEncodable == null) return encoder.convert(value); 136 if (toEncodable == null) return encoder.convert(value);
107 return new JsonEncoder(toEncodable).convert(value); 137 return new JsonEncoder(toEncodable).convert(value);
108 } 138 }
109 139
110 JsonEncoder get encoder => const JsonEncoder(); 140 JsonEncoder get encoder {
111 JsonDecoder get decoder => const JsonDecoder(null); 141 if (_toEncodable == null) return const JsonEncoder();
112 } 142 return new JsonEncoder(_toEncodable);
113
114 typedef _Reviver(var key, var value);
115
116 class _ReviverJsonCodec extends JsonCodec {
117 final _Reviver _reviver;
118 _ReviverJsonCodec(this._reviver);
119
120 dynamic decode(String source, {reviver(var key, var value)}) {
121 if (reviver == null) reviver = _reviver;
122 return new JsonDecoder(reviver).convert(source);
123 } 143 }
124 144
125 JsonDecoder get decoder => new JsonDecoder(_reviver); 145 JsonDecoder get decoder {
146 if (_reviver == null) return const JsonDecoder();
147 return new JsonDecoder(_reviver);
148 }
126 } 149 }
127 150
128 /** 151 /**
129 * This class converts JSON objects to strings. 152 * This class converts JSON objects to strings.
130 */ 153 */
131 class JsonEncoder extends Converter<Object, String> { 154 class JsonEncoder extends Converter<Object, String> {
132 final _toEncodableFunction; 155 final _toEncodableFunction;
133 156
134 /** 157 /**
135 * Creates a JSON encoder. 158 * Creates a JSON encoder.
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 /** 255 /**
233 * This class parses JSON strings and builds the corresponding objects. 256 * This class parses JSON strings and builds the corresponding objects.
234 */ 257 */
235 class JsonDecoder extends Converter<String, Object> { 258 class JsonDecoder extends Converter<String, Object> {
236 final _Reviver _reviver; 259 final _Reviver _reviver;
237 /** 260 /**
238 * Constructs a new JsonDecoder. 261 * Constructs a new JsonDecoder.
239 * 262 *
240 * The [reviver] may be `null`. 263 * The [reviver] may be `null`.
241 */ 264 */
242 const JsonDecoder(reviver(var key, var value)) : this._reviver = reviver; 265 const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver;
243 266
244 /** 267 /**
245 * Converts the given JSON-string [input] to its corresponding object. 268 * Converts the given JSON-string [input] to its corresponding object.
246 * 269 *
247 * Parsed JSON values are of the types [num], [String], [bool], [Null], 270 * Parsed JSON values are of the types [num], [String], [bool], [Null],
248 * [List]s of parsed JSON values or [Map]s from [String] to parsed 271 * [List]s of parsed JSON values or [Map]s from [String] to parsed
249 * JSON values. 272 * JSON values.
250 * 273 *
251 * If `this` was initialized with a reviver, then the parsing operation 274 * 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. 275 * invokes the reviver on every object or list property that has been parsed.
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 first = false; 497 first = false;
475 }); 498 });
476 sink.write('}'); 499 sink.write('}');
477 seen.remove(object); 500 seen.remove(object);
478 return true; 501 return true;
479 } else { 502 } else {
480 return false; 503 return false;
481 } 504 }
482 } 505 }
483 } 506 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/js_backend/backend.dart ('k') | tests/language/language.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698