001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class JSONArrayImpl implements JSONArray {
033    
034            public JSONArrayImpl() {
035                    _jsonArray = new org.json.JSONArray();
036            }
037    
038            public JSONArrayImpl(org.json.JSONArray jsonArray) {
039                    _jsonArray = jsonArray;
040            }
041    
042            public JSONArrayImpl(String json) throws JSONException {
043                    try {
044                            if (Validator.isNull(json)) {
045                                    json = _NULL_JSON;
046                            }
047    
048                            _jsonArray = new org.json.JSONArray(json);
049                    }
050                    catch (Exception e) {
051                            throw new JSONException(e);
052                    }
053            }
054    
055            @Override
056            public boolean getBoolean(int index) {
057                    return _jsonArray.optBoolean(index);
058            }
059    
060            @Override
061            public double getDouble(int index) {
062                    return _jsonArray.optDouble(index);
063            }
064    
065            @Override
066            public int getInt(int index) {
067                    return _jsonArray.optInt(index);
068            }
069    
070            public org.json.JSONArray getJSONArray() {
071                    return _jsonArray;
072            }
073    
074            @Override
075            public JSONArray getJSONArray(int index) {
076                    org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
077    
078                    if (jsonArray == null) {
079                            return null;
080                    }
081    
082                    return new JSONArrayImpl(jsonArray);
083            }
084    
085            @Override
086            public JSONObject getJSONObject(int index) {
087                    org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
088    
089                    if (jsonObj == null) {
090                            return null;
091                    }
092    
093                    return new JSONObjectImpl(jsonObj);
094            }
095    
096            @Override
097            public long getLong(int index) {
098                    return _jsonArray.optLong(index);
099            }
100    
101            @Override
102            public String getString(int index) {
103                    return _jsonArray.optString(index);
104            }
105    
106            @Override
107            public boolean isNull(int index) {
108                    return _jsonArray.isNull(index);
109            }
110    
111            @Override
112            public String join(String separator) throws JSONException {
113                    try {
114                            return _jsonArray.join(separator);
115                    }
116                    catch (Exception e) {
117                            throw new JSONException(e);
118                    }
119            }
120    
121            @Override
122            public int length() {
123                    return _jsonArray.length();
124            }
125    
126            @Override
127            public JSONArray put(boolean value) {
128                    _jsonArray.put(value);
129    
130                    return this;
131            }
132    
133            @Override
134            public JSONArray put(double value) {
135                    try {
136                            _jsonArray.put(value);
137                    }
138                    catch (Exception e) {
139                            if (_log.isWarnEnabled()) {
140                                    _log.warn(e, e);
141                            }
142                    }
143    
144                    return this;
145            }
146    
147            @Override
148            public JSONArray put(int value) {
149                    _jsonArray.put(value);
150    
151                    return this;
152            }
153    
154            @Override
155            public JSONArray put(JSONArray value) {
156                    _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
157    
158                    return this;
159            }
160    
161            @Override
162            public JSONArray put(JSONObject value) {
163                    _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
164    
165                    return this;
166            }
167    
168            @Override
169            public JSONArray put(long value) {
170                    _jsonArray.put(value);
171    
172                    return this;
173            }
174    
175            @Override
176            public JSONArray put(String value) {
177                    _jsonArray.put(value);
178    
179                    return this;
180            }
181    
182            @Override
183            public void readExternal(ObjectInput objectInput) throws IOException {
184                    try {
185                            _jsonArray = new org.json.JSONArray(objectInput.readUTF());
186                    }
187                    catch (Exception e) {
188                            throw new IOException(e);
189                    }
190            }
191    
192            @Override
193            public String toString() {
194                    return _jsonArray.toString();
195            }
196    
197            @Override
198            public String toString(int indentFactor) throws JSONException {
199                    try {
200                            return _jsonArray.toString(indentFactor);
201                    }
202                    catch (Exception e) {
203                            throw new JSONException(e);
204                    }
205            }
206    
207            @Override
208            public Writer write(Writer writer) throws JSONException {
209                    try {
210                            return _jsonArray.write(writer);
211                    }
212                    catch (Exception e) {
213                            throw new JSONException(e);
214                    }
215            }
216    
217            @Override
218            public void writeExternal(ObjectOutput objectOutput) throws IOException {
219                    objectOutput.writeUTF(toString());
220            }
221    
222            private static final String _NULL_JSON = "[]";
223    
224            private static Log _log = LogFactoryUtil.getLog(JSONArrayImpl.class);
225    
226            private org.json.JSONArray _jsonArray;
227    
228    }