| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // JavaScript implementation of the native methods declared in json.dart. | 5 // JavaScript implementation of the native methods declared in json.dart. |
| 6 | 6 |
| 7 // TODO(jmesserly): this needs lots of cleanup. | 7 // TODO(jmesserly): this needs lots of cleanup. |
| 8 // Ideally JS objects could be treated as Dart Maps directly, and then we can | 8 // Ideally JS objects could be treated as Dart Maps directly, and then we can |
| 9 // really use native JSON.parse and JSON.stringify. | 9 // really use native JSON.parse and JSON.stringify. |
| 10 | 10 |
| 11 | 11 |
| 12 var $JSON = JSON; // Save the real JSON | 12 var $JSON = JSON; // Save the real JSON |
| 13 | 13 |
| 14 JSON = function() {}; | 14 JSON = function() {}; |
| 15 JSON.parse = function(str) { | 15 JSON.parse = function(str) { |
| 16 return $JSON.parse(str, function(k, v) { return $convertJsToDart(v); }); | 16 return $JSON.parse(str, function(k, v) { return $convertJsToDart(v); }); |
| 17 } | 17 } |
| 18 | 18 |
| 19 // Shallow-converts the parsed JavaScript value into a Dart object, and | 19 // Shallow-converts the parsed JavaScript value into a Dart object, and |
| 20 // returns the Dart object. | 20 // returns the Dart object. |
| 21 // Any component values have already been converted. | 21 // Any component values have already been converted. |
| 22 function $convertJsToDart(obj) { | 22 function $convertJsToDart(obj) { |
| 23 if (obj != null && typeof obj == 'object' && !(obj instanceof Array)) { | 23 if (obj != null && typeof obj == 'object' && !(obj instanceof Array)) { |
| 24 return $fixupJsObjectToDartMap(obj); | 24 return $fixupJsObjectToDartMap(obj); |
| 25 } | 25 } |
| 26 return obj; |
| 26 } | 27 } |
| 27 | 28 |
| 28 // Converts the parsed JavaScript Object into a Dart Map. | 29 // Converts the parsed JavaScript Object into a Dart Map. |
| 29 function $fixupJsObjectToDartMap(obj) { | 30 function $fixupJsObjectToDartMap(obj) { |
| 30 var map = new HashMapImplementation(); | 31 var map = new HashMapImplementation(); |
| 31 var keys = Object.keys(obj); | 32 var keys = Object.keys(obj); |
| 32 for (var i = 0; i < keys.length; i++) { | 33 for (var i = 0; i < keys.length; i++) { |
| 33 map.$setindex(keys[i], obj[keys[i]]); | 34 map.$setindex(keys[i], obj[keys[i]]); |
| 34 } | 35 } |
| 35 return map; | 36 return map; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 // Converts only shallowly. | 77 // Converts only shallowly. |
| 77 function $convertDartMapToJsObject(map) { | 78 function $convertDartMapToJsObject(map) { |
| 78 var obj = {}; | 79 var obj = {}; |
| 79 var propertyNames = map.getKeys(); | 80 var propertyNames = map.getKeys(); |
| 80 for (var i = 0, len = propertyNames.length; i < len; i++) { | 81 for (var i = 0, len = propertyNames.length; i < len; i++) { |
| 81 var propertyName = propertyNames[i]; | 82 var propertyName = propertyNames[i]; |
| 82 obj[propertyName] = map.$index(propertyName); | 83 obj[propertyName] = map.$index(propertyName); |
| 83 } | 84 } |
| 84 return obj; | 85 return obj; |
| 85 } | 86 } |
| OLD | NEW |