001package org.json; 002 003/* 004Copyright (c) 2006 JSON.org 005 006Permission is hereby granted, free of charge, to any person obtaining a copy 007of this software and associated documentation files (the "Software"), to deal 008in the Software without restriction, including without limitation the rights 009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010copies of the Software, and to permit persons to whom the Software is 011furnished to do so, subject to the following conditions: 012 013The above copyright notice and this permission notice shall be included in all 014copies or substantial portions of the Software. 015 016The Software shall be used for Good, not Evil. 017 018THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 019IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 020FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 021AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 022LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 023OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 024SOFTWARE. 025*/ 026 027import java.io.StringWriter; 028 029/** 030 * JSONStringer provides a quick and convenient way of producing JSON text. 031 * The texts produced strictly conform to JSON syntax rules. No whitespace is 032 * added, so the results are ready for transmission or storage. Each instance of 033 * JSONStringer can produce one JSON text. 034 * <p> 035 * A JSONStringer instance provides a <code>value</code> method for appending 036 * values to the 037 * text, and a <code>key</code> 038 * method for adding keys before values in objects. There are <code>array</code> 039 * and <code>endArray</code> methods that make and bound array values, and 040 * <code>object</code> and <code>endObject</code> methods which make and bound 041 * object values. All of these methods return the JSONWriter instance, 042 * permitting cascade style. For example, <pre> 043 * myString = new JSONStringer() 044 * .object() 045 * .key("JSON") 046 * .value("Hello, World!") 047 * .endObject() 048 * .toString();</pre> which produces the string <pre> 049 * {"JSON":"Hello, World!"}</pre> 050 * <p> 051 * The first method called must be <code>array</code> or <code>object</code>. 052 * There are no methods for adding commas or colons. JSONStringer adds them for 053 * you. Objects and arrays can be nested up to 20 levels deep. 054 * <p> 055 * This can sometimes be easier than using a JSONObject to build a string. 056 * @author JSON.org 057 * @version 2015-12-09 058 */ 059public class JSONStringer extends JSONWriter { 060 /** 061 * Make a fresh JSONStringer. It can be used to build one JSON text. 062 */ 063 public JSONStringer() { 064 super(new StringWriter()); 065 } 066 067 /** 068 * Return the JSON text. This method is used to obtain the product of the 069 * JSONStringer instance. It will return <code>null</code> if there was a 070 * problem in the construction of the JSON text (such as the calls to 071 * <code>array</code> were not properly balanced with calls to 072 * <code>endArray</code>). 073 * @return The JSON text. 074 */ 075 public String toString() { 076 return this.mode == 'd' ? this.writer.toString() : null; 077 } 078}