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 org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
40 public class JSONArrayImpl implements JSONArray {
41
42 public JSONArrayImpl() {
43 _jsonArray = new org.json.JSONArray();
44 }
45
46 public JSONArrayImpl(String json) throws JSONException {
47 try {
48 _jsonArray = new org.json.JSONArray(json);
49 }
50 catch (Exception e) {
51 throw new JSONException(e);
52 }
53 }
54
55 public JSONArrayImpl(org.json.JSONArray jsonArray) {
56 _jsonArray = jsonArray;
57 }
58
59 public boolean getBoolean(int index) {
60 return _jsonArray.optBoolean(index);
61 }
62
63 public double getDouble(int index) {
64 return _jsonArray.optDouble(index);
65 }
66
67 public int getInt(int index) {
68 return _jsonArray.optInt(index);
69 }
70
71 public org.json.JSONArray getJSONArray() {
72 return _jsonArray;
73 }
74
75 public JSONArray getJSONArray(int index) {
76 org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
77
78 if (jsonArray == null) {
79 return null;
80 }
81
82 return new JSONArrayImpl(jsonArray);
83 }
84
85 public JSONObject getJSONObject(int index) {
86 org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
87
88 if (jsonObj == null) {
89 return null;
90 }
91
92 return new JSONObjectImpl(jsonObj);
93 }
94
95 public long getLong(int index) {
96 return _jsonArray.optLong(index);
97 }
98
99 public String getString(int index) {
100 return _jsonArray.optString(index);
101 }
102
103 public boolean isNull(int index) {
104 return _jsonArray.isNull(index);
105 }
106
107 public String join(String separator) throws JSONException {
108 try {
109 return _jsonArray.join(separator);
110 }
111 catch (Exception e) {
112 throw new JSONException(e);
113 }
114 }
115
116 public int length() {
117 return _jsonArray.length();
118 }
119
120 public JSONArray put(boolean value) {
121 _jsonArray.put(value);
122
123 return this;
124 }
125
126 public JSONArray put(double value) {
127 try {
128 _jsonArray.put(value);
129 }
130 catch (Exception e) {
131 if (_log.isWarnEnabled()) {
132 _log.warn(e, e);
133 }
134 }
135
136 return this;
137 }
138
139 public JSONArray put(int value) {
140 _jsonArray.put(value);
141
142 return this;
143 }
144
145 public JSONArray put(long value) {
146 _jsonArray.put(value);
147
148 return this;
149 }
150
151 public JSONArray put(JSONArray value) {
152 _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
153
154 return this;
155 }
156
157 public JSONArray put(JSONObject value) {
158 _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
159
160 return this;
161 }
162
163 public JSONArray put(String value) {
164 _jsonArray.put(value);
165
166 return this;
167 }
168
169 public String toString() {
170 return _jsonArray.toString();
171 }
172
173 public String toString(int indentFactor) throws JSONException {
174 try {
175 return _jsonArray.toString(indentFactor);
176 }
177 catch (Exception e) {
178 throw new JSONException(e);
179 }
180 }
181
182 public Writer write(Writer writer) throws JSONException {
183 try {
184 return _jsonArray.write(writer);
185 }
186 catch (Exception e) {
187 throw new JSONException(e);
188 }
189 }
190
191 private static Log _log = LogFactory.getLog(JSONArrayImpl.class);
192
193 private org.json.JSONArray _jsonArray;
194
195 }