java.lang.Object | |
↳ | com.thoughtworks.xstream.io.json.JsonWriter |
Known Direct Subclasses |
A simple writer that outputs JSON in a pretty-printed indented stream. Arrays, Lists and Sets rely on you NOT using XStream.addImplicitCollection(..).
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
JsonWriter.Node |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | DROP_ROOT_MODE | DROP_ROOT_MODE drops the JSON root node. | |||||||||
int | STRICT_MODE | STRICT_MODE prevents invalid JSON for single value objects when dropping the root. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a JsonWriter where the writer mode can be chosen.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Close the writer, if necessary.
| |||||||||||
Flush the writer, if necessary.
| |||||||||||
Write the value (text content) of the current node.
| |||||||||||
This method is deprecated.
since 1.2, use startNode(String name, Class clazz) instead.
| |||||||||||
Return the underlying HierarchicalStreamWriter implementation.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriter
| |||||||||||
From interface
com.thoughtworks.xstream.io.HierarchicalStreamWriter
|
DROP_ROOT_MODE drops the JSON root node.
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.
STRICT_MODE prevents invalid JSON for single value objects when dropping the root.
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.
Create a JsonWriter where the writer mode can be chosen.
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 to |
---|---|
mode | the JsonWriter mode |
Close the writer, if necessary.
Flush the writer, if necessary.
This method is deprecated.
since 1.2, use startNode(String name, Class clazz) instead.
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.