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.INSTANCE
becomes 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 boolean
protected static final char[]
protected int
protected boolean
protected boolean
protected 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 void
protected void
bool
(boolean b) protected void
emit
(char c) protected void
protected void
emitUnicode
(char c) boolean
Answers true iff the writer allows java's null value to be written as JSON null.boolean
Answers true iff pretty indentation is turned on for this instance.boolean
Answers true iff the writer sorts keys when writing objects, to ensure deterministic ordering.Retrieve the underlying Writer.protected void
protected void
void
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)protected void
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.void
setIndentMode
(boolean value) Alters the pretty indentation mode of this instance for future writes.void
setSortKeys
(boolean value) If given true, enables object key-sorting on write; false disables sorting.protected void
void
Emit the given object as JSON to the embedded Writer.static void
Serializes 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 String
writeToString
(Object value) Returns JSON text corresponding to value, without pretty indentation.static String
writeToString
(Object value, boolean indenting) Returns JSON text corresponding to value, with optional pretty indentation.static String
writeToString
(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
-