OLD | NEW |
---|---|
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 * An instance of the default implementation of the [JsonCodec]. | 8 * An instance of the default implementation of the [JsonCodec]. |
9 * | 9 * |
10 * This instance provides a convenient access to the most common JSON | 10 * This instance provides a convenient access to the most common JSON |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 | 59 |
60 Object decode(String str, {reviver(var key, var value)}) { | 60 Object decode(String str, {reviver(var key, var value)}) { |
61 if (reviver == null) reviver = _reviver; | 61 if (reviver == null) reviver = _reviver; |
62 return new JsonDecoder(reviver).convert(str); | 62 return new JsonDecoder(reviver).convert(str); |
63 } | 63 } |
64 | 64 |
65 JsonDecoder get decoder => new JsonDecoder(_reviver); | 65 JsonDecoder get decoder => new JsonDecoder(_reviver); |
66 } | 66 } |
67 | 67 |
68 /** | 68 /** |
69 * A [JsonEncoder] converts JSON objects to strings. | 69 * This class converts JSON objects to strings. |
70 */ | 70 */ |
71 class JsonEncoder extends Converter<Object, String> { | 71 class JsonEncoder extends Converter<Object, String> { |
72 JsonEncoder(); | 72 JsonEncoder(); |
73 | 73 |
74 /** | 74 /** |
75 * Converts the given object [o] to its JSON representation. | 75 * Converts the given object [o] to its JSON representation. |
76 * | 76 * |
77 * Directly serializable values are [num], [String], [bool], and [Null], as | 77 * Directly serializable values are [num], [String], [bool], and [Null], as |
78 * well as some [List] and [Map] values. | 78 * well as some [List] and [Map] values. |
79 * For [List], the elements must all be serializable. | 79 * For [List], the elements must all be serializable. |
(...skipping 12 matching lines...) Expand all Loading... | |
92 * If a [List] or [Map] contains a reference to itself, directly or through | 92 * If a [List] or [Map] contains a reference to itself, directly or through |
93 * other lists or maps, it cannot be serialized and a [JsonCyclicError] is | 93 * other lists or maps, it cannot be serialized and a [JsonCyclicError] is |
94 * thrown. | 94 * thrown. |
95 * | 95 * |
96 * Json Objects should not change during serialization. | 96 * Json Objects should not change during serialization. |
97 * If an object is serialized more than once, [stringify] is allowed to cache | 97 * If an object is serialized more than once, [stringify] is allowed to cache |
98 * the JSON text for it. I.e., if an object changes after it is first | 98 * the JSON text for it. I.e., if an object changes after it is first |
99 * serialized, the new values may or may not be reflected in the result. | 99 * serialized, the new values may or may not be reflected in the result. |
100 */ | 100 */ |
101 String convert(Object o) => OLD_JSON_LIB.stringify(o); | 101 String convert(Object o) => OLD_JSON_LIB.stringify(o); |
102 | |
103 ChunkedConversionSink<Object, dynamic> startChunkedConversion( | |
104 ChunkedConversionSink sink) { | |
105 return new _JsonEncoderSink(sink.adaptTo(outputInterface)); | |
106 } | |
107 | |
108 ChunkedConversionInterface get outputInterface => | |
109 StringConversionSink.INTERFACE; | |
102 } | 110 } |
103 | 111 |
112 /** | |
113 * Implements the chunked conversion from object to its JSON representation. | |
114 * | |
115 * The sink only accepts one value (and therefore only allows its input in | |
116 * [addNonChunked], but will produce output in a chunked way. | |
117 */ | |
118 class _JsonEncoderSink extends ChunkedConversionSink<Object, Object> { | |
119 StringConversionSink _sink; | |
120 | |
121 _JsonEncoderSink(this._sink); | |
122 | |
123 /** | |
124 * Encodes the given object [o]. | |
125 * | |
126 * Although the input is non-chunked the output will receive the | |
127 * encoded string in chunks. | |
128 */ | |
129 void addNonChunked(Object o) { | |
130 ClosableStringSink stringSink = _sink.asStringSink(); | |
131 OLD_JSON_LIB.printOn(o, stringSink); | |
132 stringSink.close(); | |
133 } | |
134 } | |
135 | |
136 | |
104 typedef _Reviver(var key, var value); | 137 typedef _Reviver(var key, var value); |
105 | 138 |
106 | |
107 /** | 139 /** |
108 * A [JsonDecoder] parses JSON strings and builds the corresponding objects. | 140 * This class parses JSON strings and builds the corresponding objects. |
109 */ | 141 */ |
110 class JsonDecoder extends Converter<String, Object> { | 142 class JsonDecoder extends Converter<String, Object> { |
111 final _Reviver _reviver; | 143 final _Reviver _reviver; |
112 /** | 144 /** |
113 * Constructs a new JsonDecoder. | 145 * Constructs a new JsonDecoder. |
114 * | 146 * |
115 * The [reviver] may be `null`. | 147 * The [reviver] may be `null`. |
116 */ | 148 */ |
117 JsonDecoder(reviver(var key, var value)) : this._reviver = reviver; | 149 JsonDecoder(reviver(var key, var value)) : this._reviver = reviver; |
118 | 150 |
119 /** | 151 /** |
120 * Converts the given Json-string [input] to its corresponding object. | 152 * Converts the given JSON-string [input] to its corresponding object. |
121 * | 153 * |
122 * Parsed JSON values are of the types [num], [String], [bool], [Null], | 154 * Parsed JSON values are of the types [num], [String], [bool], [Null], |
123 * [List]s of parsed JSON values or [Map]s from [String] to parsed | 155 * [List]s of parsed JSON values or [Map]s from [String] to parsed |
124 * JSON values. | 156 * JSON values. |
125 * | 157 * |
126 * If `this` was initialized with a reviver, then the parsing operation | 158 * If `this` was initialized with a reviver, then the parsing operation |
127 * invokes the reviver on every object or list property that has been parsed. | 159 * invokes the reviver on every object or list property that has been parsed. |
128 * The arguments are the property name ([String]) or list index ([int]), and | 160 * The arguments are the property name ([String]) or list index ([int]), and |
129 * the value is the parsed value. The return value of the reviver is used as | 161 * the value is the parsed value. The return value of the reviver is used as |
130 * the value of that property instead the parsed value. | 162 * the value of that property instead the parsed value. |
131 * | 163 * |
132 * Throws [FormatException] if the input is not valid JSON text. | 164 * Throws [FormatException] if the input is not valid JSON text. |
133 */ | 165 */ |
134 Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver); | 166 Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver); |
167 | |
168 /** | |
169 * Starts a conversion from a chunked JSON string to its corresponding | |
170 * object. | |
171 * | |
172 * The output [sink] receives exactly one decoded element through | |
173 * `addNonChunked`. | |
174 */ | |
175 StringConversionSink startChunkedConversion(ChunkedConversionSink sink) { | |
176 return new _JsonDecoderSink(_reviver, sink.adaptTo(outputInterface)); | |
177 } | |
178 | |
179 ChunkedConversionInterface get inputInterface => | |
180 StringConversionSink.INTERFACE; | |
135 } | 181 } |
182 | |
183 /** | |
184 * Implements the chunked conversion from a JSON string to its corresponding | |
185 * object. | |
186 * | |
187 * The sink only creates one object, but its input can be chunked. | |
188 */ | |
189 // TODO(floitsch): don't accumulate everything before starting to decode. | |
190 class _JsonDecoderSink extends _StringSinkConversionSink { | |
191 final _Reviver _reviver; | |
192 final ChunkedConversionSink<Object, Object> _chunkedSink; | |
193 | |
194 _JsonDecoderSink(this._reviver, this._chunkedSink) | |
195 : super(new StringBuffer()); | |
196 | |
197 void close() { | |
198 super.close(); | |
199 StringBuffer buffer = _stringSink; | |
Søren Gjesse
2013/07/24 09:26:41
Any reason for this temporary?
Maybe relying on t
floitsch
2013/07/24 18:31:15
The temporary was to get the StringBuffer type. I
| |
200 Object decoded = OLD_JSON_LIB.parse(buffer.toString(), _reviver); | |
201 _chunkedSink.addNonChunked(decoded); | |
202 } | |
203 } | |
OLD | NEW |