| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #library('json'); | 5 #library('json'); |
| 6 #import('dart:coreimpl'); | 6 #import('dart:coreimpl'); |
| 7 | 7 |
| 8 // TODO(jmesserly): this needs cleanup | 8 // TODO(jmesserly): this needs cleanup |
| 9 // Ideally JS objects could be treated as Dart Maps directly, and then we can | 9 // Ideally JS objects could be treated as Dart Maps directly, and then we can |
| 10 // really use native JSON.parse and JSON.stringify. | 10 // really use native JSON.parse and JSON.stringify. |
| 11 | 11 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 32 return null; | 32 return null; |
| 33 '''; | 33 '''; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * Dart interface to JavaScript objects and JSON. | 36 * Dart interface to JavaScript objects and JSON. |
| 37 */ | 37 */ |
| 38 class JSON { | 38 class JSON { |
| 39 /** | 39 /** |
| 40 * Takes a string in JSON notation and returns the value it | 40 * Takes a string in JSON notation and returns the value it |
| 41 * represents. The resulting value is one of the following: | 41 * represents. The resulting value is one of the following: |
| 42 * null | 42 * * null |
| 43 * a bool | 43 * * a bool |
| 44 * a double | 44 * * a double |
| 45 * a String | 45 * * a String |
| 46 * an Array of values (recursively) | 46 * * an Array of values (recursively) |
| 47 * a Map from property names to values (recursively) | 47 * * a Map from property names to values (recursively) |
| 48 */ | 48 */ |
| 49 static Object parse(String str) { | 49 static Object parse(String str) { |
| 50 return _JSON.parse(str, (_, obj) { | 50 return _JSON.parse(str, (_, obj) { |
| 51 final keys = _jsKeys(obj); | 51 final keys = _jsKeys(obj); |
| 52 if (keys == null) return obj; | 52 if (keys == null) return obj; |
| 53 | 53 |
| 54 // Note: only need to shallow convert here--JSON.parse handles the rest. | 54 // Note: only need to shallow convert here--JSON.parse handles the rest. |
| 55 final map = {}; | 55 final map = {}; |
| 56 for (String key in keys) { | 56 for (String key in keys) { |
| 57 map[key] = _getValue(obj, key); | 57 map[key] = _getValue(obj, key); |
| 58 } | 58 } |
| 59 return map; | 59 return map; |
| 60 }); | 60 }); |
| 61 } | 61 } |
| 62 | 62 |
| 63 /** | 63 /** |
| 64 * Takes a value and returns a string in JSON notation | 64 * Takes a value and returns a string in JSON notation |
| 65 * representing its value, or returns null if the value is not representable | 65 * representing its value, or returns null if the value is not representable |
| 66 * in JSON. A representable value is one of the following: | 66 * in JSON. A representable value is one of the following: |
| 67 * null | 67 * * null |
| 68 * a bool | 68 * * a bool |
| 69 * a double | 69 * * a double |
| 70 * a String | 70 * * a String |
| 71 * an Array of values (recursively) | 71 * * an Array of values (recursively) |
| 72 * a Map from property names to values (recursively) | 72 * * a Map from property names to values (recursively) |
| 73 */ | 73 */ |
| 74 // TODO(jmesserly): handle any List subtype? Right now it's converted as | 74 // TODO(jmesserly): handle any List subtype? Right now it's converted as |
| 75 // something like: | 75 // something like: |
| 76 // {"0":1,"1":2} | 76 // {"0":1,"1":2} |
| 77 static String stringify(Object value) { | 77 static String stringify(Object value) { |
| 78 return _JSON.stringify(value, (_, obj) { | 78 return _JSON.stringify(value, (_, obj) { |
| 79 if (_directToJson(obj)) return obj; | 79 if (_directToJson(obj)) return obj; |
| 80 if (obj is Map<String, Dynamic>) { | 80 if (obj is Map<String, Dynamic>) { |
| 81 Map<String, Dynamic> map = obj; | 81 Map<String, Dynamic> map = obj; |
| 82 obj = new Object(); | 82 obj = new Object(); |
| 83 map.forEach((k, v) => _setValue(obj, k, v)); | 83 map.forEach((k, v) => _setValue(obj, k, v)); |
| 84 return obj; | 84 return obj; |
| 85 } | 85 } |
| 86 throw new IllegalArgumentException('cannot convert "$value" to JSON'); | 86 throw new IllegalArgumentException('cannot convert "$value" to JSON'); |
| 87 }); | 87 }); |
| 88 } | 88 } |
| 89 } | 89 } |
| OLD | NEW |