1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.json;
24  
25  import com.liferay.portal.kernel.json.JSONArray;
26  import com.liferay.portal.kernel.json.JSONException;
27  import com.liferay.portal.kernel.json.JSONObject;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  
31  import java.io.Writer;
32  
33  import java.util.Date;
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  /**
38   * <a href="JSONObjectImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class JSONObjectImpl implements JSONObject {
44  
45      public JSONObjectImpl() {
46          _jsonObj = new org.json.JSONObject();
47      }
48  
49      public JSONObjectImpl(JSONObject jsonObj, String[] names)
50          throws JSONException {
51  
52          try {
53              JSONObjectImpl jsonObjImpl = (JSONObjectImpl)jsonObj;
54  
55              _jsonObj = new org.json.JSONObject(
56                  jsonObjImpl.getJSONObject(), names);
57          }
58          catch (Exception e) {
59              throw new JSONException(e);
60          }
61      }
62  
63      public JSONObjectImpl(Map<?, ?> map) {
64          _jsonObj = new org.json.JSONObject(map);
65      }
66  
67      public JSONObjectImpl(Object bean) {
68          _jsonObj = new org.json.JSONObject(bean);
69      }
70  
71      public JSONObjectImpl(Object obj, String[] names) {
72          _jsonObj = new org.json.JSONObject(obj, names);
73      }
74  
75      public JSONObjectImpl(String json) throws JSONException {
76          try {
77              _jsonObj = new org.json.JSONObject(json);
78          }
79          catch (Exception e) {
80              throw new JSONException(e);
81          }
82      }
83  
84      public JSONObjectImpl(org.json.JSONObject jsonObj) {
85          _jsonObj = jsonObj;
86      }
87  
88      public boolean getBoolean(String key) {
89          return _jsonObj.optBoolean(key);
90      }
91  
92      public double getDouble(String key) {
93          return _jsonObj.optDouble(key);
94      }
95  
96      public int getInt(String key) {
97          return _jsonObj.optInt(key);
98      }
99  
100     public JSONArray getJSONArray(String key) {
101         org.json.JSONArray jsonArray = _jsonObj.optJSONArray(key);
102 
103         if (jsonArray == null) {
104             return null;
105         }
106 
107         return new JSONArrayImpl(jsonArray);
108     }
109 
110     public org.json.JSONObject getJSONObject() {
111         return _jsonObj;
112     }
113 
114     public JSONObject getJSONObject(String key) {
115         org.json.JSONObject jsonObj = _jsonObj.optJSONObject(key);
116 
117         if (jsonObj == null) {
118             return null;
119         }
120 
121         return new JSONObjectImpl(jsonObj);
122     }
123 
124     public long getLong(String key) {
125         return _jsonObj.optLong(key);
126     }
127 
128     public String getString(String key) {
129         return _jsonObj.optString(key);
130     }
131 
132     public boolean has(String key) {
133         return _jsonObj.has(key);
134     }
135 
136     public boolean isNull(String key) {
137         return _jsonObj.isNull(key);
138     }
139 
140     public Iterator<String> keys() {
141         return _jsonObj.keys();
142     }
143 
144     public int length() {
145         return _jsonObj.length();
146     }
147 
148     public JSONObject put(String key, boolean value) {
149         try {
150             _jsonObj.put(key, value);
151         }
152         catch (Exception e) {
153             if (_log.isWarnEnabled()) {
154                 _log.warn(e, e);
155             }
156         }
157 
158         return this;
159     }
160 
161     public JSONObject put(String key, double value) {
162         try {
163             _jsonObj.put(key, value);
164         }
165         catch (Exception e) {
166             if (_log.isWarnEnabled()) {
167                 _log.warn(e, e);
168             }
169         }
170 
171         return this;
172     }
173 
174     public JSONObject put(String key, int value) {
175         try {
176             _jsonObj.put(key, value);
177         }
178         catch (Exception e) {
179             if (_log.isWarnEnabled()) {
180                 _log.warn(e, e);
181             }
182         }
183 
184         return this;
185     }
186 
187     public JSONObject put(String key, long value) {
188         try {
189             _jsonObj.put(key, value);
190         }
191         catch (Exception e) {
192             if (_log.isWarnEnabled()) {
193                 _log.warn(e, e);
194             }
195         }
196 
197         return this;
198     }
199 
200     public JSONObject put(String key, Date value) {
201         try {
202             _jsonObj.put(key, value);
203         }
204         catch (Exception e) {
205             if (_log.isWarnEnabled()) {
206                 _log.warn(e, e);
207             }
208         }
209 
210         return this;
211     }
212 
213     public JSONObject put(String key, JSONArray value) {
214         try {
215             _jsonObj.put(key, ((JSONArrayImpl)value).getJSONArray());
216         }
217         catch (Exception e) {
218             if (_log.isWarnEnabled()) {
219                 _log.warn(e, e);
220             }
221         }
222 
223         return this;
224     }
225 
226     public JSONObject put(String key, JSONObject value) {
227         try {
228             _jsonObj.put(key, ((JSONObjectImpl)value).getJSONObject());
229         }
230         catch (Exception e) {
231             if (_log.isWarnEnabled()) {
232                 _log.warn(e, e);
233             }
234         }
235 
236         return this;
237     }
238 
239     public JSONObject put(String key, String value) {
240         try {
241             _jsonObj.put(key, value);
242         }
243         catch (Exception e) {
244             if (_log.isWarnEnabled()) {
245                 _log.warn(e, e);
246             }
247         }
248 
249         return this;
250     }
251 
252     public Object remove(String key) {
253         return _jsonObj.remove(key);
254     }
255 
256     public String toString() {
257         return _jsonObj.toString();
258     }
259 
260     public String toString(int indentFactor) throws JSONException {
261         try {
262             return _jsonObj.toString(indentFactor);
263         }
264         catch (Exception e) {
265             throw new JSONException(e);
266         }
267     }
268 
269     public Writer write(Writer writer) throws JSONException {
270         try {
271             return _jsonObj.write(writer);
272         }
273         catch (Exception e) {
274             throw new JSONException(e);
275         }
276     }
277 
278     private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
279 
280     private org.json.JSONObject _jsonObj;
281 
282 }