public class SimpleJsonWriter
extends java.lang.Object
implements java.io.Closeable
Modifier and Type | Class and Description |
---|---|
(package private) static class |
SimpleJsonWriter.State
Internal state.
|
Constructor and Description |
---|
SimpleJsonWriter(StringCollector stringCollector)
Constructor.
|
SimpleJsonWriter(StringCollector stringCollector,
int basicIndent)
Constructor.
|
SimpleJsonWriter(StringCollector stringCollector,
int basicIndent,
boolean relaxed)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
SimpleJsonWriter |
array(boolean... flags)
Convenience method for outputting a whole array of boolean flags.
|
SimpleJsonWriter |
array(double... numbers)
Convenience method for outputting a whole array of double values.
|
SimpleJsonWriter |
array(int... numbers)
Convenience method for outputting a whole array of integers.
|
SimpleJsonWriter |
array(java.lang.Iterable<java.lang.String> elements)
Convenience method for outputting a whole array of strings.
|
SimpleJsonWriter |
array(PrimitiveBooleanIterable flags)
Convenience method for outputting a whole array of boolean flags.
|
SimpleJsonWriter |
array(PrimitiveDoubleIterable numbers)
Convenience method for outputting a whole array of double values.
|
SimpleJsonWriter |
array(PrimitiveIntIterable numbers)
Convenience method for outputting a whole array of integers.
|
SimpleJsonWriter |
array(java.lang.String... elements)
Convenience method for outputting a whole array of strings.
|
SimpleJsonWriter |
beginArray()
Start outputting a JSON array.
|
SimpleJsonWriter |
beginObject()
Start outputting a JSON object.
|
<T> SimpleJsonWriter |
booleanArray(java.lang.Iterable<T> elements,
java.util.function.Predicate<? super T> boolMapping)
Convenience method for outputting a whole array of boolean flags
created from general elements.
|
void |
close()
Close this writer.
|
SimpleJsonWriter |
doubleArray(java.lang.Iterable<? extends java.lang.Number> numbers)
Convenience method for outputting a whole array of numbers as double values.
|
<T> SimpleJsonWriter |
doubleArray(java.lang.Iterable<T> elements,
java.util.function.ToDoubleFunction<? super T> doubleMapping)
Convenience method for outputting a whole array of double values
created from general elements.
|
SimpleJsonWriter |
endArray()
End outputting a JSON array.
|
SimpleJsonWriter |
endObject()
End outputting a JSON object.
|
SimpleJsonWriter |
intArray(java.lang.Iterable<? extends java.lang.Number> numbers)
Convenience method for outputting a whole array of numbers as integers.
|
<T> SimpleJsonWriter |
intArray(java.lang.Iterable<T> elements,
java.util.function.ToIntFunction<? super T> intMapping)
Convenience method for outputting a whole array of integers
created from general elements.
|
SimpleJsonWriter |
key(java.lang.String key)
Output the key of a key-value pair.
|
static void |
main(java.lang.String[] args)
Test code
|
SimpleJsonWriter |
nul()
Output a
null value. |
<T> SimpleJsonWriter |
stringArray(java.lang.Iterable<T> elements,
java.util.function.Function<? super T,java.lang.String> toStringFunction)
Convenience method for outputting a whole array of strings
created from general elements.
|
SimpleJsonWriter |
value(boolean value)
Output a boolean value.
|
SimpleJsonWriter |
value(double value)
Output a double value.
|
SimpleJsonWriter |
value(int value)
Output an integer value.
|
SimpleJsonWriter |
value(java.lang.String value)
Output a string value.
|
public SimpleJsonWriter(@NotNull StringCollector stringCollector)
stringCollector
- string collector to write topublic SimpleJsonWriter(@NotNull StringCollector stringCollector, int basicIndent)
stringCollector
- string collector to write tobasicIndent
- basic indentation, use -1
for optimal compactness w/o newlines,
0
for no indentation but newlines, or greater for newlines and indentationpublic SimpleJsonWriter(@NotNull StringCollector stringCollector, int basicIndent, boolean relaxed)
stringCollector
- string collector to write tobasicIndent
- basic indentation, use -1
for optimal compactness w/o newlines,
0
for no indentation but newlines, or greater for newlines and indentationrelaxed
- by default the writer expects to be started with a value, object, or array.
If this parameter is true
a key is also allowed.@NotNull public SimpleJsonWriter key(@NotNull java.lang.String key)
key
- key to addjava.lang.IllegalStateException
- if adding a key is currently not allowed@NotNull public SimpleJsonWriter value(@Nullable java.lang.String value)
A string value can appear after a key as part of a key-value pair, inside an array, or as single element.
value
- string valuejava.lang.IllegalStateException
- if adding a value is currently not allowed@NotNull public SimpleJsonWriter value(int value)
A numeric value can appear after a key as part of a key-value pair, inside an array, or as single element. Please note that all numeric values in JSON are forwarded to 64bit double values, so this method is only for convenience.
value
- integer valuejava.lang.IllegalStateException
- if adding a value is currently not allowed@NotNull public SimpleJsonWriter value(double value)
A numeric value can appear after a key as part of a key-value pair,
inside an array, or as single element.
Please note that all numeric values in JSON are forwarded to
64bit double values, so if you call this with a long
argument you might not be able to reread this value correctly.
value
- double valuejava.lang.IllegalStateException
- if adding a value is currently not allowed@NotNull public SimpleJsonWriter value(boolean value)
A boolean value can appear after a key as part of a key-value pair, inside an array, or as single element if this is enabled for this writer.
value
- boolean valuejava.lang.IllegalStateException
- if adding a value is currently not allowed@NotNull public SimpleJsonWriter nul()
null
value.
A null value can appear after a key as part of a key-value pair, inside an array, or as single element.
java.lang.IllegalStateException
- if adding a value is currently not allowed@NotNull public SimpleJsonWriter beginObject()
An object can appear as value of a key-value pair, as element of an array, or as root element.
java.lang.IllegalStateException
- if adding an object is currently not allowed@NotNull public SimpleJsonWriter endObject()
This will close the currently outputted object, and is only allowed at places where a key is also allowed.
java.lang.IllegalStateException
- if no object is open, if this is called inside an array,
or when the value of a key-value pair is not yet outputted@NotNull public SimpleJsonWriter beginArray()
An array can appear as value of a key-value pair, as element of an array, or as root element.
java.lang.IllegalStateException
- if adding an object is currently not allowed@NotNull public SimpleJsonWriter endArray()
This will close the currently outputted array, and is only if currently inside an array.
java.lang.IllegalStateException
- if no array is open, or if this is called inside an object@NotNull public SimpleJsonWriter array(@NotNull java.lang.String... elements)
elements
- string elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull java.lang.Iterable<java.lang.String> elements)
elements
- string elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public <T> SimpleJsonWriter stringArray(@NotNull java.lang.Iterable<T> elements, @NotNull java.util.function.Function<? super T,java.lang.String> toStringFunction)
T
- element typeelements
- general elementstoStringFunction
- function which creates strings from the elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull int... numbers)
numbers
- int elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull PrimitiveIntIterable numbers)
numbers
- int elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter intArray(@NotNull java.lang.Iterable<? extends java.lang.Number> numbers)
numbers
- number elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public <T> SimpleJsonWriter intArray(@NotNull java.lang.Iterable<T> elements, @NotNull java.util.function.ToIntFunction<? super T> intMapping)
T
- element typeelements
- general elementsintMapping
- function which creates integers from the elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull double... numbers)
numbers
- double elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull PrimitiveDoubleIterable numbers)
numbers
- double elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter doubleArray(@NotNull java.lang.Iterable<? extends java.lang.Number> numbers)
numbers
- number elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public <T> SimpleJsonWriter doubleArray(@NotNull java.lang.Iterable<T> elements, @NotNull java.util.function.ToDoubleFunction<? super T> doubleMapping)
T
- element typeelements
- general elementsdoubleMapping
- function which creates doubles from the elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull boolean... flags)
flags
- boolean elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public SimpleJsonWriter array(@NotNull PrimitiveBooleanIterable flags)
flags
- boolean elementsjava.lang.IllegalStateException
- if adding an array is not allowed@NotNull public <T> SimpleJsonWriter booleanArray(@NotNull java.lang.Iterable<T> elements, @NotNull java.util.function.Predicate<? super T> boolMapping)
T
- element typeelements
- general elementsboolMapping
- function which creates booleans from the elementsjava.lang.IllegalStateException
- if adding an array is not allowedpublic void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public static void main(java.lang.String[] args) throws java.io.IOException
args
- ignoredjava.io.IOException
- when writing fails