001
014
015 package com.liferay.portal.json;
016
017 import com.liferay.portal.kernel.json.JSONArray;
018 import com.liferay.portal.kernel.json.JSONException;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024
025 import java.io.Writer;
026
027 import java.util.Date;
028 import java.util.Iterator;
029 import java.util.Map;
030
031
034 public class JSONObjectImpl implements JSONObject {
035
036 public JSONObjectImpl() {
037 _jsonObject = new org.json.JSONObject();
038 }
039
040 public JSONObjectImpl(JSONObject jsonObject, String[] names)
041 throws JSONException {
042
043 try {
044 JSONObjectImpl jsonObjectImpl = (JSONObjectImpl)jsonObject;
045
046 _jsonObject = new org.json.JSONObject(
047 jsonObjectImpl.getJSONObject(), names);
048 }
049 catch (Exception e) {
050 throw new JSONException(e);
051 }
052 }
053
054 public JSONObjectImpl(Map<?, ?> map) {
055 _jsonObject = new org.json.JSONObject(map);
056 }
057
058 public JSONObjectImpl(Object bean) {
059 _jsonObject = new org.json.JSONObject(bean);
060 }
061
062 public JSONObjectImpl(Object obj, String[] names) {
063 _jsonObject = new org.json.JSONObject(obj, names);
064 }
065
066 public JSONObjectImpl(org.json.JSONObject jsonObject) {
067 _jsonObject = jsonObject;
068 }
069
070 public JSONObjectImpl(String json) throws JSONException {
071 try {
072 if (Validator.isNull(json)) {
073 json = _NULL_JSON;
074 }
075
076 _jsonObject = new org.json.JSONObject(json);
077 }
078 catch (Exception e) {
079 throw new JSONException(e);
080 }
081 }
082
083 @Override
084 public boolean getBoolean(String key) {
085 return _jsonObject.optBoolean(key);
086 }
087
088 @Override
089 public boolean getBoolean(String key, boolean defaultValue) {
090 return _jsonObject.optBoolean(key, defaultValue);
091 }
092
093 @Override
094 public double getDouble(String key) {
095 return _jsonObject.optDouble(key);
096 }
097
098 @Override
099 public double getDouble(String key, double defaultValue) {
100 return _jsonObject.optDouble(key, defaultValue);
101 }
102
103 @Override
104 public int getInt(String key) {
105 return _jsonObject.optInt(key);
106 }
107
108 @Override
109 public int getInt(String key, int defaultValue) {
110 return _jsonObject.optInt(key, defaultValue);
111 }
112
113 @Override
114 public JSONArray getJSONArray(String key) {
115 org.json.JSONArray jsonArray = _jsonObject.optJSONArray(key);
116
117 if (jsonArray == null) {
118 return null;
119 }
120
121 return new JSONArrayImpl(jsonArray);
122 }
123
124 public org.json.JSONObject getJSONObject() {
125 return _jsonObject;
126 }
127
128 @Override
129 public JSONObject getJSONObject(String key) {
130 org.json.JSONObject jsonObject = _jsonObject.optJSONObject(key);
131
132 if (jsonObject == null) {
133 return null;
134 }
135
136 return new JSONObjectImpl(jsonObject);
137 }
138
139 @Override
140 public long getLong(String key) {
141 return _jsonObject.optLong(key);
142 }
143
144 @Override
145 public long getLong(String key, long defaultValue) {
146 return _jsonObject.optLong(key, defaultValue);
147 }
148
149 @Override
150 public String getString(String key) {
151 return _jsonObject.optString(key);
152 }
153
154 @Override
155 public String getString(String key, String defaultValue) {
156 return _jsonObject.optString(key, defaultValue);
157 }
158
159 @Override
160 public boolean has(String key) {
161 return _jsonObject.has(key);
162 }
163
164 @Override
165 public boolean isNull(String key) {
166 return _jsonObject.isNull(key);
167 }
168
169 @Override
170 public Iterator<String> keys() {
171 return _jsonObject.keys();
172 }
173
174 @Override
175 public int length() {
176 return _jsonObject.length();
177 }
178
179 @Override
180 public JSONArray names() {
181 return new JSONArrayImpl(_jsonObject.names());
182 }
183
184 @Override
185 public JSONObject put(String key, boolean value) {
186 try {
187 _jsonObject.put(key, value);
188 }
189 catch (Exception e) {
190 if (_log.isWarnEnabled()) {
191 _log.warn(e, e);
192 }
193 }
194
195 return this;
196 }
197
198 @Override
199 public JSONObject put(String key, Date value) {
200 try {
201 _jsonObject.put(key, value);
202 }
203 catch (Exception e) {
204 if (_log.isWarnEnabled()) {
205 _log.warn(e, e);
206 }
207 }
208
209 return this;
210 }
211
212 @Override
213 public JSONObject put(String key, double value) {
214 try {
215 _jsonObject.put(key, value);
216 }
217 catch (Exception e) {
218 if (_log.isWarnEnabled()) {
219 _log.warn(e, e);
220 }
221 }
222
223 return this;
224 }
225
226 @Override
227 public JSONObject put(String key, int value) {
228 try {
229 _jsonObject.put(key, value);
230 }
231 catch (Exception e) {
232 if (_log.isWarnEnabled()) {
233 _log.warn(e, e);
234 }
235 }
236
237 return this;
238 }
239
240 @Override
241 public JSONObject put(String key, JSONArray value) {
242 try {
243 _jsonObject.put(key, ((JSONArrayImpl)value).getJSONArray());
244 }
245 catch (Exception e) {
246 if (_log.isWarnEnabled()) {
247 _log.warn(e, e);
248 }
249 }
250
251 return this;
252 }
253
254 @Override
255 public JSONObject put(String key, JSONObject value) {
256 try {
257 _jsonObject.put(key, ((JSONObjectImpl)value).getJSONObject());
258 }
259 catch (Exception e) {
260 if (_log.isWarnEnabled()) {
261 _log.warn(e, e);
262 }
263 }
264
265 return this;
266 }
267
268 @Override
269 public JSONObject put(String key, long value) {
270 try {
271 _jsonObject.put(key, value);
272 }
273 catch (Exception e) {
274 if (_log.isWarnEnabled()) {
275 _log.warn(e, e);
276 }
277 }
278
279 return this;
280 }
281
282 @Override
283 public JSONObject put(String key, String value) {
284 try {
285 _jsonObject.put(key, value);
286 }
287 catch (Exception e) {
288 if (_log.isWarnEnabled()) {
289 _log.warn(e, e);
290 }
291 }
292
293 return this;
294 }
295
296 @Override
297 public JSONObject putException(Exception exception) {
298 try {
299 _jsonObject.put(
300 "exception",
301 exception.getClass() + StringPool.COLON +
302 exception.getMessage());
303 }
304 catch (Exception e) {
305 if (_log.isWarnEnabled()) {
306 _log.warn(e, e);
307 }
308 }
309
310 return this;
311 }
312
313 @Override
314 public Object remove(String key) {
315 return _jsonObject.remove(key);
316 }
317
318 @Override
319 public String toString() {
320 return _jsonObject.toString();
321 }
322
323 @Override
324 public String toString(int indentFactor) throws JSONException {
325 try {
326 return _jsonObject.toString(indentFactor);
327 }
328 catch (Exception e) {
329 throw new JSONException(e);
330 }
331 }
332
333 @Override
334 public Writer write(Writer writer) throws JSONException {
335 try {
336 return _jsonObject.write(writer);
337 }
338 catch (Exception e) {
339 throw new JSONException(e);
340 }
341 }
342
343 private static final String _NULL_JSON = "{}";
344
345 private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
346
347 private org.json.JSONObject _jsonObject;
348
349 }