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