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 #library('json'); | |
6 #import('dart:coreimpl'); | |
7 | |
8 // TODO(jmesserly): this needs cleanup | |
9 // Ideally JS objects could be treated as Dart Maps directly, and then we can | |
10 // really use native JSON.parse and JSON.stringify. | |
11 | |
12 typedef Object _NativeJsonConvert(Object key, Object value); | |
13 | |
14 class _JSON native 'JSON' { | |
15 static Object parse(String jsonString, _NativeJsonConvert fn) native; | |
16 static String stringify(Object value, _NativeJsonConvert fn) native; | |
17 } | |
18 | |
19 | |
20 Object _getValue(obj, key) native 'return obj[key]'; | |
21 void _setValue(obj, key, value) native 'obj[key] = value'; | |
22 | |
23 // This is a guard against general Dart objects getting through to | |
24 // JSON.stringify. We restrict to JS primitives and JS Array. | |
25 bool _directToJson(obj) native | |
26 "return typeof obj != 'object' || obj == null || obj instanceof Array"; | |
27 | |
28 ListFactory<String> _jsKeys(Object obj) native ''' | |
29 if (obj != null && typeof obj == 'object' && !(obj instanceof Array)) { | |
30 return Object.keys(obj); | |
31 } | |
32 return null; | |
33 '''; | |
34 | |
35 /** | |
36 * Dart interface to JavaScript objects and JSON. | |
37 */ | |
38 class JSON { | |
39 /** | |
40 * Takes a string in JSON notation and returns the value it | |
41 * represents. The resulting value is one of the following: | |
42 * null | |
43 * a bool | |
44 * a double | |
45 * a String | |
46 * an Array of values (recursively) | |
47 * a Map from property names to values (recursively) | |
48 */ | |
49 static Object parse(String str) { | |
50 return _JSON.parse(str, (_, obj) { | |
51 final keys = _jsKeys(obj); | |
52 if (keys == null) return obj; | |
53 | |
54 // Note: only need to shallow convert here--JSON.parse handles the rest. | |
55 final map = {}; | |
56 for (String key in keys) { | |
57 map[key] = _getValue(obj, key); | |
58 } | |
59 return map; | |
60 }); | |
61 } | |
62 | |
63 /** | |
64 * Takes a value and returns a string in JSON notation | |
65 * representing its value, or returns null if the value is not representable | |
66 * in JSON. A representable value is one of the following: | |
67 * null | |
68 * a bool | |
69 * a double | |
70 * a String | |
71 * an Array of values (recursively) | |
72 * a Map from property names to values (recursively) | |
73 */ | |
74 // TODO(jmesserly): handle any List subtype? Right now it's converted as | |
75 // something like: | |
76 // {"0":1,"1":2} | |
77 static String stringify(Object value) { | |
78 return _JSON.stringify(value, (_, obj) { | |
79 if (_directToJson(obj)) return obj; | |
80 if (obj is Map<String, Dynamic>) { | |
81 Map<String, Dynamic> map = obj; | |
82 obj = new Object(); | |
83 map.forEach((k, v) => _setValue(obj, k, v)); | |
84 return obj; | |
85 } | |
86 throw new IllegalArgumentException('cannot convert "$value" to JSON'); | |
87 }); | |
88 } | |
89 } | |
OLD | NEW |