public abstract class AbstractJsonWriter extends AbstractWriter
EXPLICIT_MODE
.Modifier and Type | Class and Description |
---|---|
static class |
AbstractJsonWriter.Type |
Modifier and Type | Field and Description |
---|---|
static int |
DROP_ROOT_MODE
DROP_ROOT_MODE drops the JSON root node.
|
static int |
EXPLICIT_MODE
EXPLICIT_MODE assures that all data has its explicit equivalent in the resulting JSON.
|
static int |
IEEE_754_MODE
IEEE_754_MODE keeps precision of 64-bit integer values.
|
static int |
STRICT_MODE
STRICT_MODE prevents invalid JSON for single value objects when dropping the root.
|
Constructor and Description |
---|
AbstractJsonWriter()
Construct a JSON writer.
|
AbstractJsonWriter(int mode)
Construct a JSON writer with a special mode.
|
AbstractJsonWriter(int mode,
NameCoder nameCoder)
Construct a JSON writer with a special mode and name coder.
|
AbstractJsonWriter(NameCoder nameCoder)
Construct a JSON writer with a special name coder.
|
Modifier and Type | Method and Description |
---|---|
void |
addAttribute(String name,
String value) |
protected abstract void |
addLabel(String name)
Add a label to a JSON object.
|
protected abstract void |
addValue(String value,
AbstractJsonWriter.Type type)
Add a value to a JSON object's label or to an array.
|
protected abstract void |
endArray()
End the JSON array.
|
void |
endNode() |
protected abstract void |
endObject()
End the JSON object.
|
protected AbstractJsonWriter.Type |
getType(Class clazz)
Method to return the appropriate JSON type for a Java type.
|
protected boolean |
isArray(Class clazz)
Method to declare various Java types to be handles as JSON array.
|
protected abstract void |
nextElement()
Prepare a JSON object or array for another element.
|
void |
setValue(String text)
Write the value (text content) of the current node.
|
protected abstract void |
startArray()
Start a JSON array.
|
void |
startNode(String name) |
void |
startNode(String name,
Class clazz) |
protected abstract void |
startObject()
Start a JSON object.
|
encodeAttribute, encodeNode, underlyingWriter
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
close, flush
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 static final int EXPLICIT_MODE
XStream is normally using attributes in XML that have no real equivalent in JSON. Additionally it is essential in XML that the individual child elements of a tag keep order and may have the same tag name. XStream's model relies on both characteristics. However, properties of a JSON object do not have a defined order, but their names have to be unique. Only a JSON array defines the order of its elements.
Therefore XStream uses in explicit mode a JSON format that supports the original requirements at the expense of the simplicity of the JSON objects and arrays. Each Java object will be represented by a JSON object with a single property representing the name of the object and an array as value that contains two more arrays. The first one contains a JSON object with all attributes, the second one the value of the Java object which can be null, a string or integer value or again a new JSON object representing a Java object. Here an example of an string array with one member, where the array and the string has an additional attribute 'id':
{"string-array":[[{"id":"1"}],[{"string":[[{"id":"2"}],["Joe"]]}]]}
This format can be used to always deserialize into Java again.
This mode cannot combined with STRICT_MODE
or DROP_ROOT_MODE
.
public static final int IEEE_754_MODE
In JavaScript every number is expressed as 64-bit double value with a precision of 53 bits following IEEE 754. Therefore it is not possible to represent the complete value range of 64-bit integer values. Any integer value > 253 (9007199254740992) or < -253 (-9007199254740992) will therefore be written as string value.
CAUTION: A client must be aware that the element may contain a number or a string value.
public AbstractJsonWriter()
public AbstractJsonWriter(int mode)
mode
- a bit mask of the mode constantspublic AbstractJsonWriter(NameCoder nameCoder)
nameCoder
- the name coder to usepublic AbstractJsonWriter(int mode, NameCoder nameCoder)
mode
- a bit mask of the mode constantsnameCoder
- the name coder to usepublic void startNode(String name, Class clazz)
AbstractWriter
startNode
in interface ExtendedHierarchicalStreamWriter
startNode
in class AbstractWriter
public void startNode(String name)
public void setValue(String text)
HierarchicalStreamWriter
public void endNode()
protected AbstractJsonWriter.Type getType(Class clazz)
clazz
- the typeAbstractJsonWriter.Type
instancesprotected boolean isArray(Class clazz)
clazz
- the typetrue
if handles as arrayprotected abstract void startObject()
protected abstract void addLabel(String name)
name
- the label's nameprotected abstract void addValue(String value, AbstractJsonWriter.Type type)
value
- the value itselftype
- the JSON typeprotected abstract void startArray()
protected abstract void nextElement()
protected abstract void endArray()
protected abstract void endObject()
Copyright © 2004–2017 XStream. All rights reserved.