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.Writer;
025
026
029 public class JSONArrayImpl implements JSONArray {
030
031 public JSONArrayImpl() {
032 _jsonArray = new org.json.JSONArray();
033 }
034
035 public JSONArrayImpl(org.json.JSONArray jsonArray) {
036 _jsonArray = jsonArray;
037 }
038
039 public JSONArrayImpl(String json) throws JSONException {
040 try {
041 if (Validator.isNull(json)) {
042 json = _NULL_JSON;
043 }
044
045 _jsonArray = new org.json.JSONArray(json);
046 }
047 catch (Exception e) {
048 throw new JSONException(e);
049 }
050 }
051
052 @Override
053 public boolean getBoolean(int index) {
054 return _jsonArray.optBoolean(index);
055 }
056
057 @Override
058 public double getDouble(int index) {
059 return _jsonArray.optDouble(index);
060 }
061
062 @Override
063 public int getInt(int index) {
064 return _jsonArray.optInt(index);
065 }
066
067 public org.json.JSONArray getJSONArray() {
068 return _jsonArray;
069 }
070
071 @Override
072 public JSONArray getJSONArray(int index) {
073 org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
074
075 if (jsonArray == null) {
076 return null;
077 }
078
079 return new JSONArrayImpl(jsonArray);
080 }
081
082 @Override
083 public JSONObject getJSONObject(int index) {
084 org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
085
086 if (jsonObj == null) {
087 return null;
088 }
089
090 return new JSONObjectImpl(jsonObj);
091 }
092
093 @Override
094 public long getLong(int index) {
095 return _jsonArray.optLong(index);
096 }
097
098 @Override
099 public String getString(int index) {
100 return _jsonArray.optString(index);
101 }
102
103 @Override
104 public boolean isNull(int index) {
105 return _jsonArray.isNull(index);
106 }
107
108 @Override
109 public String join(String separator) throws JSONException {
110 try {
111 return _jsonArray.join(separator);
112 }
113 catch (Exception e) {
114 throw new JSONException(e);
115 }
116 }
117
118 @Override
119 public int length() {
120 return _jsonArray.length();
121 }
122
123 @Override
124 public JSONArray put(boolean value) {
125 _jsonArray.put(value);
126
127 return this;
128 }
129
130 @Override
131 public JSONArray put(double value) {
132 try {
133 _jsonArray.put(value);
134 }
135 catch (Exception e) {
136 if (_log.isWarnEnabled()) {
137 _log.warn(e, e);
138 }
139 }
140
141 return this;
142 }
143
144 @Override
145 public JSONArray put(int value) {
146 _jsonArray.put(value);
147
148 return this;
149 }
150
151 @Override
152 public JSONArray put(JSONArray value) {
153 _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
154
155 return this;
156 }
157
158 @Override
159 public JSONArray put(JSONObject value) {
160 _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
161
162 return this;
163 }
164
165 @Override
166 public JSONArray put(long value) {
167 _jsonArray.put(value);
168
169 return this;
170 }
171
172 @Override
173 public JSONArray put(String value) {
174 _jsonArray.put(value);
175
176 return this;
177 }
178
179 @Override
180 public String toString() {
181 return _jsonArray.toString();
182 }
183
184 @Override
185 public String toString(int indentFactor) throws JSONException {
186 try {
187 return _jsonArray.toString(indentFactor);
188 }
189 catch (Exception e) {
190 throw new JSONException(e);
191 }
192 }
193
194 @Override
195 public Writer write(Writer writer) throws JSONException {
196 try {
197 return _jsonArray.write(writer);
198 }
199 catch (Exception e) {
200 throw new JSONException(e);
201 }
202 }
203
204 private static final String _NULL_JSON = "[]";
205
206 private static Log _log = LogFactoryUtil.getLog(JSONArrayImpl.class);
207
208 private org.json.JSONArray _jsonArray;
209
210 }