001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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, Object value) {
287                    try {
288                            if (value instanceof JSONArray) {
289                                    put(key, (JSONArray)value);
290                            }
291                            else if (value instanceof JSONObject) {
292                                    put(key, (JSONObject)value);
293                            }
294                            else {
295                                    _jsonObject.put(key, value);
296                            }
297                    }
298                    catch (Exception e) {
299                            if (_log.isWarnEnabled()) {
300                                    _log.warn(e, e);
301                            }
302                    }
303    
304                    return this;
305            }
306    
307            @Override
308            public JSONObject put(String key, String value) {
309                    try {
310                            _jsonObject.put(key, value);
311                    }
312                    catch (Exception e) {
313                            if (_log.isWarnEnabled()) {
314                                    _log.warn(e, e);
315                            }
316                    }
317    
318                    return this;
319            }
320    
321            @Override
322            public JSONObject putException(Exception exception) {
323                    try {
324                            _jsonObject.put(
325                                    "exception",
326                                    exception.getClass() + StringPool.COLON +
327                                            exception.getMessage());
328                    }
329                    catch (Exception e) {
330                            if (_log.isWarnEnabled()) {
331                                    _log.warn(e, e);
332                            }
333                    }
334    
335                    return this;
336            }
337    
338            @Override
339            public void readExternal(ObjectInput objectInput) throws IOException {
340                    try {
341                            _jsonObject = new org.json.JSONObject(
342                                    (String)objectInput.readObject());
343                    }
344                    catch (Exception e) {
345                            throw new IOException(e);
346                    }
347            }
348    
349            @Override
350            public Object remove(String key) {
351                    return _jsonObject.remove(key);
352            }
353    
354            @Override
355            public String toString() {
356                    return _jsonObject.toString();
357            }
358    
359            @Override
360            public String toString(int indentFactor) throws JSONException {
361                    try {
362                            return _jsonObject.toString(indentFactor);
363                    }
364                    catch (Exception e) {
365                            throw new JSONException(e);
366                    }
367            }
368    
369            @Override
370            public Writer write(Writer writer) throws JSONException {
371                    try {
372                            return _jsonObject.write(writer);
373                    }
374                    catch (Exception e) {
375                            throw new JSONException(e);
376                    }
377            }
378    
379            @Override
380            public void writeExternal(ObjectOutput objectOutput) throws IOException {
381                    objectOutput.writeObject(toString());
382            }
383    
384            private static final String _NULL_JSON = "{}";
385    
386            private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
387    
388            private org.json.JSONObject _jsonObject;
389    
390    }