Class JSONStringer
- java.lang.Object
-
- com.github.openjson.JSONStringer
-
public class JSONStringer extends java.lang.ObjectImplementsJSONObject.toString()andJSONArray.toString(). Most application developers should use those methods directly and disregard this API. For example:JSONObject object = ... String json = object.toString();
Stringers only encode well-formed JSON strings. In particular:
- The stringer must have exactly one top-level array or object.
- Lexical scopes must be balanced: every call to
array()must have a matching call toendArray()and every call toobject()must have a matching call toendObject(). - Arrays may not contain keys (property names).
- Objects must alternate keys (property names) and values.
- Values are inserted with either literal
valuecalls, or by nesting arrays or objects.
JSONException.
This class provides no facility for pretty-printing (ie. indenting) output. To encode indented output, use
JSONObject.toString(int)orJSONArray.toString(int).
Some implementations of the API support at most 20 levels of nesting. Attempts to create more than 20 levels of nesting may fail with a
JSONException.
Each stringer may be used to encode a single top level value. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classJSONStringer.ScopeLexical scoping elements within this stringer, necessary to insert the appropriate separator characters (ie.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringindentA string containing a full set of spaces for a single level of indentation, or null for no pretty printing.protected java.lang.StringBuilderoutThe output data, containing at most one top-level array or object.private java.util.List<JSONStringer.Scope>stackUnlike the original implementation, this stack isn't limited to 20 levels of nesting.
-
Constructor Summary
Constructors Constructor Description JSONStringer()JSONStringer(int indentSpaces)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONStringerarray()Begins encoding a new array.private voidbeforeKey()Inserts any necessary separators and whitespace before a name.private voidbeforeValue()Inserts any necessary separators and whitespace before a literal value, inline array, or inline object.(package private) JSONStringerclose(JSONStringer.Scope empty, JSONStringer.Scope nonempty, java.lang.String closeBracket)Closes the current scope by appending any necessary whitespace and the given bracket.protected JSONStringercreateKey(java.lang.String name)Creates String representation of the key (property name) to this stringer Override this method to provide your own representation of the name.JSONStringerendArray()Ends encoding the current array.JSONStringerendObject()Ends encoding the current object.JSONStringerentry(java.util.Map.Entry<java.lang.String,java.lang.Object> entry)Encodeskey/valuepair to this stringer.JSONStringerkey(java.lang.String name)Encodes the key (property name) to this stringer.private voidnewline()JSONStringerobject()Begins encoding a new object.(package private) JSONStringeropen(JSONStringer.Scope empty, java.lang.String openBracket)Enters a new scope by appending any necessary whitespace and the given bracket.private JSONStringer.Scopepeek()Returns the value on the top of the stack.private voidreplaceTop(JSONStringer.Scope topOfStack)Replace the value on the top of the stack with the given value.private voidstring(java.lang.String value)java.lang.StringtoString()Returns the encoded JSON string.JSONStringervalue(boolean value)Encodesvalueto this stringer.JSONStringervalue(double value)Encodesvalueto this stringer.JSONStringervalue(long value)Encodesvalueto this stringer.JSONStringervalue(java.lang.Object value)Encodesvalue.
-
-
-
Field Detail
-
out
protected final java.lang.StringBuilder out
The output data, containing at most one top-level array or object.
-
stack
private final java.util.List<JSONStringer.Scope> stack
Unlike the original implementation, this stack isn't limited to 20 levels of nesting.
-
indent
private final java.lang.String indent
A string containing a full set of spaces for a single level of indentation, or null for no pretty printing.
-
-
Method Detail
-
array
public JSONStringer array() throws JSONException
Begins encoding a new array. Each call to this method must be paired with a call toendArray().- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
endArray
public JSONStringer endArray() throws JSONException
Ends encoding the current array.- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
object
public JSONStringer object() throws JSONException
Begins encoding a new object. Each call to this method must be paired with a call toendObject().- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
endObject
public JSONStringer endObject() throws JSONException
Ends encoding the current object.- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
open
JSONStringer open(JSONStringer.Scope empty, java.lang.String openBracket) throws JSONException
Enters a new scope by appending any necessary whitespace and the given bracket.- Throws:
JSONException
-
close
JSONStringer close(JSONStringer.Scope empty, JSONStringer.Scope nonempty, java.lang.String closeBracket) throws JSONException
Closes the current scope by appending any necessary whitespace and the given bracket.- Throws:
JSONException
-
peek
private JSONStringer.Scope peek() throws JSONException
Returns the value on the top of the stack.- Throws:
JSONException
-
replaceTop
private void replaceTop(JSONStringer.Scope topOfStack)
Replace the value on the top of the stack with the given value.
-
value
public JSONStringer value(java.lang.Object value) throws JSONException
Encodesvalue.- Parameters:
value- aJSONObject,JSONArray, String, Boolean, Integer, Long, Double or null. May not beNaNsorinfinities.- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
value
public JSONStringer value(boolean value) throws JSONException
Encodesvalueto this stringer.- Parameters:
value- The value to encode.- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
value
public JSONStringer value(double value) throws JSONException
Encodesvalueto this stringer.- Parameters:
value- a finite value. May not beNaNsorinfinities.- Returns:
- this stringer.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
value
public JSONStringer value(long value) throws JSONException
Encodesvalueto this stringer.- Parameters:
value- The value to encode.- Returns:
- this stringer.
- Throws:
JSONException- If we have an internal error. Shouldn't happen.
-
entry
public JSONStringer entry(java.util.Map.Entry<java.lang.String,java.lang.Object> entry)
Encodeskey/valuepair to this stringer.- Parameters:
entry- The entry to encode.- Returns:
- this stringer.
- Throws:
JSONException- If we have an internal error. Shouldn't happen.
-
string
private void string(java.lang.String value)
-
newline
private void newline()
-
createKey
protected JSONStringer createKey(java.lang.String name)
Creates String representation of the key (property name) to this stringer Override this method to provide your own representation of the name.- Parameters:
name- the name of the forthcoming value.- Returns:
- this stringer.
-
key
public JSONStringer key(java.lang.String name) throws JSONException
Encodes the key (property name) to this stringer.- Parameters:
name- the name of the forthcoming value. May not be null.- Returns:
- this stringer.
- Throws:
JSONException- on internal errors, shouldn't happen.
-
beforeKey
private void beforeKey() throws JSONExceptionInserts any necessary separators and whitespace before a name. Also adjusts the stack to expect the key's value.- Throws:
JSONException
-
beforeValue
private void beforeValue() throws JSONExceptionInserts any necessary separators and whitespace before a literal value, inline array, or inline object. Also adjusts the stack to expect either a closing bracket or another element.- Throws:
JSONException
-
toString
public java.lang.String toString()
Returns the encoded JSON string.
If invoked with unterminated arrays or unclosed objects, this method's return value is undefined.
Warning: although it contradicts the general contract of
Object.toString(), this method returns null if the stringer contains no data.- Overrides:
toStringin classjava.lang.Object
-
-