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