001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.IOException;
026    import java.io.ObjectInput;
027    import java.io.ObjectOutput;
028    import java.io.Writer;
029    
030    import java.util.Date;
031    import java.util.Iterator;
032    import java.util.Map;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class JSONObjectImpl implements JSONObject {
038    
039            public JSONObjectImpl() {
040                    _jsonObject = new org.json.JSONObject();
041            }
042    
043            public JSONObjectImpl(JSONObject jsonObject, String[] names)
044                    throws JSONException {
045    
046                    try {
047                            JSONObjectImpl jsonObjectImpl = (JSONObjectImpl)jsonObject;
048    
049                            _jsonObject = new org.json.JSONObject(
050                                    jsonObjectImpl.getJSONObject(), names);
051                    }
052                    catch (Exception e) {
053                            throw new JSONException(e);
054                    }
055            }
056    
057            public JSONObjectImpl(Map<?, ?> map) {
058                    _jsonObject = new org.json.JSONObject(map);
059            }
060    
061            public JSONObjectImpl(Object bean) {
062                    _jsonObject = new org.json.JSONObject(bean);
063            }
064    
065            public JSONObjectImpl(Object obj, String[] names) {
066                    _jsonObject = new org.json.JSONObject(obj, names);
067            }
068    
069            public JSONObjectImpl(org.json.JSONObject jsonObject) {
070                    _jsonObject = jsonObject;
071            }
072    
073            public JSONObjectImpl(String json) throws JSONException {
074                    try {
075                            if (Validator.isNull(json)) {
076                                    json = _NULL_JSON;
077                            }
078    
079                            _jsonObject = new org.json.JSONObject(json);
080                    }
081                    catch (Exception e) {
082                            throw new JSONException(e);
083                    }
084            }
085    
086            @Override
087            public boolean getBoolean(String key) {
088                    return _jsonObject.optBoolean(key);
089            }
090    
091            @Override
092            public boolean getBoolean(String key, boolean defaultValue) {
093                    return _jsonObject.optBoolean(key, defaultValue);
094            }
095    
096            @Override
097            public double getDouble(String key) {
098                    return _jsonObject.optDouble(key);
099            }
100    
101            @Override
102            public double getDouble(String key, double defaultValue) {
103                    return _jsonObject.optDouble(key, defaultValue);
104            }
105    
106            @Override
107            public int getInt(String key) {
108                    return _jsonObject.optInt(key);
109            }
110    
111            @Override
112            public int getInt(String key, int defaultValue) {
113                    return _jsonObject.optInt(key, defaultValue);
114            }
115    
116            @Override
117            public JSONArray getJSONArray(String key) {
118                    org.json.JSONArray jsonArray = _jsonObject.optJSONArray(key);
119    
120                    if (jsonArray == null) {
121                            return null;
122                    }
123    
124                    return new JSONArrayImpl(jsonArray);
125            }
126    
127            public org.json.JSONObject getJSONObject() {
128                    return _jsonObject;
129            }
130    
131            @Override
132            public JSONObject getJSONObject(String key) {
133                    org.json.JSONObject jsonObject = _jsonObject.optJSONObject(key);
134    
135                    if (jsonObject == null) {
136                            return null;
137                    }
138    
139                    return new JSONObjectImpl(jsonObject);
140            }
141    
142            @Override
143            public long getLong(String key) {
144                    return _jsonObject.optLong(key);
145            }
146    
147            @Override
148            public long getLong(String key, long defaultValue) {
149                    return _jsonObject.optLong(key, defaultValue);
150            }
151    
152            @Override
153            public String getString(String key) {
154                    return _jsonObject.optString(key);
155            }
156    
157            @Override
158            public String getString(String key, String defaultValue) {
159                    return _jsonObject.optString(key, defaultValue);
160            }
161    
162            @Override
163            public boolean has(String key) {
164                    return _jsonObject.has(key);
165            }
166    
167            @Override
168            public boolean isNull(String key) {
169                    return _jsonObject.isNull(key);
170            }
171    
172            @Override
173            public Iterator<String> keys() {
174                    return _jsonObject.keys();
175            }
176    
177            @Override
178            public int length() {
179                    return _jsonObject.length();
180            }
181    
182            @Override
183            public JSONArray names() {
184                    return new JSONArrayImpl(_jsonObject.names());
185            }
186    
187            @Override
188            public JSONObject put(String key, boolean value) {
189                    try {
190                            _jsonObject.put(key, value);
191                    }
192                    catch (Exception e) {
193                            if (_log.isWarnEnabled()) {
194                                    _log.warn(e, e);
195                            }
196                    }
197    
198                    return this;
199            }
200    
201            @Override
202            public JSONObject put(String key, Date value) {
203                    try {
204                            _jsonObject.put(key, value);
205                    }
206                    catch (Exception e) {
207                            if (_log.isWarnEnabled()) {
208                                    _log.warn(e, e);
209                            }
210                    }
211    
212                    return this;
213            }
214    
215            @Override
216            public JSONObject put(String key, double value) {
217                    try {
218                            _jsonObject.put(key, value);
219                    }
220                    catch (Exception e) {
221                            if (_log.isWarnEnabled()) {
222                                    _log.warn(e, e);
223                            }
224                    }
225    
226                    return this;
227            }
228    
229            @Override
230            public JSONObject put(String key, int value) {
231                    try {
232                            _jsonObject.put(key, value);
233                    }
234                    catch (Exception e) {
235                            if (_log.isWarnEnabled()) {
236                                    _log.warn(e, e);
237                            }
238                    }
239    
240                    return this;
241            }
242    
243            @Override
244            public JSONObject put(String key, JSONArray value) {
245                    try {
246                            _jsonObject.put(key, ((JSONArrayImpl)value).getJSONArray());
247                    }
248                    catch (Exception e) {
249                            if (_log.isWarnEnabled()) {
250                                    _log.warn(e, e);
251                            }
252                    }
253    
254                    return this;
255            }
256    
257            @Override
258            public JSONObject put(String key, JSONObject value) {
259                    try {
260                            _jsonObject.put(key, ((JSONObjectImpl)value).getJSONObject());
261                    }
262                    catch (Exception e) {
263                            if (_log.isWarnEnabled()) {
264                                    _log.warn(e, e);
265                            }
266                    }
267    
268                    return this;
269            }
270    
271            @Override
272            public JSONObject put(String key, long value) {
273                    try {
274                            _jsonObject.put(key, value);
275                    }
276                    catch (Exception e) {
277                            if (_log.isWarnEnabled()) {
278                                    _log.warn(e, e);
279                            }
280                    }
281    
282                    return this;
283            }
284    
285            @Override
286            public JSONObject put(String key, String value) {
287                    try {
288                            _jsonObject.put(key, value);
289                    }
290                    catch (Exception e) {
291                            if (_log.isWarnEnabled()) {
292                                    _log.warn(e, e);
293                            }
294                    }
295    
296                    return this;
297            }
298    
299            @Override
300            public JSONObject putException(Exception exception) {
301                    try {
302                            _jsonObject.put(
303                                    "exception",
304                                    exception.getClass() + StringPool.COLON +
305                                            exception.getMessage());
306                    }
307                    catch (Exception e) {
308                            if (_log.isWarnEnabled()) {
309                                    _log.warn(e, e);
310                            }
311                    }
312    
313                    return this;
314            }
315    
316            @Override
317            public void readExternal(ObjectInput objectInput) throws IOException {
318                    try {
319                            _jsonObject = new org.json.JSONObject(objectInput.readUTF());
320                    }
321                    catch (Exception e) {
322                            throw new IOException(e);
323                    }
324            }
325    
326            @Override
327            public Object remove(String key) {
328                    return _jsonObject.remove(key);
329            }
330    
331            @Override
332            public String toString() {
333                    return _jsonObject.toString();
334            }
335    
336            @Override
337            public String toString(int indentFactor) throws JSONException {
338                    try {
339                            return _jsonObject.toString(indentFactor);
340                    }
341                    catch (Exception e) {
342                            throw new JSONException(e);
343                    }
344            }
345    
346            @Override
347            public Writer write(Writer writer) throws JSONException {
348                    try {
349                            return _jsonObject.write(writer);
350                    }
351                    catch (Exception e) {
352                            throw new JSONException(e);
353                    }
354            }
355    
356            @Override
357            public void writeExternal(ObjectOutput objectOutput) throws IOException {
358                    objectOutput.writeUTF(toString());
359            }
360    
361            private static final String _NULL_JSON = "{}";
362    
363            private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
364    
365            private org.json.JSONObject _jsonObject;
366    
367    }