001/* Copyright 2006 FangYidong
002
003   Licensed under the Apache License, Version 2.0 (the "License");
004   you may not use this file except in compliance with the License.
005   You may obtain a copy of the License at
006
007       http://www.apache.org/licenses/LICENSE-2.0
008
009   Unless required by applicable law or agreed to in writing, software
010   distributed under the License is distributed on an "AS IS" BASIS,
011   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012   See the License for the specific language governing permissions and
013   limitations under the License. */
014package org.json.simple;
015
016import java.io.IOException;
017import java.io.StringWriter;
018import java.io.Writer;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Iterator;
022
023/**
024 * A JSON array. JSONObject supports java.util.List interface.
025 * 
026 * @author FangYidong<fangyidong@yahoo.com.cn>
027 * @deprecated since 2.0.0, replaced by {@link org.json.simple.JsonArray}
028 */
029@Deprecated
030public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
031        private static final long serialVersionUID = 3957988303675231981L;
032        
033        /**
034         * Constructs an empty JSONArray.
035         */
036        public JSONArray(){
037                super();
038        }
039        
040        /**
041         * Constructs a JSONArray containing the elements of the specified
042         * collection, in the order they are returned by the collection's iterator.
043         * 
044         * @param c the collection whose elements are to be placed into this JSONArray
045         */
046        public JSONArray(Collection c){
047                super(c);
048        }
049        
050    /**
051     * Encode a list into JSON text and write it to out. 
052     * If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
053     * 
054     * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
055     * 
056     * @param collection description omitted.
057     * @param out description omitted.
058     * @throws IOException description omitted.
059     */
060        public static void writeJSONString(Collection collection, Writer out) throws IOException{
061                if(collection == null){
062                        out.write("null");
063                        return;
064                }
065                
066                boolean first = true;
067                Iterator iter=collection.iterator();
068                
069        out.write('[');
070                while(iter.hasNext()){
071            if(first)
072                first = false;
073            else
074                out.write(',');
075            
076                        Object value=iter.next();
077                        if(value == null){
078                                out.write("null");
079                                continue;
080                        }
081                        
082                        JSONValue.writeJSONString(value, out);
083                }
084                out.write(']');
085        }
086        
087        public void writeJSONString(Writer out) throws IOException{
088                writeJSONString(this, out);
089        }
090        
091        /**
092         * Convert a list to JSON text. The result is a JSON array. 
093         * If this list is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
094         * 
095         * @see org.json.simple.JSONValue#toJSONString(Object)
096         * 
097         * @param collection description omitted.
098         * @return JSON text, or "null" if list is null.
099         */
100        public static String toJSONString(Collection collection){
101                final StringWriter writer = new StringWriter();
102                
103                try {
104                        writeJSONString(collection, writer);
105                        return writer.toString();
106                } catch(IOException e){
107                        // This should never happen for a StringWriter
108                        throw new RuntimeException(e);
109                }
110        }
111
112        /**
113         * description omitted.
114         *
115         * @param array description omitted.
116         * @param out description omitted.
117         * @throws IOException description omitted.
118         */
119        public static void writeJSONString(byte[] array, Writer out) throws IOException{
120                if(array == null){
121                        out.write("null");
122                } else if(array.length == 0) {
123                        out.write("[]");
124                } else {
125                        out.write("[");
126                        out.write(String.valueOf(array[0]));
127                        
128                        for(int i = 1; i < array.length; i++){
129                                out.write(",");
130                                out.write(String.valueOf(array[i]));
131                        }
132                        
133                        out.write("]");
134                }
135        }
136        
137        /**
138         * description omitted.
139         *
140         * @param array description omitted.
141         * @return description omitted.
142         */
143        public static String toJSONString(byte[] array){
144                final StringWriter writer = new StringWriter();
145                
146                try {
147                        writeJSONString(array, writer);
148                        return writer.toString();
149                } catch(IOException e){
150                        // This should never happen for a StringWriter
151                        throw new RuntimeException(e);
152                }
153        }
154        
155        /**
156         * description omitted.
157         *
158         * @param array description omitted.
159         * @param out description omitted.
160         * @throws IOException description omitted.
161         */
162        public static void writeJSONString(short[] array, Writer out) throws IOException{
163                if(array == null){
164                        out.write("null");
165                } else if(array.length == 0) {
166                        out.write("[]");
167                } else {
168                        out.write("[");
169                        out.write(String.valueOf(array[0]));
170                        
171                        for(int i = 1; i < array.length; i++){
172                                out.write(",");
173                                out.write(String.valueOf(array[i]));
174                        }
175                        
176                        out.write("]");
177                }
178        }
179        
180        /**
181         * description omitted.
182         *
183         * @param array description omitted.
184         * @return description omitted.
185         */
186        public static String toJSONString(short[] array){
187                final StringWriter writer = new StringWriter();
188                
189                try {
190                        writeJSONString(array, writer);
191                        return writer.toString();
192                } catch(IOException e){
193                        // This should never happen for a StringWriter
194                        throw new RuntimeException(e);
195                }
196        }
197        
198        /**
199         * description omitted.
200         *
201         * @param array description omitted.
202         * @param out description omitted.
203         * @throws IOException description omitted.
204         */
205        public static void writeJSONString(int[] array, Writer out) throws IOException{
206                if(array == null){
207                        out.write("null");
208                } else if(array.length == 0) {
209                        out.write("[]");
210                } else {
211                        out.write("[");
212                        out.write(String.valueOf(array[0]));
213                        
214                        for(int i = 1; i < array.length; i++){
215                                out.write(",");
216                                out.write(String.valueOf(array[i]));
217                        }
218                        
219                        out.write("]");
220                }
221        }
222        
223        /**
224         * description omitted.
225         *
226         * @param array description omitted.
227         * @return description omitted.
228         */
229        public static String toJSONString(int[] array){
230                final StringWriter writer = new StringWriter();
231                
232                try {
233                        writeJSONString(array, writer);
234                        return writer.toString();
235                } catch(IOException e){
236                        // This should never happen for a StringWriter
237                        throw new RuntimeException(e);
238                }
239        }
240        
241        /**
242         * description omitted.
243         *
244         * @param array description omitted.
245         * @param out description omitted.
246         * @throws IOException description omitted.
247         */
248        public static void writeJSONString(long[] array, Writer out) throws IOException{
249                if(array == null){
250                        out.write("null");
251                } else if(array.length == 0) {
252                        out.write("[]");
253                } else {
254                        out.write("[");
255                        out.write(String.valueOf(array[0]));
256                        
257                        for(int i = 1; i < array.length; i++){
258                                out.write(",");
259                                out.write(String.valueOf(array[i]));
260                        }
261                        
262                        out.write("]");
263                }
264        }
265        
266        /**
267         * description omitted.
268         *
269         * @param array description omitted.
270         * @return description omitted.
271         */
272        public static String toJSONString(long[] array){
273                final StringWriter writer = new StringWriter();
274                
275                try {
276                        writeJSONString(array, writer);
277                        return writer.toString();
278                } catch(IOException e){
279                        // This should never happen for a StringWriter
280                        throw new RuntimeException(e);
281                }
282        }
283        
284        /**
285         * description omitted.
286         *
287         * @param array description omitted.
288         * @param out description omitted.
289         * @throws IOException description omitted.
290         */
291        public static void writeJSONString(float[] array, Writer out) throws IOException{
292                if(array == null){
293                        out.write("null");
294                } else if(array.length == 0) {
295                        out.write("[]");
296                } else {
297                        out.write("[");
298                        out.write(String.valueOf(array[0]));
299                        
300                        for(int i = 1; i < array.length; i++){
301                                out.write(",");
302                                out.write(String.valueOf(array[i]));
303                        }
304                        
305                        out.write("]");
306                }
307        }
308        
309        /**
310         * description omitted.
311         *
312         * @param array description omitted.
313         * @return description omitted.
314         */
315        public static String toJSONString(float[] array){
316                final StringWriter writer = new StringWriter();
317                
318                try {
319                        writeJSONString(array, writer);
320                        return writer.toString();
321                } catch(IOException e){
322                        // This should never happen for a StringWriter
323                        throw new RuntimeException(e);
324                }
325        }
326        
327        /**
328         * description omitted.
329         *
330         * @param array description omitted.
331         * @param out description omitted.
332         * @throws IOException description omitted.
333         */
334        public static void writeJSONString(double[] array, Writer out) throws IOException{
335                if(array == null){
336                        out.write("null");
337                } else if(array.length == 0) {
338                        out.write("[]");
339                } else {
340                        out.write("[");
341                        out.write(String.valueOf(array[0]));
342                        
343                        for(int i = 1; i < array.length; i++){
344                                out.write(",");
345                                out.write(String.valueOf(array[i]));
346                        }
347                        
348                        out.write("]");
349                }
350        }
351        
352        /**
353         * description omitted.
354         *
355         * @param array description omitted.
356         * @return description omitted.
357         */
358        public static String toJSONString(double[] array){
359                final StringWriter writer = new StringWriter();
360                
361                try {
362                        writeJSONString(array, writer);
363                        return writer.toString();
364                } catch(IOException e){
365                        // This should never happen for a StringWriter
366                        throw new RuntimeException(e);
367                }
368        }
369        
370        /**
371         * description omitted.
372         *
373         * @param array description omitted.
374         * @param out description omitted.
375         * @throws IOException description omitted.
376         */
377        public static void writeJSONString(boolean[] array, Writer out) throws IOException{
378                if(array == null){
379                        out.write("null");
380                } else if(array.length == 0) {
381                        out.write("[]");
382                } else {
383                        out.write("[");
384                        out.write(String.valueOf(array[0]));
385                        
386                        for(int i = 1; i < array.length; i++){
387                                out.write(",");
388                                out.write(String.valueOf(array[i]));
389                        }
390                        
391                        out.write("]");
392                }
393        }
394        
395        /**
396         * description omitted.
397         *
398         * @param array description omitted.
399         * @return description omitted.
400         */
401        public static String toJSONString(boolean[] array){
402                final StringWriter writer = new StringWriter();
403                
404                try {
405                        writeJSONString(array, writer);
406                        return writer.toString();
407                } catch(IOException e){
408                        // This should never happen for a StringWriter
409                        throw new RuntimeException(e);
410                }
411        }
412        
413        /**
414         * description omitted.
415         *
416         * @param array description omitted.
417         * @param out description omitted.
418         * @throws IOException description omitted.
419         */
420        public static void writeJSONString(char[] array, Writer out) throws IOException{
421                if(array == null){
422                        out.write("null");
423                } else if(array.length == 0) {
424                        out.write("[]");
425                } else {
426                        out.write("[\"");
427                        out.write(String.valueOf(array[0]));
428                        
429                        for(int i = 1; i < array.length; i++){
430                                out.write("\",\"");
431                                out.write(String.valueOf(array[i]));
432                        }
433                        
434                        out.write("\"]");
435                }
436        }
437        
438        /**
439         * description omitted.
440         *
441         * @param array description omitted.
442         * @return description omitted.
443         */
444        public static String toJSONString(char[] array){
445                final StringWriter writer = new StringWriter();
446                
447                try {
448                        writeJSONString(array, writer);
449                        return writer.toString();
450                } catch(IOException e){
451                        // This should never happen for a StringWriter
452                        throw new RuntimeException(e);
453                }
454        }
455        
456        /**
457         * description omitted.
458         *
459         * @param array description omitted.
460         * @param out description omitted.
461         * @throws IOException description omitted.
462         */
463        public static void writeJSONString(Object[] array, Writer out) throws IOException{
464                if(array == null){
465                        out.write("null");
466                } else if(array.length == 0) {
467                        out.write("[]");
468                } else {
469                        out.write("[");
470                        JSONValue.writeJSONString(array[0], out);
471                        
472                        for(int i = 1; i < array.length; i++){
473                                out.write(",");
474                                JSONValue.writeJSONString(array[i], out);
475                        }
476                        
477                        out.write("]");
478                }
479        }
480        
481        /**
482         * description omitted.
483         *
484         * @param array description omitted.
485         * @return description omitted.
486         */
487        public static String toJSONString(Object[] array){
488                final StringWriter writer = new StringWriter();
489                
490                try {
491                        writeJSONString(array, writer);
492                        return writer.toString();
493                } catch(IOException e){
494                        // This should never happen for a StringWriter
495                        throw new RuntimeException(e);
496                }
497        }
498        
499        public String toJSONString(){
500                return toJSONString(this);
501        }
502
503        /**
504         * Returns a string representation of this array. This is equivalent to
505         * calling {@link JSONArray#toJSONString()}.
506         */
507        public String toString() {
508                return toJSONString();
509        }
510}