001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 *
027 * This file is based on:
028 * org.json.JSONStringer
029 * from the JSON in Java implementation.
030 *
031 * Copyright (c) 2002 JSON.org
032 *
033 * Permission is hereby granted, free of charge, to any person obtaining a copy
034 * of this software and associated documentation files (the "Software"), to deal
035 * in the Software without restriction, including without limitation the rights
036 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
037 * copies of the Software, and to permit persons to whom the Software is
038 * furnished to do so, subject to the following conditions:
039 *
040 * The above copyright notice and this permission notice shall be included in all
041 * copies or substantial portions of the Software.
042 *
043 * The Software shall be used for Good, not Evil.
044 *
045 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
046 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
047 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
048 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
049 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
050 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
051 * SOFTWARE.
052 */
053
054package org.opencms.json;
055
056import java.io.StringWriter;
057
058/**
059 * JSONStringer provides a quick and convenient way of producing JSON text.
060 * The texts produced strictly conform to JSON syntax rules. No whitespace is
061 * added, so the results are ready for transmission or storage. Each instance of
062 * JSONStringer can produce one JSON text.
063 * <p>
064 * A JSONStringer instance provides a <code>value</code> method for appending
065 * values to the
066 * text, and a <code>key</code>
067 * method for adding keys before values in objects. There are <code>array</code>
068 * and <code>endArray</code> methods that make and bound array values, and
069 * <code>object</code> and <code>endObject</code> methods which make and bound
070 * object values. All of these methods return the JSONWriter instance,
071 * permitting cascade style. For example, <pre>
072 * myString = new JSONStringer()
073 *     .object()
074 *         .key("JSON")
075 *         .value("Hello, World!")
076 *     .endObject()
077 *     .toString();</pre> which produces the string <pre>
078 * {"JSON":"Hello, World!"}</pre>
079 * <p>
080 * The first method called must be <code>array</code> or <code>object</code>.
081 * There are no methods for adding commas or colons. JSONStringer adds them for
082 * you. Objects and arrays can be nested up to 20 levels deep.
083 * <p>
084 * This can sometimes be easier than using a JSONObject to build a string.
085 */
086public class JSONStringer extends JSONWriter {
087
088    /**
089     * Make a fresh JSONStringer. It can be used to build one JSON text.
090     */
091    public JSONStringer() {
092
093        super(new StringWriter());
094    }
095
096    /**
097     * Return the JSON text. This method is used to obtain the product of the
098     * JSONStringer instance. It will return <code>null</code> if there was a
099     * problem in the construction of the JSON text (such as the calls to
100     * <code>array</code> were not properly balanced with calls to
101     * <code>endArray</code>).
102     * @return The JSON text.
103     */
104    @Override
105    public String toString() {
106
107        return m_mode == 'd' ? m_writer.toString() : null;
108    }
109}