Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.thoughtworks.xstream.io.json
Class JsonWriter

    java.lang.Object
      com.thoughtworks.xstream.io.json.JsonWriter

All Implemented Interfaces:
  ExtendedHierarchicalStreamWriter, HierarchicalStreamWriter

Direct Known Subclasses:
  JsonHierarchicalStreamWriter

    public class JsonWriter

extends Object
implements ExtendedHierarchicalStreamWriter

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
Author:
  Paul Hammant, Jörg Schaible

Nested Class Summary
`class` | `JsonWriter.Node`

Field Summary
`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 Summary
`JsonWriter(Writer writer)`
`JsonWriter(Writer writer, char[] lineIndenter)`
`JsonWriter(Writer writer, char[] lineIndenter, String newLine)`
`JsonWriter(Writer writer, char[] lineIndenter, String newLine, int mode)`
`JsonWriter(Writer writer, int mode)` Create a JsonWriter where the writer mode can be chosen.
`JsonWriter(Writer writer, String lineIndenter)`
`JsonWriter(Writer writer, String lineIndenter, String newLine)`

Method Summary
`void` | `addAttribute(String key, String value)`
`void` | `close()` Close the writer, if necessary.
`void` | `endNode()`
`protected void` | `endOfLine()`
`void` | `flush()` Flush the writer, if necessary.
`void` | `setValue(String text)` Write the value (text content) of the current node.
`void` | `startNode(String name)` Deprecated. since 1.2, use startNode(String name, Class clazz) instead.
`void` | `startNode(String name, Class clazz)`
`HierarchicalStreamWriter` | `underlyingWriter()` Return the underlying HierarchicalStreamWriter implementation.
`protected void` | `writeAttributeValue(com.thoughtworks.xstream.core.util.QuickWriter writer, String text)`
`protected void` | `writeText(com.thoughtworks.xstream.core.util.QuickWriter writer, String text)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

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 a `ConversionException` is thrown instead.

  Since:
    1.3.1
  See Also:
    Constant Field Values

Constructor Detail

### JsonWriter

    public JsonWriter(Writer writer,
                      char[] lineIndenter,
                      String newLine)

### JsonWriter

    public JsonWriter(Writer writer,
                      char[] lineIndenter)

### JsonWriter

    public JsonWriter(Writer writer,
                      String lineIndenter,
                      String newLine)

### JsonWriter

    public JsonWriter(Writer writer,
                      String lineIndenter)

### JsonWriter

    public JsonWriter(Writer writer)

### JsonWriter

    public JsonWriter(Writer writer,
                      char[] lineIndenter,
                      String newLine,
                      int mode)

Since:
  1.3.1

### JsonWriter

    public JsonWriter(Writer writer,
                      int mode)

  Create a JsonWriter where the writer mode can be chosen.

  Following constants can be used as bit mask:

  - `DROP_ROOT_MODE`: drop the root node
  - `STRICT_MODE`: do not throw `ConversionException`, if writer should generate invalid JSON

  Parameters:
    `writer` - the `Writer` where the JSON is written to
    `mode` - the JsonWriter mode
  Since:
    1.3.1

Method Detail

### startNode

    public void startNode(String name)

  Deprecated. since 1.2, use startNode(String name, Class clazz) instead.

  Specified by:
    `startNode` in interface `HierarchicalStreamWriter`

### startNode

    public void startNode(String name,
                          Class clazz)

  Specified by:
    `startNode` in interface `ExtendedHierarchicalStreamWriter`

### setValue

    public void setValue(String text)

  Description copied from interface: `HierarchicalStreamWriter`
  Write the value (text content) of the current node.

  Specified by:
    `setValue` in interface `HierarchicalStreamWriter`

### addAttribute

    public void addAttribute(String key,
                             String value)

  Specified by:
    `addAttribute` in interface `HierarchicalStreamWriter`

### writeAttributeValue

    protected void writeAttributeValue(com.thoughtworks.xstream.core.util.QuickWriter writer,
                                       String text)

### writeText

    protected void writeText(com.thoughtworks.xstream.core.util.QuickWriter writer,
                             String text)

### endNode

    public void endNode()

  Specified by:
    `endNode` in interface `HierarchicalStreamWriter`

### endOfLine

    protected void endOfLine()

### flush

    public void flush()

  Description copied from interface: `HierarchicalStreamWriter`
  Flush the writer, if necessary.

  Specified by:
    `flush` in interface `HierarchicalStreamWriter`

### close

    public void close()

  Description copied from interface: `HierarchicalStreamWriter`
  Close the writer, if necessary.

  Specified by:
    `close` in interface `HierarchicalStreamWriter`

### underlyingWriter

    public HierarchicalStreamWriter underlyingWriter()

  Description copied from interface: `HierarchicalStreamWriter`
  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 interface `HierarchicalStreamWriter`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2004-2008 XStream. All Rights Reserved.