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