1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 JSONObject put(String key, boolean value) {
140         try {
141             _jsonObj.put(key, value);
142         }
143         catch (Exception e) {
144             if (_log.isWarnEnabled()) {
145                 _log.warn(e, e);
146             }
147         }
148 
149         return this;
150     }
151 
152     public JSONObject put(String key, double value) {
153         try {
154             _jsonObj.put(key, value);
155         }
156         catch (Exception e) {
157             if (_log.isWarnEnabled()) {
158                 _log.warn(e, e);
159             }
160         }
161 
162         return this;
163     }
164 
165     public JSONObject put(String key, int value) {
166         try {
167             _jsonObj.put(key, value);
168         }
169         catch (Exception e) {
170             if (_log.isWarnEnabled()) {
171                 _log.warn(e, e);
172             }
173         }
174 
175         return this;
176     }
177 
178     public JSONObject put(String key, long value) {
179         try {
180             _jsonObj.put(key, value);
181         }
182         catch (Exception e) {
183             if (_log.isWarnEnabled()) {
184                 _log.warn(e, e);
185             }
186         }
187 
188         return this;
189     }
190 
191     public JSONObject put(String key, Date value) {
192         try {
193             _jsonObj.put(key, value);
194         }
195         catch (Exception e) {
196             if (_log.isWarnEnabled()) {
197                 _log.warn(e, e);
198             }
199         }
200 
201         return this;
202     }
203 
204     public JSONObject put(String key, JSONArray value) {
205         try {
206             _jsonObj.put(key, ((JSONArrayImpl)value).getJSONArray());
207         }
208         catch (Exception e) {
209             if (_log.isWarnEnabled()) {
210                 _log.warn(e, e);
211             }
212         }
213 
214         return this;
215     }
216 
217     public JSONObject put(String key, JSONObject value) {
218         try {
219             _jsonObj.put(key, ((JSONObjectImpl)value).getJSONObject());
220         }
221         catch (Exception e) {
222             if (_log.isWarnEnabled()) {
223                 _log.warn(e, e);
224             }
225         }
226 
227         return this;
228     }
229 
230     public JSONObject put(String key, String value) {
231         try {
232             _jsonObj.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     public Object remove(String key) {
244         return _jsonObj.remove(key);
245     }
246 
247     public String toString() {
248         return _jsonObj.toString();
249     }
250 
251     public String toString(int indentFactor) throws JSONException {
252         try {
253             return _jsonObj.toString(indentFactor);
254         }
255         catch (Exception e) {
256             throw new JSONException(e);
257         }
258     }
259 
260     public Writer write(Writer writer) throws JSONException {
261         try {
262             return _jsonObj.write(writer);
263         }
264         catch (Exception e) {
265             throw new JSONException(e);
266         }
267     }
268 
269     private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
270 
271     private org.json.JSONObject _jsonObj;
272 
273 }