JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / org / json / JSONStringer.java
1 package org.json;
2
3 /*
4 Copyright (c) 2006 JSON.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 The Software shall be used for Good, not Evil.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27 import java.io.StringWriter;
28
29 /**
30  * JSONStringer provides a quick and convenient way of producing JSON text. The
31  * texts produced strictly conform to JSON syntax rules. No whitespace is added,
32  * so the results are ready for transmission or storage. Each instance of
33  * JSONStringer can produce one JSON text.
34  * <p>
35  * A JSONStringer instance provides a <code>value</code> method for appending
36  * values to the text, and a <code>key</code> method for adding keys before
37  * values in objects. There are <code>array</code> and <code>endArray</code>
38  * methods that make and bound array values, and <code>object</code> and
39  * <code>endObject</code> methods which make and bound object values. All of
40  * these methods return the JSONWriter instance, permitting cascade style. For
41  * example,
42  * 
43  * <pre>
44  * myString = new JSONStringer().object().key("JSON").value("Hello, World!")
45  *         .endObject().toString();
46  * </pre>
47  * 
48  * which produces the string
49  * 
50  * <pre>
51  * {"JSON":"Hello, World!"}
52  * </pre>
53  * <p>
54  * The first method called must be <code>array</code> or <code>object</code>.
55  * There are no methods for adding commas or colons. JSONStringer adds them for
56  * you. Objects and arrays can be nested up to 20 levels deep.
57  * <p>
58  * This can sometimes be easier than using a JSONObject to build a string.
59  * 
60  * @author JSON.org
61  * @version 2015-12-09
62  */
63 public class JSONStringer extends JSONWriter
64 {
65   /**
66    * Make a fresh JSONStringer. It can be used to build one JSON text.
67    */
68   public JSONStringer()
69   {
70     super(new StringWriter());
71   }
72
73   /**
74    * Return the JSON text. This method is used to obtain the product of the
75    * JSONStringer instance. It will return <code>null</code> if there was a
76    * problem in the construction of the JSON text (such as the calls to
77    * <code>array</code> were not properly balanced with calls to
78    * <code>endArray</code>).
79    * 
80    * @return The JSON text.
81    */
82   @Override
83   public String toString()
84   {
85     return this.mode == 'd' ? this.writer.toString() : null;
86   }
87 }