1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JSONObjectImpl implements JSONObject {
43  
44      public JSONObjectImpl() {
45          _jsonObj = new org.json.JSONObject();
46      }
47  
48      public JSONObjectImpl(JSONObject jsonObj, String[] names)
49          throws JSONException {
50  
51          try {
52              JSONObjectImpl jsonObjImpl = (JSONObjectImpl)jsonObj;
53  
54              _jsonObj = new org.json.JSONObject(
55                  jsonObjImpl.getJSONObject(), names);
56          }
57          catch (Exception e) {
58              throw new JSONException(e);
59          }
60      }
61  
62      public JSONObjectImpl(Map<?, ?> map) {
63          _jsonObj = new org.json.JSONObject(map);
64      }
65  
66      public JSONObjectImpl(Object bean) {
67          _jsonObj = new org.json.JSONObject(bean);
68      }
69  
70      public JSONObjectImpl(Object obj, String[] names) {
71          _jsonObj = new org.json.JSONObject(obj, names);
72      }
73  
74      public JSONObjectImpl(String json) throws JSONException {
75          try {
76              _jsonObj = new org.json.JSONObject(json);
77          }
78          catch (Exception e) {
79              throw new JSONException(e);
80          }
81      }
82  
83      public JSONObjectImpl(org.json.JSONObject jsonObj) {
84          _jsonObj = jsonObj;
85      }
86  
87      public boolean getBoolean(String key) {
88          return _jsonObj.optBoolean(key);
89      }
90  
91      public double getDouble(String key) {
92          return _jsonObj.optDouble(key);
93      }
94  
95      public int getInt(String key) {
96          return _jsonObj.optInt(key);
97      }
98  
99      public JSONArray getJSONArray(String key) {
100         org.json.JSONArray jsonArray = _jsonObj.optJSONArray(key);
101 
102         if (jsonArray == null) {
103             return null;
104         }
105 
106         return new JSONArrayImpl(jsonArray);
107     }
108 
109     public org.json.JSONObject getJSONObject() {
110         return _jsonObj;
111     }
112 
113     public JSONObject getJSONObject(String key) {
114         org.json.JSONObject jsonObj = _jsonObj.optJSONObject(key);
115 
116         if (jsonObj == null) {
117             return null;
118         }
119 
120         return new JSONObjectImpl(jsonObj);
121     }
122 
123     public long getLong(String key) {
124         return _jsonObj.optLong(key);
125     }
126 
127     public String getString(String key) {
128         return _jsonObj.optString(key);
129     }
130 
131     public boolean has(String key) {
132         return _jsonObj.has(key);
133     }
134 
135     public boolean isNull(String key) {
136         return _jsonObj.isNull(key);
137     }
138 
139     public Iterator<String> keys() {
140         return _jsonObj.keys();
141     }
142 
143     public int length() {
144         return _jsonObj.length();
145     }
146 
147     public JSONObject put(String key, boolean value) {
148         try {
149             _jsonObj.put(key, value);
150         }
151         catch (Exception e) {
152             if (_log.isWarnEnabled()) {
153                 _log.warn(e, e);
154             }
155         }
156 
157         return this;
158     }
159 
160     public JSONObject put(String key, double value) {
161         try {
162             _jsonObj.put(key, value);
163         }
164         catch (Exception e) {
165             if (_log.isWarnEnabled()) {
166                 _log.warn(e, e);
167             }
168         }
169 
170         return this;
171     }
172 
173     public JSONObject put(String key, int value) {
174         try {
175             _jsonObj.put(key, value);
176         }
177         catch (Exception e) {
178             if (_log.isWarnEnabled()) {
179                 _log.warn(e, e);
180             }
181         }
182 
183         return this;
184     }
185 
186     public JSONObject put(String key, long value) {
187         try {
188             _jsonObj.put(key, value);
189         }
190         catch (Exception e) {
191             if (_log.isWarnEnabled()) {
192                 _log.warn(e, e);
193             }
194         }
195 
196         return this;
197     }
198 
199     public JSONObject put(String key, Date value) {
200         try {
201             _jsonObj.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     public JSONObject put(String key, JSONArray value) {
213         try {
214             _jsonObj.put(key, ((JSONArrayImpl)value).getJSONArray());
215         }
216         catch (Exception e) {
217             if (_log.isWarnEnabled()) {
218                 _log.warn(e, e);
219             }
220         }
221 
222         return this;
223     }
224 
225     public JSONObject put(String key, JSONObject value) {
226         try {
227             _jsonObj.put(key, ((JSONObjectImpl)value).getJSONObject());
228         }
229         catch (Exception e) {
230             if (_log.isWarnEnabled()) {
231                 _log.warn(e, e);
232             }
233         }
234 
235         return this;
236     }
237 
238     public JSONObject put(String key, String value) {
239         try {
240             _jsonObj.put(key, value);
241         }
242         catch (Exception e) {
243             if (_log.isWarnEnabled()) {
244                 _log.warn(e, e);
245             }
246         }
247 
248         return this;
249     }
250 
251     public Object remove(String key) {
252         return _jsonObj.remove(key);
253     }
254 
255     public String toString() {
256         return _jsonObj.toString();
257     }
258 
259     public String toString(int indentFactor) throws JSONException {
260         try {
261             return _jsonObj.toString(indentFactor);
262         }
263         catch (Exception e) {
264             throw new JSONException(e);
265         }
266     }
267 
268     public Writer write(Writer writer) throws JSONException {
269         try {
270             return _jsonObj.write(writer);
271         }
272         catch (Exception e) {
273             throw new JSONException(e);
274         }
275     }
276 
277     private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
278 
279     private org.json.JSONObject _jsonObj;
280 
281 }