1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts.json;
20
21 import java.util.Iterator;
22
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 import org.json.JSONStringer;
26 import org.mozilla.javascript.Context;
27 import org.mozilla.javascript.IdScriptableObject;
28 import org.mozilla.javascript.NativeArray;
29 import org.mozilla.javascript.NativeJavaObject;
30 import org.mozilla.javascript.NativeObject;
31 import org.springframework.extensions.surf.exception.WebScriptsPlatformException;
32
33
34
35
36
37
38
39 public class JSONUtils
40 {
41
42
43
44
45
46
47
48 public String toJSONString(Object object)
49 throws JSONException
50 {
51 JSONStringer json = new JSONStringer();
52
53 if (object instanceof NativeArray)
54 {
55 nativeArrayToJSONString((NativeArray)object, json);
56 }
57 else if (object instanceof NativeObject)
58 {
59 nativeObjectToJSONString((NativeObject)object, json);
60 }
61 else
62 {
63
64 throw new WebScriptsPlatformException("Only native objects and arrays are currently supported by the toJSONString method.");
65 }
66
67 return json.toString();
68 }
69
70
71
72
73
74
75
76
77 public NativeObject toObject(String jsonString)
78 throws JSONException
79 {
80
81
82
83 JSONObject jsonObject = new JSONObject(jsonString);
84
85
86 return toObject(jsonObject);
87 }
88
89
90
91
92
93
94
95
96 public NativeObject toObject(JSONObject jsonObject)
97 throws JSONException
98 {
99
100 NativeObject object = new NativeObject();
101
102 Iterator<String> keys = jsonObject.keys();
103 while (keys.hasNext())
104 {
105 String key = (String)keys.next();
106 Object value = jsonObject.get(key);
107 if (value instanceof JSONObject)
108 {
109 object.put(key, object, toObject((JSONObject)value));
110 }
111 else
112 {
113 object.put(key, object, value);
114 }
115 }
116
117 return object;
118 }
119
120
121
122
123
124
125
126
127 private void nativeObjectToJSONString(NativeObject nativeObject, JSONStringer json)
128 throws JSONException
129 {
130 json.object();
131
132 Object[] ids = nativeObject.getIds();
133 for (Object id : ids)
134 {
135 String key = id.toString();
136 json.key(key);
137
138 Object value = nativeObject.get(key, nativeObject);
139 valueToJSONString(value, json);
140 }
141
142 json.endObject();
143 }
144
145
146
147
148
149
150
151 private void nativeArrayToJSONString(NativeArray nativeArray, JSONStringer json)
152 throws JSONException
153 {
154 Object[] propIds = nativeArray.getIds();
155 if (isArray(propIds) == true)
156 {
157 json.array();
158
159 for (int i=0; i<propIds.length; i++)
160 {
161 Object propId = propIds[i];
162 if (propId instanceof Integer)
163 {
164 Object value = nativeArray.get((Integer)propId, nativeArray);
165 valueToJSONString(value, json);
166 }
167 }
168
169 json.endArray();
170 }
171 else
172 {
173 json.object();
174
175 for (Object propId : propIds)
176 {
177 Object value = nativeArray.get(propId.toString(), nativeArray);
178 json.key(propId.toString());
179 valueToJSONString(value, json);
180 }
181
182 json.endObject();
183 }
184 }
185
186
187
188
189
190
191
192 private boolean isArray(Object[] ids)
193 {
194 boolean result = true;
195 for (Object id : ids)
196 {
197 if (id instanceof Integer == false)
198 {
199 result = false;
200 break;
201 }
202 }
203 return result;
204 }
205
206
207
208
209
210
211
212
213 private void valueToJSONString(Object value, JSONStringer json)
214 throws JSONException
215 {
216 if (value instanceof IdScriptableObject &&
217 ((IdScriptableObject)value).getClassName().equals("Date") == true)
218 {
219
220 Object year = NativeObject.callMethod((IdScriptableObject)value, "getUTCFullYear", null);
221 Object month = NativeObject.callMethod((IdScriptableObject)value, "getUTCMonth", null);
222 Object date = NativeObject.callMethod((IdScriptableObject)value, "getUTCDate", null);
223 Object hours = NativeObject.callMethod((IdScriptableObject)value, "getUTCHours", null);
224 Object minutes = NativeObject.callMethod((IdScriptableObject)value, "getUTCMinutes", null);
225 Object seconds = NativeObject.callMethod((IdScriptableObject)value, "getUTCSeconds", null);
226 Object milliSeconds = NativeObject.callMethod((IdScriptableObject)value, "getUTCMilliseconds", null);
227
228
229 json.object()
230 .key("zone").value("UTC")
231 .key("year").value(year)
232 .key("month").value(month)
233 .key("date").value(date)
234 .key("hours").value(hours)
235 .key("minutes").value(minutes)
236 .key("seconds").value(seconds)
237 .key("milliseconds").value(milliSeconds)
238 .endObject();
239
240 }
241 else if (value instanceof NativeJavaObject)
242 {
243 Object javaValue = Context.jsToJava(value, Object.class);
244 json.value(javaValue);
245 }
246 else if (value instanceof NativeArray)
247 {
248
249 nativeArrayToJSONString((NativeArray)value, json);
250 }
251 else if (value instanceof NativeObject)
252 {
253
254 nativeObjectToJSONString((NativeObject)value, json);
255 }
256 else
257 {
258 json.value(value);
259 }
260 }
261
262
263
264
265
266
267
268 public Object encodeJSONString(Object value)
269 {
270 if (value instanceof String)
271 {
272 return JSONWriter.encodeJSONString((String)value);
273 }
274 else
275 {
276 return value;
277 }
278 }
279 }