001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * JSON.simple 010 * ----------- 011 * The code in this file originates from the JSON.simple project by 012 * FangYidong<fangyidong@yahoo.com.cn>: 013 * 014 * https://code.google.com/p/json-simple/ 015 * 016 * which is licensed under the Apache Software License version 2.0. 017 * 018 * It has been modified locally and repackaged under 019 * org.jfree.data.json.impl.* to avoid conflicts with any other version that 020 * may be present on the classpath. 021 * 022 */ 023 024package org.jfree.data.json.impl; 025 026import java.io.IOException; 027import java.io.Writer; 028import java.util.ArrayList; 029import java.util.Iterator; 030import java.util.List; 031 032/** 033 * A JSON array. 034 * <br><br> 035 * This class is for internal use by JFreeChart, it is not 036 * part of the supported API and you should not call it directly. If you need 037 * JSON support in your project you should include JSON.simple 038 * (https://code.google.com/p/json-simple/) or some other JSON library directly 039 * in your project. 040 */ 041public class JSONArray extends ArrayList implements List, JSONAware, 042 JSONStreamAware { 043 044 private static final long serialVersionUID = 3957988303675231981L; 045 046 /** 047 * Encode a list into JSON text and write it to out. 048 * If this list is also a {@link JSONStreamAware} or a {@link JSONAware}, 049 * {@code JSONStreamAware} and {@code JSONAware} specific 050 * behaviours will be ignored at this top level. 051 * 052 * @see org.jfree.data.json.impl.JSONValue#writeJSONString(Object, Writer) 053 * 054 * @param list the list ({@code null} permitted). 055 * @param out the output writer ({@code null} not permitted). 056 * 057 * @throws IOException if there is an I/O problem. 058 */ 059 public static void writeJSONString(List list, Writer out) 060 throws IOException { 061 if (list == null) { 062 out.write("null"); 063 return; 064 } 065 066 boolean first = true; 067 Iterator iter = list.iterator(); 068 out.write('['); 069 while (iter.hasNext()) { 070 if (first) { 071 first = false; 072 } 073 else { 074 out.write(','); 075 } 076 077 Object value = iter.next(); 078 if (value == null) { 079 out.write("null"); 080 continue; 081 } 082 JSONValue.writeJSONString(value, out); 083 } 084 out.write(']'); 085 } 086 087 /** 088 * Writes this array to the specified output writer. 089 * 090 * @param out the output writer ({@code null} not permitted). 091 * 092 * @throws IOException if there is an I/O problem. 093 */ 094 @Override 095 public void writeJSONString(Writer out) throws IOException { 096 writeJSONString(this, out); 097 } 098 099 /** 100 * Convert a list to JSON text. The result is a JSON array. 101 * If this list is also a {@link JSONAware}, {@link JSONAware} specific 102 * behaviours will be omitted at this top level. 103 * 104 * @see org.jfree.data.json.impl.JSONValue#toJSONString(Object) 105 * 106 * @param list the list ({@code null} permitted). 107 * 108 * @return JSON text, or "null" if list is null. 109 */ 110 public static String toJSONString(List list){ 111 if (list == null) { 112 return "null"; 113 } 114 115 boolean first = true; 116 StringBuilder sb = new StringBuilder(); 117 Iterator iter = list.iterator(); 118 sb.append('['); 119 while (iter.hasNext()) { 120 if (first) { 121 first = false; 122 } 123 else { 124 sb.append(','); 125 } 126 127 Object value = iter.next(); 128 if (value == null) { 129 sb.append("null"); 130 continue; 131 } 132 sb.append(JSONValue.toJSONString(value)); 133 } 134 sb.append(']'); 135 return sb.toString(); 136 } 137 138 /** 139 * Returns a JSON string representation of this list. 140 * 141 * @return A string. 142 */ 143 @Override 144 public String toJSONString(){ 145 return toJSONString(this); 146 } 147 148 /** 149 * Returns a string representation of this list. 150 * 151 * @return A string. 152 */ 153 @Override 154 public String toString() { 155 return toJSONString(); 156 } 157 158}