Class AbstractJsonWriter
- java.lang.Object
-
- com.thoughtworks.xstream.io.AbstractWriter
-
- com.thoughtworks.xstream.io.json.AbstractJsonWriter
-
- All Implemented Interfaces:
ExtendedHierarchicalStreamWriter
,HierarchicalStreamWriter
- Direct Known Subclasses:
JsonWriter
public abstract class AbstractJsonWriter extends AbstractWriter
An abstract implementation of a writer that calls abstract methods to build JSON structures. Note, that XStream's implicit collection feature is only compatible with the syntax inEXPLICIT_MODE
.- Since:
- 1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
AbstractJsonWriter.IllegalWriterStateException
private static class
AbstractJsonWriter.StackElement
static class
AbstractJsonWriter.Type
-
Field Summary
Fields Modifier and Type Field Description static int
DROP_ROOT_MODE
DROP_ROOT_MODE drops the JSON root node.private int
expectedStates
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.private int
mode
private static java.util.Set
NUMBER_TYPES
private FastStack
stack
private static int
STATE_END_ATTRIBUTES
private static int
STATE_END_ELEMENTS
private static int
STATE_END_OBJECT
private static int
STATE_NEXT_ATTRIBUTE
private static int
STATE_NEXT_ELEMENT
private static int
STATE_ROOT
private static int
STATE_SET_VALUE
private static int
STATE_START_ATTRIBUTES
private static int
STATE_START_ELEMENTS
private static int
STATE_START_OBJECT
static int
STRICT_MODE
STRICT_MODE prevents invalid JSON for single value objects when dropping the root.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addAttribute(java.lang.String name, java.lang.String value)
protected abstract void
addLabel(java.lang.String name)
Add a label to a JSON object.protected abstract void
addValue(java.lang.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(java.lang.Class clazz)
Method to return the appropriate JSON type for a Java type.private void
handleCheckedStateTransition(int requiredState, java.lang.String elementToAdd, java.lang.String valueToAdd)
private int
handleStateTransition(int currentState, int requiredState, java.lang.String elementToAdd, java.lang.String valueToAdd)
protected boolean
isArray(java.lang.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(java.lang.String text)
Write the value (text content) of the current node.protected abstract void
startArray()
Start a JSON array.void
startNode(java.lang.String name)
void
startNode(java.lang.String name, java.lang.Class clazz)
protected abstract void
startObject()
Start a JSON object.-
Methods inherited from class com.thoughtworks.xstream.io.AbstractWriter
encodeAttribute, encodeNode, underlyingWriter
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.thoughtworks.xstream.io.HierarchicalStreamWriter
close, flush
-
-
-
-
Field Detail
-
DROP_ROOT_MODE
public static final int DROP_ROOT_MODE
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.- Since:
- 1.3.1
- See Also:
- Constant Field Values
-
STRICT_MODE
public static final int STRICT_MODE
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 aConversionException
is thrown instead.- Since:
- 1.3.1
- See Also:
- Constant Field Values
-
EXPLICIT_MODE
public static final int EXPLICIT_MODE
EXPLICIT_MODE assures that all data has its explicit equivalent in the resulting JSON.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
orDROP_ROOT_MODE
.- Since:
- 1.4
- See Also:
- Constant Field Values
-
IEEE_754_MODE
public static final int IEEE_754_MODE
IEEE_754_MODE keeps precision of 64-bit integer values.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.
- Since:
- 1.4.5
- See Also:
- ECMA Specification: The Number Type, Constant Field Values
-
STATE_ROOT
private static final int STATE_ROOT
- See Also:
- Constant Field Values
-
STATE_END_OBJECT
private static final int STATE_END_OBJECT
- See Also:
- Constant Field Values
-
STATE_START_OBJECT
private static final int STATE_START_OBJECT
- See Also:
- Constant Field Values
-
STATE_START_ATTRIBUTES
private static final int STATE_START_ATTRIBUTES
- See Also:
- Constant Field Values
-
STATE_NEXT_ATTRIBUTE
private static final int STATE_NEXT_ATTRIBUTE
- See Also:
- Constant Field Values
-
STATE_END_ATTRIBUTES
private static final int STATE_END_ATTRIBUTES
- See Also:
- Constant Field Values
-
STATE_START_ELEMENTS
private static final int STATE_START_ELEMENTS
- See Also:
- Constant Field Values
-
STATE_NEXT_ELEMENT
private static final int STATE_NEXT_ELEMENT
- See Also:
- Constant Field Values
-
STATE_END_ELEMENTS
private static final int STATE_END_ELEMENTS
- See Also:
- Constant Field Values
-
STATE_SET_VALUE
private static final int STATE_SET_VALUE
- See Also:
- Constant Field Values
-
NUMBER_TYPES
private static final java.util.Set NUMBER_TYPES
-
mode
private int mode
-
stack
private FastStack stack
-
expectedStates
private int expectedStates
-
-
Constructor Detail
-
AbstractJsonWriter
public AbstractJsonWriter()
Construct a JSON writer.- Since:
- 1.4
-
AbstractJsonWriter
public AbstractJsonWriter(int mode)
Construct a JSON writer with a special mode.- Parameters:
mode
- a bit mask of the mode constants- Since:
- 1.4
-
AbstractJsonWriter
public AbstractJsonWriter(NameCoder nameCoder)
Construct a JSON writer with a special name coder.- Parameters:
nameCoder
- the name coder to use- Since:
- 1.4
-
AbstractJsonWriter
public AbstractJsonWriter(int mode, NameCoder nameCoder)
Construct a JSON writer with a special mode and name coder.- Parameters:
mode
- a bit mask of the mode constantsnameCoder
- the name coder to use- Since:
- 1.4
-
-
Method Detail
-
startNode
public void startNode(java.lang.String name, java.lang.Class clazz)
Description copied from class:AbstractWriter
- Specified by:
startNode
in interfaceExtendedHierarchicalStreamWriter
- Overrides:
startNode
in classAbstractWriter
-
startNode
public void startNode(java.lang.String name)
-
addAttribute
public void addAttribute(java.lang.String name, java.lang.String value)
-
setValue
public void setValue(java.lang.String text)
Description copied from interface:HierarchicalStreamWriter
Write the value (text content) of the current node.
-
endNode
public void endNode()
-
handleCheckedStateTransition
private void handleCheckedStateTransition(int requiredState, java.lang.String elementToAdd, java.lang.String valueToAdd)
-
handleStateTransition
private int handleStateTransition(int currentState, int requiredState, java.lang.String elementToAdd, java.lang.String valueToAdd)
-
getType
protected AbstractJsonWriter.Type getType(java.lang.Class clazz)
Method to return the appropriate JSON type for a Java type.- Parameters:
clazz
- the type- Returns:
- One of the
AbstractJsonWriter.Type
instances - Since:
- 1.4.4
-
isArray
protected boolean isArray(java.lang.Class clazz)
Method to declare various Java types to be handles as JSON array.- Parameters:
clazz
- the type- Returns:
true
if handles as array- Since:
- 1.4
-
startObject
protected abstract void startObject()
Start a JSON object.- Since:
- 1.4
-
addLabel
protected abstract void addLabel(java.lang.String name)
Add a label to a JSON object.- Parameters:
name
- the label's name- Since:
- 1.4
-
addValue
protected abstract void addValue(java.lang.String value, AbstractJsonWriter.Type type)
Add a value to a JSON object's label or to an array.- Parameters:
value
- the value itselftype
- the JSON type- Since:
- 1.4
-
startArray
protected abstract void startArray()
Start a JSON array.- Since:
- 1.4
-
nextElement
protected abstract void nextElement()
Prepare a JSON object or array for another element.- Since:
- 1.4
-
endArray
protected abstract void endArray()
End the JSON array.- Since:
- 1.4
-
endObject
protected abstract void endObject()
End the JSON object.- Since:
- 1.4
-
-