PlotSquared/src/main/java/com/intellectualcrafters/json/JSONStringer.java

56 lines
2.0 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
package com.intellectualcrafters.json;
import java.io.StringWriter;
/**
2014-12-18 03:15:11 +01:00
* JSONStringer provides a quick and convenient way of producing JSON text. The texts produced strictly conform to JSON
* syntax rules. No whitespace is added, so the results are ready for transmission or storage. Each instance of
* JSONStringer can produce one JSON text.
2015-03-13 08:05:06 +01:00
*
2014-12-18 03:15:11 +01:00
* A JSONStringer instance provides a <code>value</code> method for appending values to the text, and a <code>key</code>
* method for adding keys before values in objects. There are <code>array</code> and <code>endArray</code> methods that
* make and bound array values, and <code>object</code> and <code>endObject</code> methods which make and bound object
* values. All of these methods return the JSONWriter instance, permitting cascade style. For example,
2015-03-13 08:05:06 +01:00
*
*
2014-11-05 04:42:08 +01:00
* <pre>
2014-12-18 03:15:11 +01:00
* myString = new JSONStringer().object().key(&quot;JSON&quot;).value(&quot;Hello,
* World!&quot;).endObject().toString();
2014-11-05 04:42:08 +01:00
* </pre>
2015-03-13 08:05:06 +01:00
*
2014-11-05 04:42:08 +01:00
* which produces the string
2015-03-13 08:05:06 +01:00
*
*
2014-11-05 04:42:08 +01:00
* <pre>
* {"JSON":"Hello, World!"}
* </pre>
2015-03-13 08:05:06 +01:00
*
2014-12-18 03:15:11 +01:00
* The first method called must be <code>array</code> or <code>object</code>. There are no methods for adding commas or
* colons. JSONStringer adds them for you. Objects and arrays can be nested up to 20 levels deep.
2015-03-13 08:05:06 +01:00
*
* This can sometimes be easier than using a JSONObject to build a string.
2014-11-05 04:42:08 +01:00
*
* @author JSON.org
* @version 2008-09-18
*/
public class JSONStringer extends JSONWriter {
/**
* Make a fresh JSONStringer. It can be used to build one JSON text.
*/
public JSONStringer() {
super(new StringWriter());
}
2015-02-23 02:32:27 +01:00
/**
2014-12-18 03:15:11 +01:00
* Return the JSON text. This method is used to obtain the product of the JSONStringer instance. It will return
* <code>null</code> if there was a problem in the construction of the JSON text (such as the calls to
* <code>array</code> were not properly balanced with calls to <code>endArray</code>).
2014-11-05 04:42:08 +01:00
*
* @return The JSON text.
*/
2014-11-05 04:42:08 +01:00
@Override
public String toString() {
return this.mode == 'd' ? this.writer.toString() : null;
}
}