001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------
028 * JSONUtils.java
029 * --------------
030 * (C) Copyright 2014-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.json;
038
039import org.jfree.chart.internal.Args;
040import org.jfree.data.KeyedValues;
041import org.jfree.data.KeyedValues2D;
042import org.jfree.data.category.CategoryDataset;
043import org.jfree.data.general.PieDataset;
044import org.jfree.data.json.impl.JSONValue;
045
046import java.io.IOException;
047import java.io.StringWriter;
048import java.io.Writer;
049import java.util.List;
050
051/**
052 * A utility class that can read and write data in specific JSON formats.
053 * 
054 * @since 1.0.20
055 */
056public class JSONUtils {
057
058    /**
059     * Returns a string containing the data in JSON format.  The format is
060     * an array of arrays, where each sub-array represents one data value.
061     * The sub-array should contain two items, first the item key as a string
062     * and second the item value as a number.  For example:
063     * {@code [["Key A", 1.0], ["Key B", 2.0]]}
064     * <br><br>
065     * Note that this method can be used with instances of {@link PieDataset}.
066     * 
067     * @param data  the data ({@code null} not permitted).
068     * 
069     * @return A string in JSON format. 
070     */
071    public static String writeKeyedValues(KeyedValues data) {
072        Args.nullNotPermitted(data, "data");
073        StringWriter sw = new StringWriter();
074        try {
075            writeKeyedValues(data, sw);
076        } catch (IOException ex) {
077            throw new RuntimeException(ex);
078        }
079        return sw.toString();
080    }
081
082    /**
083     * Writes the data in JSON format to the supplied writer.
084     * <br><br>
085     * Note that this method can be used with instances of {@link PieDataset}.
086     * 
087     * @param data  the data ({@code null} not permitted).
088     * @param writer  the writer ({@code null} not permitted).
089     * 
090     * @throws IOException if there is an I/O problem.
091     */
092    public static void writeKeyedValues(KeyedValues data, Writer writer) 
093            throws IOException {
094        Args.nullNotPermitted(data, "data");
095        Args.nullNotPermitted(writer, "writer");
096        writer.write("[");
097        boolean first = true;
098        for (Object o : data.getKeys()) {
099            Comparable key = (Comparable) o;
100            if (!first) {
101                writer.write(", ");
102            } else {
103                first = false;
104            }
105            writer.write("[");
106            writer.write(JSONValue.toJSONString(key.toString()));
107            writer.write(", ");
108            writer.write(JSONValue.toJSONString(data.getValue(key)));
109            writer.write("]");
110        }
111        writer.write("]");
112    }
113    
114    /**
115     * Returns a string containing the data in JSON format.  The format is...
116     * <br><br>
117     * Note that this method can be used with instances of 
118     * {@link CategoryDataset}.
119     * 
120     * @param data  the data ({@code null} not permitted).
121     * 
122     * @return A string in JSON format. 
123     */
124    public static String writeKeyedValues2D(KeyedValues2D data) {
125        Args.nullNotPermitted(data, "data");
126        StringWriter sw = new StringWriter();
127        try {
128            writeKeyedValues2D(data, sw);
129        } catch (IOException ex) {
130            throw new RuntimeException(ex);
131        }
132        return sw.toString();
133    }
134
135    /**
136     * Writes the data in JSON format to the supplied writer.
137     * <br><br>
138     * Note that this method can be used with instances of 
139     * {@link CategoryDataset}.
140     * 
141     * @param data  the data ({@code null} not permitted).
142     * @param writer  the writer ({@code null} not permitted).
143     * 
144     * @throws IOException if there is an I/O problem.
145     */
146    public static void writeKeyedValues2D(KeyedValues2D data, Writer writer) 
147            throws IOException {
148        Args.nullNotPermitted(data, "data");
149        Args.nullNotPermitted(writer, "writer");
150        List<Comparable<?>> columnKeys = data.getColumnKeys();
151        List<Comparable<?>> rowKeys = data.getRowKeys();
152        writer.write("{");
153        if (!columnKeys.isEmpty()) {
154            writer.write("\"columnKeys\": [");
155            boolean first = true;
156            for (Comparable<?> columnKey : columnKeys) {
157                if (!first) {
158                    writer.write(", ");
159                } else {
160                    first = false;
161                }
162                writer.write(JSONValue.toJSONString(columnKey.toString()));
163            }
164            writer.write("]");
165        }
166        if (!rowKeys.isEmpty()) {
167            writer.write(", \"rows\": [");
168            boolean firstRow = true;
169            for (Comparable<?> rowKey : rowKeys) {   
170                if (!firstRow) {
171                    writer.write(", [");
172                } else {
173                    writer.write("[");
174                    firstRow = false;
175                }
176                // write the row data 
177                writer.write(JSONValue.toJSONString(rowKey.toString()));
178                writer.write(", [");
179                boolean first = true;
180                for (Comparable<?> columnKey : columnKeys) {
181                    if (!first) {
182                        writer.write(", ");
183                    } else {
184                        first = false;
185                    }
186                    writer.write(JSONValue.toJSONString(data.getValue(rowKey, 
187                            columnKey)));
188                }
189                writer.write("]]");
190            }
191            writer.write("]");
192        }
193        writer.write("}");    
194    }
195    
196}