1
19
20 package com.liferay.portal.json;
21
22 import com.liferay.portal.kernel.json.JSONArray;
23 import com.liferay.portal.kernel.json.JSONException;
24 import com.liferay.portal.kernel.json.JSONObject;
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27
28 import java.io.Writer;
29
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.Map;
33
34
40 public class JSONObjectImpl implements JSONObject {
41
42 public JSONObjectImpl() {
43 _jsonObj = new org.json.JSONObject();
44 }
45
46 public JSONObjectImpl(JSONObject jsonObj, String[] names)
47 throws JSONException {
48
49 try {
50 JSONObjectImpl jsonObjImpl = (JSONObjectImpl)jsonObj;
51
52 _jsonObj = new org.json.JSONObject(
53 jsonObjImpl.getJSONObject(), names);
54 }
55 catch (Exception e) {
56 throw new JSONException(e);
57 }
58 }
59
60 public JSONObjectImpl(Map<?, ?> map) {
61 _jsonObj = new org.json.JSONObject(map);
62 }
63
64 public JSONObjectImpl(Object bean) {
65 _jsonObj = new org.json.JSONObject(bean);
66 }
67
68 public JSONObjectImpl(Object obj, String[] names) {
69 _jsonObj = new org.json.JSONObject(obj, names);
70 }
71
72 public JSONObjectImpl(String json) throws JSONException {
73 try {
74 _jsonObj = new org.json.JSONObject(json);
75 }
76 catch (Exception e) {
77 throw new JSONException(e);
78 }
79 }
80
81 public JSONObjectImpl(org.json.JSONObject jsonObj) {
82 _jsonObj = jsonObj;
83 }
84
85 public boolean getBoolean(String key) {
86 return _jsonObj.optBoolean(key);
87 }
88
89 public double getDouble(String key) {
90 return _jsonObj.optDouble(key);
91 }
92
93 public int getInt(String key) {
94 return _jsonObj.optInt(key);
95 }
96
97 public JSONArray getJSONArray(String key) {
98 org.json.JSONArray jsonArray = _jsonObj.optJSONArray(key);
99
100 if (jsonArray == null) {
101 return null;
102 }
103
104 return new JSONArrayImpl(jsonArray);
105 }
106
107 public org.json.JSONObject getJSONObject() {
108 return _jsonObj;
109 }
110
111 public JSONObject getJSONObject(String key) {
112 org.json.JSONObject jsonObj = _jsonObj.optJSONObject(key);
113
114 if (jsonObj == null) {
115 return null;
116 }
117
118 return new JSONObjectImpl(jsonObj);
119 }
120
121 public long getLong(String key) {
122 return _jsonObj.optLong(key);
123 }
124
125 public String getString(String key) {
126 return _jsonObj.optString(key);
127 }
128
129 public boolean has(String key) {
130 return _jsonObj.has(key);
131 }
132
133 public boolean isNull(String key) {
134 return _jsonObj.isNull(key);
135 }
136
137 public Iterator<String> keys() {
138 return _jsonObj.keys();
139 }
140
141 public int length() {
142 return _jsonObj.length();
143 }
144
145 public JSONObject put(String key, boolean value) {
146 try {
147 _jsonObj.put(key, value);
148 }
149 catch (Exception e) {
150 if (_log.isWarnEnabled()) {
151 _log.warn(e, e);
152 }
153 }
154
155 return this;
156 }
157
158 public JSONObject put(String key, double value) {
159 try {
160 _jsonObj.put(key, value);
161 }
162 catch (Exception e) {
163 if (_log.isWarnEnabled()) {
164 _log.warn(e, e);
165 }
166 }
167
168 return this;
169 }
170
171 public JSONObject put(String key, int value) {
172 try {
173 _jsonObj.put(key, value);
174 }
175 catch (Exception e) {
176 if (_log.isWarnEnabled()) {
177 _log.warn(e, e);
178 }
179 }
180
181 return this;
182 }
183
184 public JSONObject put(String key, long value) {
185 try {
186 _jsonObj.put(key, value);
187 }
188 catch (Exception e) {
189 if (_log.isWarnEnabled()) {
190 _log.warn(e, e);
191 }
192 }
193
194 return this;
195 }
196
197 public JSONObject put(String key, Date value) {
198 try {
199 _jsonObj.put(key, value);
200 }
201 catch (Exception e) {
202 if (_log.isWarnEnabled()) {
203 _log.warn(e, e);
204 }
205 }
206
207 return this;
208 }
209
210 public JSONObject put(String key, JSONArray value) {
211 try {
212 _jsonObj.put(key, ((JSONArrayImpl)value).getJSONArray());
213 }
214 catch (Exception e) {
215 if (_log.isWarnEnabled()) {
216 _log.warn(e, e);
217 }
218 }
219
220 return this;
221 }
222
223 public JSONObject put(String key, JSONObject value) {
224 try {
225 _jsonObj.put(key, ((JSONObjectImpl)value).getJSONObject());
226 }
227 catch (Exception e) {
228 if (_log.isWarnEnabled()) {
229 _log.warn(e, e);
230 }
231 }
232
233 return this;
234 }
235
236 public JSONObject put(String key, String value) {
237 try {
238 _jsonObj.put(key, value);
239 }
240 catch (Exception e) {
241 if (_log.isWarnEnabled()) {
242 _log.warn(e, e);
243 }
244 }
245
246 return this;
247 }
248
249 public Object remove(String key) {
250 return _jsonObj.remove(key);
251 }
252
253 public String toString() {
254 return _jsonObj.toString();
255 }
256
257 public String toString(int indentFactor) throws JSONException {
258 try {
259 return _jsonObj.toString(indentFactor);
260 }
261 catch (Exception e) {
262 throw new JSONException(e);
263 }
264 }
265
266 public Writer write(Writer writer) throws JSONException {
267 try {
268 return _jsonObj.write(writer);
269 }
270 catch (Exception e) {
271 throw new JSONException(e);
272 }
273 }
274
275 private static Log _log = LogFactoryUtil.getLog(JSONObjectImpl.class);
276
277 private org.json.JSONObject _jsonObj;
278
279 }