001    /**
002     * Copyright (c) 2000-present 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 Object get(String key) {
088                    return _jsonObject.opt(key);
089            }
090    
091            @Override
092            public boolean getBoolean(String key) {
093                    return _jsonObject.optBoolean(key);
094            }
095    
096            @Override
097            public boolean getBoolean(String key, boolean defaultValue) {
098                    return _jsonObject.optBoolean(key, defaultValue);
099            }
100    
101            @Override
102            public double getDouble(String key) {
103                    return _jsonObject.optDouble(key);
104            }
105    
106            @Override
107            public double getDouble(String key, double defaultValue) {
108                    return _jsonObject.optDouble(key, defaultValue);
109            }
110    
111            @Override
112            public int getInt(String key) {
113                    return _jsonObject.optInt(key);
114            }
115    
116            @Override
117            public int getInt(String key, int defaultValue) {
118                    return _jsonObject.optInt(key, defaultValue);
119            }
120    
121            @Override
122            public JSONArray getJSONArray(String key) {
123                    org.json.JSONArray jsonArray = _jsonObject.optJSONArray(key);
124    
125                    if (jsonArray == null) {
126                            return null;
127                    }
128    
129                    return new JSONArrayImpl(jsonArray);
130            }
131    
132            public org.json.JSONObject getJSONObject() {
133                    return _jsonObject;
134            }
135    
136            @Override
137            public JSONObject getJSONObject(String key) {
138                    org.json.JSONObject jsonObject = _jsonObject.optJSONObject(key);
139    
140                    if (jsonObject == null) {
141                            return null;
142                    }
143    
144                    return new JSONObjectImpl(jsonObject);
145            }
146    
147            @Override
148            public long getLong(String key) {
149                    return _jsonObject.optLong(key);
150            }
151    
152            @Override
153            public long getLong(String key, long defaultValue) {
154                    return _jsonObject.optLong(key, defaultValue);
155            }
156    
157            @Override
158            public String getString(String key) {
159                    return _jsonObject.optString(key);
160            }
161    
162            @Override
163            public String getString(String key, String defaultValue) {
164                    return _jsonObject.optString(key, defaultValue);
165            }
166    
167            @Override
168            public boolean has(String key) {
169                    return _jsonObject.has(key);
170            }
171    
172            @Override
173            public boolean isNull(String key) {
174                    return _jsonObject.isNull(key);
175            }
176    
177            @Override
178            public Iterator<String> keys() {
179                    return _jsonObject.keys();
180            }
181    
182            @Override
183            public int length() {
184                    return _jsonObject.length();
185            }
186    
187            @Override
188            public JSONArray names() {
189                    return new JSONArrayImpl(_jsonObject.names());
190            }
191    
192            @Override
193            public JSONObject put(String key, boolean value) {
194                    try {
195                            _jsonObject.put(key, value);
196                    }
197                    catch (Exception e) {
198                            if (_log.isWarnEnabled()) {
199                                    _log.warn(e, e);
200                            }
201                    }
202    
203                    return this;
204            }
205    
206            @Override
207            public JSONObject put(String key, Date value) {
208                    try {
209                            _jsonObject.put(key, value);
210                    }
211                    catch (Exception e) {
212                            if (_log.isWarnEnabled()) {
213                                    _log.warn(e, e);
214                            }
215                    }
216    
217                    return this;
218            }
219    
220            @Override
221            public JSONObject put(String key, double value) {
222                    try {
223                            _jsonObject.put(key, value);
224                    }
225                    catch (Exception e) {
226                            if (_log.isWarnEnabled()) {
227                                    _log.warn(e, e);
228                            }
229                    }
230    
231                    return this;
232            }
233    
234            @Override
235            public JSONObject put(String key, int value) {
236                    try {
237                            _jsonObject.put(key, value);
238                    }
239                    catch (Exception e) {
240                            if (_log.isWarnEnabled()) {
241                                    _log.warn(e, e);
242                            }
243                    }
244    
245                    return this;
246            }
247    
248            @Override
249            public JSONObject put(String key, JSONArray value) {
250                    try {
251                            _jsonObject.put(key, ((JSONArrayImpl)value).getJSONArray());
252                    }
253                    catch (Exception e) {
254                            if (_log.isWarnEnabled()) {
255                                    _log.warn(e, e);
256                            }
257                    }
258    
259                    return this;
260            }
261    
262            @Override
263            public JSONObject put(String key, JSONObject value) {
264                    try {
265                            _jsonObject.put(key, ((JSONObjectImpl)value).getJSONObject());
266                    }
267                    catch (Exception e) {
268                            if (_log.isWarnEnabled()) {
269                                    _log.warn(e, e);
270                            }
271                    }
272    
273                    return this;
274            }
275    
276            @Override
277            public JSONObject put(String key, long value) {
278                    try {
279                            _jsonObject.put(key, String.valueOf(value));
280                    }
281                    catch (Exception e) {
282                            if (_log.isWarnEnabled()) {
283                                    _log.warn(e, e);
284                            }
285                    }
286    
287                    return this;
288            }
289    
290            @Override
291            public JSONObject put(String key, Object value) {
292                    try {
293                            if (value instanceof JSONArray) {
294                                    put(key, (JSONArray)value);
295                            }
296                            else if (value instanceof JSONObject) {
297                                    put(key, (JSONObject)value);
298                            }
299                            else {
300                                    _jsonObject.put(key, value);
301                            }
302                    }
303                    catch (Exception e) {
304                            if (_log.isWarnEnabled()) {
305                                    _log.warn(e, e);
306                            }
307                    }
308    
309                    return this;
310            }
311    
312            @Override
313            public JSONObject put(String key, String value) {
314                    try {
315                            _jsonObject.put(key, value);
316                    }
317                    catch (Exception e) {
318                            if (_log.isWarnEnabled()) {
319                                    _log.warn(e, e);
320                            }
321                    }
322    
323                    return this;
324            }
325    
326            @Override
327            public JSONObject putException(Exception exception) {
328                    try {
329                            _jsonObject.put(
330                                    "exception",
331                                    exception.getClass() + StringPool.COLON +
332                                            exception.getMessage());
333                    }
334                    catch (Exception e) {
335                            if (_log.isWarnEnabled()) {
336                                    _log.warn(e, e);
337                            }
338                    }
339    
340                    return this;
341            }
342    
343            @Override
344            public void readExternal(ObjectInput objectInput) throws IOException {
345                    try {
346                            _jsonObject = new org.json.JSONObject(objectInput.readUTF());
347                    }
348                    catch (Exception e) {
349                            throw new IOException(e);
350                    }
351            }
352    
353            @Override
354            public Object remove(String key) {
355                    return _jsonObject.remove(key);
356            }
357    
358            @Override
359            public String toJSONString() {
360                    return toString();
361            }
362    
363            @Override
364            public String toString() {
365                    return _jsonObject.toString();
366            }
367    
368            @Override
369            public String toString(int indentFactor) throws JSONException {
370                    try {
371                            return _jsonObject.toString(indentFactor);
372                    }
373                    catch (Exception e) {
374                            throw new JSONException(e);
375                    }
376            }
377    
378            @Override
379            public Writer write(Writer writer) throws JSONException {
380                    try {
381                            return _jsonObject.write(writer);
382                    }
383                    catch (Exception e) {
384                            throw new JSONException(e);
385                    }
386            }
387    
388            @Override
389            public void writeExternal(ObjectOutput objectOutput) throws IOException {
390                    objectOutput.writeUTF(toString());
391            }
392    
393            private static final String _NULL_JSON = "{}";
394    
395            private static final Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
396    
397            private org.json.JSONObject _jsonObject;
398    
399    }