| 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 // Patch file for dart:convert library. | 5 // Patch file for dart:convert library. |
| 6 | 6 |
| 7 import 'dart:_js_helper' show patch; | 7 import 'dart:_js_helper' show argumentErrorValue, patch; |
| 8 import 'dart:_foreign_helper' show JS; | 8 import 'dart:_foreign_helper' show JS; |
| 9 import 'dart:_interceptors' show JSExtendableArray; | 9 import 'dart:_interceptors' show JSExtendableArray; |
| 10 import 'dart:_internal' show MappedIterable, ListIterable; | 10 import 'dart:_internal' show MappedIterable, ListIterable; |
| 11 import 'dart:collection' show Maps, LinkedHashMap; | 11 import 'dart:collection' show Maps, LinkedHashMap; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Parses [json] and builds the corresponding parsed JSON value. | 14 * Parses [json] and builds the corresponding parsed JSON value. |
| 15 * | 15 * |
| 16 * Parsed JSON values Nare of the types [num], [String], [bool], [Null], | 16 * Parsed JSON values Nare of the types [num], [String], [bool], [Null], |
| 17 * [List]s of parsed JSON values or [Map]s from [String] to parsed | 17 * [List]s of parsed JSON values or [Map]s from [String] to parsed |
| 18 * JSON values. | 18 * JSON values. |
| 19 * | 19 * |
| 20 * The optional [reviver] function, if provided, is called once for each object | 20 * The optional [reviver] function, if provided, is called once for each object |
| 21 * or list property parsed. The arguments are the property name ([String]) or | 21 * or list property parsed. The arguments are the property name ([String]) or |
| 22 * list index ([int]), and the value is the parsed value. The return value of | 22 * list index ([int]), and the value is the parsed value. The return value of |
| 23 * the reviver will be used as the value of that property instead of the parsed | 23 * the reviver will be used as the value of that property instead of the parsed |
| 24 * value. The top level value is passed to the reviver with the empty string as | 24 * value. The top level value is passed to the reviver with the empty string as |
| 25 * a key. | 25 * a key. |
| 26 * | 26 * |
| 27 * Throws [FormatException] if the input is not valid JSON text. | 27 * Throws [FormatException] if the input is not valid JSON text. |
| 28 */ | 28 */ |
| 29 @patch | 29 @patch |
| 30 _parseJson(String source, reviver(key, value)) { | 30 _parseJson(String source, reviver(key, value)) { |
| 31 if (source is! String) throw new ArgumentError(source); | 31 if (source is! String) throw argumentErrorValue(source); |
| 32 | 32 |
| 33 var parsed; | 33 var parsed; |
| 34 try { | 34 try { |
| 35 parsed = JS('=Object|JSExtendableArray|Null|bool|num|String', | 35 parsed = JS('=Object|JSExtendableArray|Null|bool|num|String', |
| 36 'dart.global.JSON.parse(#)', | 36 'JSON.parse(#)', |
| 37 source); | 37 source); |
| 38 } catch (e) { | 38 } catch (e) { |
| 39 throw new FormatException(JS('String', 'String(#)', e)); | 39 throw new FormatException(JS('String', 'String(#)', e)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 if (reviver == null) { | 42 if (reviver == null) { |
| 43 return _convertJsonToDartLazy(parsed); | 43 return _convertJsonToDartLazy(parsed); |
| 44 } else { | 44 } else { |
| 45 return _convertJsonToDart(parsed, reviver); | 45 return _convertJsonToDart(parsed, reviver); |
| 46 } | 46 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 // We keep track of the map entries that we have already | 133 // We keep track of the map entries that we have already |
| 134 // processed by adding them to a separate JavaScript object. | 134 // processed by adding them to a separate JavaScript object. |
| 135 var _processed = _newJavaScriptObject(); | 135 var _processed = _newJavaScriptObject(); |
| 136 | 136 |
| 137 // If the data slot isn't null, it represents either the list | 137 // If the data slot isn't null, it represents either the list |
| 138 // of keys (for non-upgraded JSON maps) or the upgraded map. | 138 // of keys (for non-upgraded JSON maps) or the upgraded map. |
| 139 var _data = null; | 139 var _data = null; |
| 140 | 140 |
| 141 _JsonMap(this._original); | 141 _JsonMap(this._original); |
| 142 | 142 |
| 143 operator[](Object key) { | 143 operator[](key) { |
| 144 if (_isUpgraded) { | 144 if (_isUpgraded) { |
| 145 return _upgradedMap[key]; | 145 return _upgradedMap[key]; |
| 146 } else if (key is !String) { | 146 } else if (key is !String) { |
| 147 return null; | 147 return null; |
| 148 } else { | 148 } else { |
| 149 var result = _getProperty(_processed, key); | 149 var result = _getProperty(_processed, key); |
| 150 if (_isUnprocessed(result)) result = _process(key); | 150 if (_isUnprocessed(result)) result = _process(key); |
| 151 return result; | 151 return result; |
| 152 } | 152 } |
| 153 } | 153 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 183 _upgrade()[key] = value; | 183 _upgrade()[key] = value; |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 void addAll(Map other) { | 187 void addAll(Map other) { |
| 188 other.forEach((key, value) { | 188 other.forEach((key, value) { |
| 189 this[key] = value; | 189 this[key] = value; |
| 190 }); | 190 }); |
| 191 } | 191 } |
| 192 | 192 |
| 193 bool containsValue(Object value) { | 193 bool containsValue(value) { |
| 194 if (_isUpgraded) return _upgradedMap.containsValue(value); | 194 if (_isUpgraded) return _upgradedMap.containsValue(value); |
| 195 List<String> keys = _computeKeys(); | 195 List<String> keys = _computeKeys(); |
| 196 for (int i = 0; i < keys.length; i++) { | 196 for (int i = 0; i < keys.length; i++) { |
| 197 String key = keys[i]; | 197 String key = keys[i]; |
| 198 if (this[key] == value) return true; | 198 if (this[key] == value) return true; |
| 199 } | 199 } |
| 200 return false; | 200 return false; |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool containsKey(Object key) { | 203 bool containsKey(key) { |
| 204 if (_isUpgraded) return _upgradedMap.containsKey(key); | 204 if (_isUpgraded) return _upgradedMap.containsKey(key); |
| 205 if (key is !String) return false; | 205 if (key is !String) return false; |
| 206 return _hasProperty(_original, key); | 206 return _hasProperty(_original, key); |
| 207 } | 207 } |
| 208 | 208 |
| 209 putIfAbsent(key, ifAbsent()) { | 209 putIfAbsent(key, ifAbsent()) { |
| 210 if (containsKey(key)) return this[key]; | 210 if (containsKey(key)) return this[key]; |
| 211 var value = ifAbsent(); | 211 var value = ifAbsent(); |
| 212 this[key] = value; | 212 this[key] = value; |
| 213 return value; | 213 return value; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 _sink.add(decoded); | 393 _sink.add(decoded); |
| 394 _sink.close(); | 394 _sink.close(); |
| 395 } | 395 } |
| 396 } | 396 } |
| 397 | 397 |
| 398 @patch class Utf8Decoder { | 398 @patch class Utf8Decoder { |
| 399 @patch | 399 @patch |
| 400 Converter<List<int>,dynamic> fuse(Converter<String, dynamic> next) { | 400 Converter<List<int>,dynamic> fuse(Converter<String, dynamic> next) { |
| 401 return super.fuse(next); | 401 return super.fuse(next); |
| 402 } | 402 } |
| 403 |
| 404 // Currently not intercepting UTF8 decoding. |
| 405 @patch |
| 406 static String _convertIntercepted(bool allowMalformed, List<int> codeUnits, |
| 407 int start, int end) { |
| 408 return null; // This call was not intercepted. |
| 409 } |
| 403 } | 410 } |
| OLD | NEW |