Class JSONWriter<W extends Writer>
java.lang.Object
com.leastfixedpoint.json.JSONWriter<W>
Writes certain Java objects as JSON to a Writer.
- java.lang.String becomes a JSON string.
- java.lang.Boolean become JSON booleans.
JSONNull.INSTANCEbecomes JSON null.- java.lang.Number and subclasses become JSON numbers (via BigDecimal).
- Java arrays and Iterable objects become JSON arrays.
- java.util.Map objects become JSON objects/maps.
If a Map has a non-String key, or an unsupported object is discovered while writing, JSONSerializationError will be thrown.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected static final char[]protected intprotected booleanprotected booleanprotected W -
Constructor Summary
ConstructorsConstructorDescriptionJSONWriter(W writer) Construct a JSONWriter that will output on the given Writer, by default without pretty indentation.JSONWriter(W writer, boolean indenting) Construct a JSONWriter that will output on the given Writer, with optional pretty indentation. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected voidbool(boolean b) protected voidemit(char c) protected voidprotected voidemitUnicode(char c) booleanAnswers true iff the writer allows java's null value to be written as JSON null.booleanAnswers true iff pretty indentation is turned on for this instance.booleanAnswers true iff the writer sorts keys when writing objects, to ensure deterministic ordering.Retrieve the underlying Writer.protected voidprotected voidvoidnewline()If pretty indentation is turned on, outputs a newline character followed by a number of spaces corresponding to the current indentation level (which is not under direct control of any public methods)protected voidvoidsetAllowJavaNull(boolean value) If given true, allows java's null value to be written as JSON null; otherwise, requires nulls to be represented with instances of JSONNull.voidsetIndentMode(boolean value) Alters the pretty indentation mode of this instance for future writes.voidsetSortKeys(boolean value) If given true, enables object key-sorting on write; false disables sorting.protected voidvoidEmit the given object as JSON to the embedded Writer.static voidSerializes value as JSON, outputting to writer, without pretty indentation.static <W extends Writer>
voidSerializes value as JSON, outputting to writer, with optional pretty indentation.static StringwriteToString(Object value) Returns JSON text corresponding to value, without pretty indentation.static StringwriteToString(Object value, boolean indenting) Returns JSON text corresponding to value, with optional pretty indentation.static StringwriteToString(String value) Returns JSON text corresponding to value.
-
Field Details
-
indentLevel
protected int indentLevel -
writer
-
indentMode
protected boolean indentMode -
sortKeys
protected boolean sortKeys -
allowJavaNull
protected boolean allowJavaNull -
hex
protected static final char[] hex
-
-
Constructor Details
-
JSONWriter
Construct a JSONWriter that will output on the given Writer, by default without pretty indentation. -
JSONWriter
Construct a JSONWriter that will output on the given Writer, with optional pretty indentation.
-
-
Method Details
-
writeTo
Serializes value as JSON, outputting to writer, without pretty indentation.- Throws:
IOException
-
writeTo
public static <W extends Writer> void writeTo(W w, Object value, boolean indenting) throws IOException Serializes value as JSON, outputting to writer, with optional pretty indentation.- Throws:
IOException
-
writeToString
Returns JSON text corresponding to value, without pretty indentation.- Throws:
JSONSerializationError
-
writeToString
Returns JSON text corresponding to value, with optional pretty indentation.- Throws:
JSONSerializationError
-
writeToString
-
getWriter
Retrieve the underlying Writer. -
getIndentMode
public boolean getIndentMode()Answers true iff pretty indentation is turned on for this instance. -
setIndentMode
public void setIndentMode(boolean value) Alters the pretty indentation mode of this instance for future writes. -
getSortKeys
public boolean getSortKeys()Answers true iff the writer sorts keys when writing objects, to ensure deterministic ordering. -
setSortKeys
public void setSortKeys(boolean value) If given true, enables object key-sorting on write; false disables sorting. -
getAllowJavaNull
public boolean getAllowJavaNull()Answers true iff the writer allows java's null value to be written as JSON null. -
setAllowJavaNull
public void setAllowJavaNull(boolean value) If given true, allows java's null value to be written as JSON null; otherwise, requires nulls to be represented with instances of JSONNull. -
newline
If pretty indentation is turned on, outputs a newline character followed by a number of spaces corresponding to the current indentation level (which is not under direct control of any public methods)- Throws:
IOException
-
write
Emit the given object as JSON to the embedded Writer.- Throws:
IOException
-
number
- Throws:
IOException
-
map
- Throws:
IOException
-
iterable
- Throws:
IOException
-
array
- Throws:
IOException
-
bool
- Throws:
IOException
-
string
- Throws:
IOException
-
emit
- Throws:
IOException
-
emit
- Throws:
IOException
-
emitUnicode
- Throws:
IOException
-