1
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
29 import java.io.Writer;
30
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.Map;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
44 public class JSONObjectImpl implements JSONObject {
45
46 public JSONObjectImpl() {
47 _jsonObj = new org.json.JSONObject();
48 }
49
50 public JSONObjectImpl(JSONObject jsonObj, String[] names)
51 throws JSONException {
52
53 try {
54 JSONObjectImpl jsonObjImpl = (JSONObjectImpl)jsonObj;
55
56 _jsonObj = new org.json.JSONObject(
57 jsonObjImpl.getJSONObject(), names);
58 }
59 catch (Exception e) {
60 throw new JSONException(e);
61 }
62 }
63
64 public JSONObjectImpl(Map<?, ?> map) {
65 _jsonObj = new org.json.JSONObject(map);
66 }
67
68 public JSONObjectImpl(Object bean) {
69 _jsonObj = new org.json.JSONObject(bean);
70 }
71
72 public JSONObjectImpl(Object obj, String[] names) {
73 _jsonObj = new org.json.JSONObject(obj, names);
74 }
75
76 public JSONObjectImpl(String json) throws JSONException {
77 try {
78 _jsonObj = new org.json.JSONObject(json);
79 }
80 catch (Exception e) {
81 throw new JSONException(e);
82 }
83 }
84
85 public JSONObjectImpl(org.json.JSONObject jsonObj) {
86 _jsonObj = jsonObj;
87 }
88
89 public boolean getBoolean(String key) {
90 return _jsonObj.optBoolean(key);
91 }
92
93 public double getDouble(String key) {
94 return _jsonObj.optDouble(key);
95 }
96
97 public int getInt(String key) {
98 return _jsonObj.optInt(key);
99 }
100
101 public JSONArray getJSONArray(String key) {
102 org.json.JSONArray jsonArray = _jsonObj.optJSONArray(key);
103
104 if (jsonArray == null) {
105 return null;
106 }
107
108 return new JSONArrayImpl(jsonArray);
109 }
110
111 public org.json.JSONObject getJSONObject() {
112 return _jsonObj;
113 }
114
115 public JSONObject getJSONObject(String key) {
116 org.json.JSONObject jsonObj = _jsonObj.optJSONObject(key);
117
118 if (jsonObj == null) {
119 return null;
120 }
121
122 return new JSONObjectImpl(jsonObj);
123 }
124
125 public long getLong(String key) {
126 return _jsonObj.optLong(key);
127 }
128
129 public String getString(String key) {
130 return _jsonObj.optString(key);
131 }
132
133 public boolean has(String key) {
134 return _jsonObj.has(key);
135 }
136
137 public boolean isNull(String key) {
138 return _jsonObj.isNull(key);
139 }
140
141 public Iterator<String> keys() {
142 return _jsonObj.keys();
143 }
144
145 public int length() {
146 return _jsonObj.length();
147 }
148
149 public JSONObject put(String key, boolean value) {
150 try {
151 _jsonObj.put(key, value);
152 }
153 catch (Exception e) {
154 if (_log.isWarnEnabled()) {
155 _log.warn(e, e);
156 }
157 }
158
159 return this;
160 }
161
162 public JSONObject put(String key, double value) {
163 try {
164 _jsonObj.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, int value) {
176 try {
177 _jsonObj.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, long value) {
189 try {
190 _jsonObj.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, Date value) {
202 try {
203 _jsonObj.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 _jsonObj.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 _jsonObj.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, String value) {
241 try {
242 _jsonObj.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 Object remove(String key) {
254 return _jsonObj.remove(key);
255 }
256
257 public String toString() {
258 return _jsonObj.toString();
259 }
260
261 public String toString(int indentFactor) throws JSONException {
262 try {
263 return _jsonObj.toString(indentFactor);
264 }
265 catch (Exception e) {
266 throw new JSONException(e);
267 }
268 }
269
270 public Writer write(Writer writer) throws JSONException {
271 try {
272 return _jsonObj.write(writer);
273 }
274 catch (Exception e) {
275 throw new JSONException(e);
276 }
277 }
278
279 private static Log _log = LogFactory.getLog(JSONObjectImpl.class);
280
281 private org.json.JSONObject _jsonObj;
282
283 }