001
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
024 import java.io.Writer;
025
026 import java.util.Date;
027 import java.util.Iterator;
028 import java.util.Map;
029
030
033 public class JSONObjectImpl implements JSONObject {
034
035 public JSONObjectImpl() {
036 _jsonObject = new org.json.JSONObject();
037 }
038
039 public JSONObjectImpl(JSONObject jsonObject, String[] names)
040 throws JSONException {
041
042 try {
043 JSONObjectImpl jsonObjectImpl = (JSONObjectImpl)jsonObject;
044
045 _jsonObject = new org.json.JSONObject(
046 jsonObjectImpl.getJSONObject(), names);
047 }
048 catch (Exception e) {
049 throw new JSONException(e);
050 }
051 }
052
053 public JSONObjectImpl(Map<?, ?> map) {
054 _jsonObject = new org.json.JSONObject(map);
055 }
056
057 public JSONObjectImpl(Object bean) {
058 _jsonObject = new org.json.JSONObject(bean);
059 }
060
061 public JSONObjectImpl(Object obj, String[] names) {
062 _jsonObject = new org.json.JSONObject(obj, names);
063 }
064
065 public JSONObjectImpl(String json) throws JSONException {
066 try {
067 _jsonObject = new org.json.JSONObject(json);
068 }
069 catch (Exception e) {
070 throw new JSONException(e);
071 }
072 }
073
074 public JSONObjectImpl(org.json.JSONObject jsonObj) {
075 _jsonObject = jsonObj;
076 }
077
078 public boolean getBoolean(String key) {
079 return _jsonObject.optBoolean(key);
080 }
081
082 public boolean getBoolean(String key, boolean defaultValue) {
083 return _jsonObject.optBoolean(key, defaultValue);
084 }
085
086 public double getDouble(String key) {
087 return _jsonObject.optDouble(key);
088 }
089
090 public double getDouble(String key, double defaultValue) {
091 return _jsonObject.optDouble(key, defaultValue);
092 }
093
094 public int getInt(String key) {
095 return _jsonObject.optInt(key);
096 }
097
098 public int getInt(String key, int defaultValue) {
099 return _jsonObject.optInt(key, defaultValue);
100 }
101
102 public JSONArray getJSONArray(String key) {
103 org.json.JSONArray jsonArray = _jsonObject.optJSONArray(key);
104
105 if (jsonArray == null) {
106 return null;
107 }
108
109 return new JSONArrayImpl(jsonArray);
110 }
111
112 public org.json.JSONObject getJSONObject() {
113 return _jsonObject;
114 }
115
116 public JSONObject getJSONObject(String key) {
117 org.json.JSONObject jsonObj = _jsonObject.optJSONObject(key);
118
119 if (jsonObj == null) {
120 return null;
121 }
122
123 return new JSONObjectImpl(jsonObj);
124 }
125
126 public long getLong(String key) {
127 return _jsonObject.optLong(key);
128 }
129
130 public long getLong(String key, long defaultValue) {
131 return _jsonObject.optLong(key, defaultValue);
132 }
133
134 public String getString(String key) {
135 return _jsonObject.optString(key);
136 }
137
138 public String getString(String key, String defaultValue) {
139 return _jsonObject.optString(key, defaultValue);
140 }
141
142 public boolean has(String key) {
143 return _jsonObject.has(key);
144 }
145
146 public boolean isNull(String key) {
147 return _jsonObject.isNull(key);
148 }
149
150 public Iterator<String> keys() {
151 return _jsonObject.keys();
152 }
153
154 public int length() {
155 return _jsonObject.length();
156 }
157
158 public JSONArray names() {
159 return new JSONArrayImpl(_jsonObject.names());
160 }
161
162 public JSONObject put(String key, boolean value) {
163 try {
164 _jsonObject.put(key, value);
165 }
166 catch (Exception e) {
167 if (_log.isWarnEnabled()) {
168 _log.warn(e, e);
169 }
170 }
171
172 return this;
173 }
174
175 public JSONObject put(String key, Date value) {
176 try {
177 _jsonObject.put(key, value);
178 }
179 catch (Exception e) {
180 if (_log.isWarnEnabled()) {
181 _log.warn(e, e);
182 }
183 }
184
185 return this;
186 }
187
188 public JSONObject put(String key, double 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 public JSONObject put(String key, int value) {
202 try {
203 _jsonObject.put(key, value);
204 }
205 catch (Exception e) {
206 if (_log.isWarnEnabled()) {
207 _log.warn(e, e);
208 }
209 }
210
211 return this;
212 }
213
214 public JSONObject put(String key, JSONArray value) {
215 try {
216 _jsonObject.put(key, ((JSONArrayImpl)value).getJSONArray());
217 }
218 catch (Exception e) {
219 if (_log.isWarnEnabled()) {
220 _log.warn(e, e);
221 }
222 }
223
224 return this;
225 }
226
227 public JSONObject put(String key, JSONObject value) {
228 try {
229 _jsonObject.put(key, ((JSONObjectImpl)value).getJSONObject());
230 }
231 catch (Exception e) {
232 if (_log.isWarnEnabled()) {
233 _log.warn(e, e);
234 }
235 }
236
237 return this;
238 }
239
240 public JSONObject put(String key, long value) {
241 try {
242 _jsonObject.put(key, value);
243 }
244 catch (Exception e) {
245 if (_log.isWarnEnabled()) {
246 _log.warn(e, e);
247 }
248 }
249
250 return this;
251 }
252
253 public JSONObject put(String key, String value) {
254 try {
255 _jsonObject.put(key, value);
256 }
257 catch (Exception e) {
258 if (_log.isWarnEnabled()) {
259 _log.warn(e, e);
260 }
261 }
262
263 return this;
264 }
265
266 public JSONObject putException(Exception exception) {
267 try {
268 _jsonObject.put(
269 "exception",
270 exception.getClass() + StringPool.COLON +
271 exception.getMessage());
272 }
273 catch (Exception e) {
274 if (_log.isWarnEnabled()) {
275 _log.warn(e, e);
276 }
277 }
278
279 return this;
280 }
281
282 public Object remove(String key) {
283 return _jsonObject.remove(key);
284 }
285
286 @Override
287 public String toString() {
288 return _jsonObject.toString();
289 }
290
291 public String toString(int indentFactor) throws JSONException {
292 try {
293 return _jsonObject.toString(indentFactor);
294 }
295 catch (Exception e) {
296 throw new JSONException(e);
297 }
298 }
299
300 public Writer write(Writer writer) throws JSONException {
301 try {
302 return _jsonObject.write(writer);
303 }
304 catch (Exception e) {
305 throw new JSONException(e);
306 }
307 }
308
309 private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
310
311 private org.json.JSONObject _jsonObject;
312
313 }