public class JsonWriter extends java.lang.Object implements ExtendedHierarchicalStreamWriter
Modifier and Type | Class and Description |
---|---|
class |
JsonWriter.Node |
Modifier and Type | Field and Description |
---|---|
static int |
DROP_ROOT_MODE
DROP_ROOT_MODE drops the JSON root node.
|
static int |
STRICT_MODE
STRICT_MODE prevents invalid JSON for single value objects when dropping the root.
|
Constructor and Description |
---|
JsonWriter(java.io.Writer writer) |
JsonWriter(java.io.Writer writer,
char[] lineIndenter) |
JsonWriter(java.io.Writer writer,
char[] lineIndenter,
java.lang.String newLine) |
JsonWriter(java.io.Writer writer,
char[] lineIndenter,
java.lang.String newLine,
int mode) |
JsonWriter(java.io.Writer writer,
int mode)
Create a JsonWriter where the writer mode can be chosen.
|
JsonWriter(java.io.Writer writer,
java.lang.String lineIndenter) |
JsonWriter(java.io.Writer writer,
java.lang.String lineIndenter,
java.lang.String newLine) |
Modifier and Type | Method and Description |
---|---|
void |
addAttribute(java.lang.String key,
java.lang.String value) |
void |
close()
Close the writer, if necessary.
|
void |
endNode() |
protected void |
endOfLine() |
void |
flush()
Flush the writer, if necessary.
|
void |
setValue(java.lang.String text)
Write the value (text content) of the current node.
|
void |
startNode(java.lang.String name)
Deprecated.
since 1.2, use startNode(String name, Class clazz) instead.
|
void |
startNode(java.lang.String name,
java.lang.Class clazz) |
HierarchicalStreamWriter |
underlyingWriter()
Return the underlying HierarchicalStreamWriter implementation.
|
protected void |
writeAttributeValue(com.thoughtworks.xstream.core.util.QuickWriter writer,
java.lang.String text) |
protected void |
writeText(com.thoughtworks.xstream.core.util.QuickWriter writer,
java.lang.String text) |
public static final int DROP_ROOT_MODE
The root node is the first level of the JSON object i.e.
{ "person": { "name": "Joe" }}will be written without root simply as
{ "name": "Joe" }Without a root node, the top level element might now also be an array. However, it is possible to generate invalid JSON unless
STRICT_MODE
is also set.
public static final int STRICT_MODE
The mode is only useful in combination with the DROP_ROOT_MODE
. An object with a
single value as first node i.e.
{ "name": "Joe" }is simply written as
"Joe"However, this is no longer valid JSON. Therefore you can activate
STRICT_MODE
and a ConversionException
is thrown instead.
public JsonWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine)
public JsonWriter(java.io.Writer writer, char[] lineIndenter)
public JsonWriter(java.io.Writer writer, java.lang.String lineIndenter, java.lang.String newLine)
public JsonWriter(java.io.Writer writer, java.lang.String lineIndenter)
public JsonWriter(java.io.Writer writer)
public JsonWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine, int mode)
public JsonWriter(java.io.Writer writer, int mode)
Following constants can be used as bit mask:
DROP_ROOT_MODE
: drop the root nodeSTRICT_MODE
: do not throw ConversionException
, if writer should
generate invalid JSONwriter
- the Writer
where the JSON is written tomode
- the JsonWriter modepublic void startNode(java.lang.String name)
startNode
in interface HierarchicalStreamWriter
public void startNode(java.lang.String name, java.lang.Class clazz)
startNode
in interface ExtendedHierarchicalStreamWriter
public void setValue(java.lang.String text)
HierarchicalStreamWriter
setValue
in interface HierarchicalStreamWriter
public void addAttribute(java.lang.String key, java.lang.String value)
addAttribute
in interface HierarchicalStreamWriter
protected void writeAttributeValue(com.thoughtworks.xstream.core.util.QuickWriter writer, java.lang.String text)
protected void writeText(com.thoughtworks.xstream.core.util.QuickWriter writer, java.lang.String text)
public void endNode()
endNode
in interface HierarchicalStreamWriter
protected void endOfLine()
public void flush()
HierarchicalStreamWriter
flush
in interface HierarchicalStreamWriter
public void close()
HierarchicalStreamWriter
close
in interface HierarchicalStreamWriter
public HierarchicalStreamWriter underlyingWriter()
HierarchicalStreamWriter
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.
underlyingWriter
in interface HierarchicalStreamWriter
Copyright © 2004-2012 XStream. All Rights Reserved.