Package com.thoughtworks.xstream.io.json
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
public class JsonWriter extends AbstractJsonWriter
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 Classes Modifier and Type Class Description static class
JsonWriter.Format
Format definition for JSON.-
Nested classes/interfaces inherited from class com.thoughtworks.xstream.io.json.AbstractJsonWriter
AbstractJsonWriter.Type
-
-
Field Summary
Fields Modifier and Type Field Description private int
depth
protected JsonWriter.Format
format
private boolean
newLineProposed
protected QuickWriter
writer
-
Fields inherited from class com.thoughtworks.xstream.io.json.AbstractJsonWriter
DROP_ROOT_MODE, EXPLICIT_MODE, IEEE_754_MODE, STRICT_MODE
-
-
Constructor Summary
Constructors Constructor Description JsonWriter(java.io.Writer writer)
JsonWriter(java.io.Writer writer, char[] lineIndenter)
Deprecated.As of 1.4 useinstead
JsonWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine)
Deprecated.As of 1.4 useinstead
JsonWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine, int mode)
Deprecated.As of 1.4 useinstead
JsonWriter(java.io.Writer writer, int mode)
Create a JsonWriter where the writer mode can be chosen.JsonWriter(java.io.Writer writer, int mode, JsonWriter.Format format)
Create a JsonWriter where the writer mode can be chosen and the format definition is provided.JsonWriter(java.io.Writer writer, int mode, JsonWriter.Format format, int bufferSize)
Create a JsonWriter.JsonWriter(java.io.Writer writer, JsonWriter.Format format)
Create a JsonWriter where the format is provided.JsonWriter(java.io.Writer writer, java.lang.String lineIndenter)
Deprecated.As of 1.4 useinstead
JsonWriter(java.io.Writer writer, java.lang.String lineIndenter, java.lang.String newLine)
Deprecated.As of 1.4 useinstead
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addLabel(java.lang.String name)
Add a label to a JSON object.protected void
addValue(java.lang.String value, AbstractJsonWriter.Type type)
Add a value to a JSON object's label or to an array.void
close()
Close the writer, if necessary.protected void
endArray()
End the JSON array.private void
endNewLine()
protected void
endObject()
End the JSON object.void
flush()
Flush the writer, if necessary.protected void
nextElement()
Prepare a JSON object or array for another element.protected void
startArray()
Start a JSON array.private void
startNewLine()
protected void
startObject()
Start a JSON object.HierarchicalStreamWriter
underlyingWriter()
Return the underlying HierarchicalStreamWriter implementation.private void
writeNewLine()
private void
writeText(java.lang.String text)
-
Methods inherited from class com.thoughtworks.xstream.io.json.AbstractJsonWriter
addAttribute, endNode, getType, isArray, setValue, startNode, startNode
-
Methods inherited from class com.thoughtworks.xstream.io.AbstractWriter
encodeAttribute, encodeNode
-
-
-
-
Field Detail
-
writer
protected final QuickWriter writer
-
format
protected final JsonWriter.Format format
-
depth
private int depth
-
newLineProposed
private boolean newLineProposed
-
-
Constructor Detail
-
JsonWriter
public JsonWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine)
Deprecated.As of 1.4 useinstead
-
JsonWriter
public JsonWriter(java.io.Writer writer, char[] lineIndenter)
Deprecated.As of 1.4 useinstead
-
JsonWriter
public JsonWriter(java.io.Writer writer, java.lang.String lineIndenter, java.lang.String newLine)
Deprecated.As of 1.4 useinstead
-
JsonWriter
public JsonWriter(java.io.Writer writer, java.lang.String lineIndenter)
Deprecated.As of 1.4 useinstead
-
JsonWriter
public JsonWriter(java.io.Writer writer)
-
JsonWriter
public JsonWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine, int mode)
Deprecated.As of 1.4 useinstead
- Since:
- 1.3.1
-
JsonWriter
public JsonWriter(java.io.Writer writer, int mode)
Create a JsonWriter where the writer mode can be chosen.- Parameters:
writer
- theWriter
where the JSON is written tomode
- the JsonWriter mode- Since:
- 1.3.1
- See Also:
JsonWriter(Writer, int, Format)
-
JsonWriter
public JsonWriter(java.io.Writer writer, JsonWriter.Format format)
Create a JsonWriter where the format is provided.- Parameters:
writer
- theWriter
where the JSON is written toformat
- the JSON format definition- Since:
- 1.4
- See Also:
JsonWriter(Writer, int, Format)
-
JsonWriter
public JsonWriter(java.io.Writer writer, int mode, JsonWriter.Format format)
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
- theWriter
where the JSON is written tomode
- the JsonWriter modeformat
- the JSON format definition- Since:
- 1.4
-
JsonWriter
public JsonWriter(java.io.Writer writer, int mode, JsonWriter.Format format, int bufferSize)
Create a JsonWriter.- Parameters:
writer
- theWriter
where 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:
JsonWriter(Writer, int, Format)
-
-
Method Detail
-
flush
public void flush()
Description copied from interface:HierarchicalStreamWriter
Flush the writer, if necessary.
-
close
public void close()
Description copied from interface:HierarchicalStreamWriter
Close the writer, if necessary.
-
underlyingWriter
public HierarchicalStreamWriter underlyingWriter()
Description copied from class:AbstractWriter
Return 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:
underlyingWriter
in interfaceHierarchicalStreamWriter
- Overrides:
underlyingWriter
in classAbstractWriter
-
startObject
protected void startObject()
Start a JSON object.- Specified by:
startObject
in classAbstractJsonWriter
-
addLabel
protected void addLabel(java.lang.String name)
Add a label to a JSON object.- Specified by:
addLabel
in classAbstractJsonWriter
- Parameters:
name
- the label's name
-
addValue
protected void addValue(java.lang.String value, AbstractJsonWriter.Type type)
Add a value to a JSON object's label or to an array.- Specified by:
addValue
in classAbstractJsonWriter
- Parameters:
value
- the value itselftype
- the JSON type
-
startArray
protected void startArray()
Start a JSON array.- Specified by:
startArray
in classAbstractJsonWriter
-
nextElement
protected void nextElement()
Prepare a JSON object or array for another element.- Specified by:
nextElement
in classAbstractJsonWriter
-
endArray
protected void endArray()
End the JSON array.- Specified by:
endArray
in classAbstractJsonWriter
-
endObject
protected void endObject()
End the JSON object.- Specified by:
endObject
in classAbstractJsonWriter
-
startNewLine
private void startNewLine()
-
endNewLine
private void endNewLine()
-
writeNewLine
private void writeNewLine()
-
writeText
private void writeText(java.lang.String text)
-
-