| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // JavaScript implementation of the native methods declared in json.dart. | |
| 6 | |
| 7 // TODO(jmesserly): this needs lots of cleanup. | |
| 8 // Ideally JS objects could be treated as Dart Maps directly, and then we can | |
| 9 // really use native JSON.parse and JSON.stringify. | |
| 10 | |
| 11 | |
| 12 var $JSON = JSON; // Save the real JSON | |
| 13 | |
| 14 JSON = function() {}; | |
| 15 JSON.parse = function(str) { | |
| 16 return $JSON.parse(str, function(k, v) { return $convertJsToDart(v); }); | |
| 17 } | |
| 18 | |
| 19 // Shallow-converts the parsed JavaScript value into a Dart object, and | |
| 20 // returns the Dart object. | |
| 21 // Any component values have already been converted. | |
| 22 function $convertJsToDart(obj) { | |
| 23 if (obj != null && typeof obj == 'object' && !(obj instanceof Array)) { | |
| 24 return $fixupJsObjectToDartMap(obj); | |
| 25 } | |
| 26 return obj; | |
| 27 } | |
| 28 | |
| 29 // Converts the parsed JavaScript Object into a Dart Map. | |
| 30 function $fixupJsObjectToDartMap(obj) { | |
| 31 var map = new HashMapImplementation(); | |
| 32 var keys = Object.keys(obj); | |
| 33 for (var i = 0; i < keys.length; i++) { | |
| 34 map.$setindex(keys[i], obj[keys[i]]); | |
| 35 } | |
| 36 return map; | |
| 37 } | |
| 38 | |
| 39 /////////////////////////////////////////////////////////////////////////////// | |
| 40 | |
| 41 JSON.stringify = function(obj) { | |
| 42 return $JSON.stringify(obj, $convertDartToJs); | |
| 43 } | |
| 44 | |
| 45 // TODO(jmesserly): better error message! | |
| 46 function UnconvertibleException() {} | |
| 47 | |
| 48 // Converts the Dart object into a JavaScript value, suitable for applying | |
| 49 // JSON.stringify to. | |
| 50 // Throws UnconvertibleException if the Dart value is not convertible. | |
| 51 function $convertDartToJs(key, obj) { | |
| 52 // Only Dart Maps need to be converted into JavaScript objects. | |
| 53 if (obj === null || obj === undefined) { | |
| 54 return null; | |
| 55 } | |
| 56 switch (typeof obj) { | |
| 57 case 'boolean': | |
| 58 case 'number': | |
| 59 case 'string': | |
| 60 return obj; | |
| 61 case 'object': | |
| 62 if (obj instanceof Array) { | |
| 63 return obj; | |
| 64 } else { | |
| 65 try { | |
| 66 return $convertDartMapToJsObject(obj); | |
| 67 } catch (e) { | |
| 68 // TODO(jmesserly): HACK: assume a failure means it's not a Map, and | |
| 69 // throw UnconvertibleException below... | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 $throw(new UnconvertibleException()); | |
| 74 } | |
| 75 | |
| 76 // Converts the Dart Map into a JavaScript object. | |
| 77 // Converts only shallowly. | |
| 78 function $convertDartMapToJsObject(map) { | |
| 79 var obj = {}; | |
| 80 var propertyNames = map.getKeys(); | |
| 81 for (var i = 0, len = propertyNames.length; i < len; i++) { | |
| 82 var propertyName = propertyNames[i]; | |
| 83 obj[propertyName] = map.$index(propertyName); | |
| 84 } | |
| 85 return obj; | |
| 86 } | |
| OLD | NEW |