Class JsonWriter
java.lang.Object
com.thoughtworks.xstream.io.AbstractWriter
com.thoughtworks.xstream.io.json.AbstractJsonWriter
com.thoughtworks.xstream.io.json.JsonWriter
- All Implemented Interfaces:
ExtendedHierarchicalStreamWriter, HierarchicalStreamWriter
- Direct Known Subclasses:
JsonHierarchicalStreamWriter
A simple writer that outputs JSON in a pretty-printed indented stream. Arrays, Lists and Sets
rely on you NOT using XStream.addImplicitCollection(..).
- Since:
- 1.3.1
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class AbstractJsonWriter
AbstractJsonWriter.Type -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprotected final JsonWriter.Formatprivate booleanprotected final QuickWriterFields inherited from class AbstractJsonWriter
DROP_ROOT_MODE, EXPLICIT_MODE, IEEE_754_MODE, STRICT_MODE -
Constructor Summary
ConstructorsConstructorDescriptionJsonWriter(Writer writer) JsonWriter(Writer writer, char[] lineIndenter) Deprecated.JsonWriter(Writer writer, char[] lineIndenter, String newLine) Deprecated.As of 1.4 useinsteadJsonWriter(Writer writer, char[] lineIndenter, String newLine, int mode) Deprecated.As of 1.4 useinsteadJsonWriter(Writer writer, int mode) Create a JsonWriter where the writer mode can be chosen.JsonWriter(Writer writer, int mode, JsonWriter.Format format) Create a JsonWriter where the writer mode can be chosen and the format definition is provided.JsonWriter(Writer writer, int mode, JsonWriter.Format format, int bufferSize) Create a JsonWriter.JsonWriter(Writer writer, JsonWriter.Format format) Create a JsonWriter where the format is provided.JsonWriter(Writer writer, String lineIndenter) Deprecated.As of 1.4 useinsteadJsonWriter(Writer writer, String lineIndenter, String newLine) Deprecated.As of 1.4 useinstead -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAdd a label to a JSON object.protected voidaddValue(String value, AbstractJsonWriter.Type type) Add a value to a JSON object's label or to an array.voidclose()Close the writer, if necessary.protected voidendArray()End the JSON array.private voidprotected voidEnd the JSON object.voidflush()Flush the writer, if necessary.protected voidPrepare a JSON object or array for another element.protected voidStart a JSON array.private voidprotected voidStart a JSON object.Return the underlying HierarchicalStreamWriter implementation.private voidprivate voidMethods inherited from class AbstractJsonWriter
addAttribute, endNode, getType, isArray, setValue, startNode, startNodeMethods inherited from class AbstractWriter
encodeAttribute, encodeNode
-
Field Details
-
writer
-
format
-
depth
private int depth -
newLineProposed
private boolean newLineProposed
-
-
Constructor Details
-
JsonWriter
-
JsonWriter
-
JsonWriter
-
JsonWriter
-
JsonWriter
-
JsonWriter
-
JsonWriter
-
JsonWriter
Create a JsonWriter where the format is provided.- Parameters:
writer- theWriterwhere the JSON is written toformat- the JSON format definition- Since:
- 1.4
- See Also:
-
JsonWriter
Create a JsonWriter where the writer mode can be chosen and the format definition is provided.Following constants can be used as bit mask for the mode:
AbstractJsonWriter.DROP_ROOT_MODE: drop the root nodeAbstractJsonWriter.STRICT_MODE: do not throwConversionException, if writer should generate invalid JSONAbstractJsonWriter.EXPLICIT_MODE: ensure that all available data is explicitly written even if addition objects must be added
- Parameters:
writer- theWriterwhere the JSON is written tomode- the JsonWriter modeformat- the JSON format definition- Since:
- 1.4
-
JsonWriter
Create a JsonWriter.- Parameters:
writer- theWriterwhere the JSON is written tomode- the JsonWriter modeformat- the JSON format definitionbufferSize- the buffer size of the internally used QuickWriter- Since:
- 1.4
- See Also:
-
-
Method Details
-
flush
public void flush()Description copied from interface:HierarchicalStreamWriterFlush the writer, if necessary. -
close
public void close()Description copied from interface:HierarchicalStreamWriterClose the writer, if necessary. -
underlyingWriter
Description copied from class:AbstractWriterReturn the underlying HierarchicalStreamWriter implementation.If a Converter needs to access methods of a specific HierarchicalStreamWriter implementation that are not defined in the HierarchicalStreamWriter interface, it should call this method before casting. This is because the writer passed to the Converter is often wrapped/decorated by another implementation to provide additional functionality (such as XPath tracking).
For example:
MySpecificWriter mySpecificWriter = (MySpecificWriter)writer; // INCORRECT! mySpecificWriter.doSomethingSpecific();
MySpecificWriter mySpecificWriter = (MySpecificWriter)writer.underlyingWriter(); // CORRECT! mySpecificWriter.doSomethingSpecific();
Implementations of HierarchicalStreamWriter should return 'this', unless they are a decorator, in which case they should delegate to whatever they are wrapping.
- Specified by:
underlyingWriterin interfaceHierarchicalStreamWriter- Overrides:
underlyingWriterin classAbstractWriter
-
startObject
protected void startObject()Start a JSON object.- Specified by:
startObjectin classAbstractJsonWriter
-
addLabel
Add a label to a JSON object.- Specified by:
addLabelin classAbstractJsonWriter- Parameters:
name- the label's name
-
addValue
Add a value to a JSON object's label or to an array.- Specified by:
addValuein classAbstractJsonWriter- Parameters:
value- the value itselftype- the JSON type
-
startArray
protected void startArray()Start a JSON array.- Specified by:
startArrayin classAbstractJsonWriter
-
nextElement
protected void nextElement()Prepare a JSON object or array for another element.- Specified by:
nextElementin classAbstractJsonWriter
-
endArray
-
endObject
-
startNewLine
private void startNewLine() -
endNewLine
private void endNewLine() -
writeNewLine
private void writeNewLine() -
writeText
-
instead