org.apache.synapse.commons.staxon.core.json

## Class JsonXMLStreamWriter

- java.lang.Object
- - org.apache.synapse.commons.staxon.core.base.AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>
  - - org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter

- All Implemented Interfaces:
    javax.xml.stream.XMLStreamWriter

      public class JsonXMLStreamWriter
      extends AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>

  JSON XML stream writer.

  #### Limitations

  - Mixed content (e.g. `<alice>bob<edgar/></alice>`) is not supported.
  - `writeDTD(...)` and `writeEntityRef(...)` are not supported.
  - `writeCData(...)` delegates to writeCharacters(...).
  - `writeComment(...)` does nothing.
  - `writeProcessingInstruction(...)` does nothing (except for target `xml-multiple`, see below).

  The writer may consume processing instructions (e.g. `<?xml-multiple element-name?>`) to properly insert JSON array tokens (`'['` and `']'`). The client provides this instruction through the `AbstractXMLStreamWriter.writeProcessingInstruction(String, String)` method, passing the (possibly prefixed) field name as data e.g.

         ...
         writer.writeProcessingInstruction("xml-multiple", "item");
         for (Item item : items) {
           writer.writeStartElement("item");
           ...
           writer.writeEndElement();
         }
         ...

  The element name passed as processing instruction data is optional. If omitted, the next element within the current scope will start an array. Note, that this method does not allow to create empty arrays (in fact, the above code sample could create unexpected results, if the name would have been omitted and collection were empty).

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `JsonXMLStreamWriter(JsonStreamTarget target, boolean repairNamespaces, boolean multiplePI, char namespaceSeparator, boolean namespaceDeclarations)` Create writer instance.
    `JsonXMLStreamWriter(JsonStreamTarget target, boolean repairNamespaces, boolean multiplePI, char namespaceSeparator, boolean namespaceDeclarations, boolean xmlNilReadWriteEnabled, boolean xmlWriteNullForEmptyElement)` Create writer instance.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `void` | `flush()`
    `protected void` | `writeAttr(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI, java.lang.String value)` Write attribute.
    `void` | `writeAttribute(java.lang.String prefix, java.lang.String namespaceURI, java.lang.String localName, java.lang.String value)`
    `void` | `writeBoolean(java.lang.Boolean value)` Write boolean value.
    `protected void` | `writeData(java.lang.Object data, int type)` Write characters/comment/dtd/entity data.
    `void` | `writeEndArray()`
    `void` | `writeEndDocument()`
    `protected void` | `writeEndElementTag()` Write end element tag.
    `void` | `writeNamespace(java.lang.String prefix, java.lang.String namespaceURI)`
    `protected void` | `writeNsDecl(java.lang.String prefix, java.lang.String namespaceURI)` Write namespace declaration.
    `void` | `writeNumber(java.lang.Number value)` Write number value.
    `protected void` | `writePI(java.lang.String target, java.lang.String data)` Read processing instruction.
    `void` | `writeStartArray(java.lang.String fieldName)`
    `void` | `writeStartDocument(java.lang.String encoding, java.lang.String version)`
    `protected org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo` | `writeStartElementTag(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI)` Write open start element tag.
    `protected void` | `writeStartElementTagEnd()` Write close start element tag.

    - ### Methods inherited from class org.apache.synapse.commons.staxon.core.base.AbstractXMLStreamWriter

      `getNamespaceContext, getPrefix, getProperty, getScope, isStartDocumentWritten, setDefaultNamespace, setNamespaceContext, setPrefix, writeAttribute, writeAttribute, writeCData, writeCharacters, writeCharacters, writeCharacters, writeComment, writeDefaultNamespace, writeDTD, writeEmptyElement, writeEmptyElement, writeEmptyElement, writeEndElement, writeEntityRef, writeProcessingInstruction, writeProcessingInstruction, writeStartDocument, writeStartDocument, writeStartElement, writeStartElement, writeStartElement`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JsonXMLStreamWriter

          public JsonXMLStreamWriter(JsonStreamTarget target,
                                     boolean repairNamespaces,
                                     boolean multiplePI,
                                     char namespaceSeparator,
                                     boolean namespaceDeclarations)

      Create writer instance.
      Parameters:
        `target` - stream target
        `multiplePI` - whether to consume `<xml-multiple?>` PIs to trigger array start
        `namespaceSeparator` - namespace prefix separator
        `namespaceDeclarations` - whether to write namespace declarations
        `repairNamespaces` - repair Namespaces

    - #### JsonXMLStreamWriter

          public JsonXMLStreamWriter(JsonStreamTarget target,
                                     boolean repairNamespaces,
                                     boolean multiplePI,
                                     char namespaceSeparator,
                                     boolean namespaceDeclarations,
                                     boolean xmlNilReadWriteEnabled,
                                     boolean xmlWriteNullForEmptyElement)

      Create writer instance.
      Parameters:
        `target` - stream target
        `multiplePI` - whether to consume `<xml-multiple?>` PIs to trigger array start
        `namespaceSeparator` - namespace prefix separator
        `namespaceDeclarations` - whether to write namespace declarations
        `xmlNilReadWriteEnabled` - Supports reading and writing of XML Nil elements as defined by http://www.w3.org/TR/xmlschema-1/#xsi_nil when the XML/JSON inputs contains nil/null values.
        `repairNamespaces` - repair Namespaces
        `xmlWriteNullForEmptyElement` - write null or "" for XML empty elements without nil attribute

  - ### Method Detail

    - #### writeStartElementTag

          protected org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo writeStartElementTag(java.lang.String prefix,
                                                                                                                   java.lang.String localName,
                                                                                                                   java.lang.String namespaceURI)
                                                                                                            throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Write open start element tag. The returned scope info is stored in the new scope and will be available via `getScope().getInfo()`.
      Specified by:
        `writeStartElementTag` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Parameters:
        `prefix` - element prefix (may be `XMLConstants.DEFAULT_NS_PREFIX`)
        `localName` - local name
        `namespaceURI` - namespace URI
      Returns:
        new scope info
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeStartElementTagEnd

          protected void writeStartElementTagEnd()
                                          throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Write close start element tag.
      Specified by:
        `writeStartElementTagEnd` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeEndElementTag

          protected void writeEndElementTag()
                                     throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Write end element tag.
      Specified by:
        `writeEndElementTag` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeAttribute

          public void writeAttribute(java.lang.String prefix,
                                     java.lang.String namespaceURI,
                                     java.lang.String localName,
                                     java.lang.String value)
                              throws javax.xml.stream.XMLStreamException

      Specified by:
        `writeAttribute` in interface `javax.xml.stream.XMLStreamWriter`
      Overrides:
        `writeAttribute` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeNamespace

          public void writeNamespace(java.lang.String prefix,
                                     java.lang.String namespaceURI)
                              throws javax.xml.stream.XMLStreamException

      Specified by:
        `writeNamespace` in interface `javax.xml.stream.XMLStreamWriter`
      Overrides:
        `writeNamespace` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeAttr

          protected void writeAttr(java.lang.String prefix,
                                   java.lang.String localName,
                                   java.lang.String namespaceURI,
                                   java.lang.String value)
                            throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Write attribute.
      Specified by:
        `writeAttr` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Parameters:
        `prefix` - attribute prefix (may be `XMLConstants.DEFAULT_NS_PREFIX`)
        `localName` - local name
        `namespaceURI` - namespace URI
        `value` - attribute value
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeNsDecl

          protected void writeNsDecl(java.lang.String prefix,
                                     java.lang.String namespaceURI)
                              throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Write namespace declaration.
      Specified by:
        `writeNsDecl` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Parameters:
        `prefix` - namespace prefix
        `namespaceURI` - namespace URI
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeData

          protected void writeData(java.lang.Object data,
                                   int type)
                            throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Write characters/comment/dtd/entity data.
      Specified by:
        `writeData` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Parameters:
        `data` - text/data
        `type` - one of `CHARACTERS, COMMENT, CDATA, DTD, ENTITY_REFERENCE, SPACE`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeStartDocument

          public void writeStartDocument(java.lang.String encoding,
                                         java.lang.String version)
                                  throws javax.xml.stream.XMLStreamException

      Specified by:
        `writeStartDocument` in interface `javax.xml.stream.XMLStreamWriter`
      Overrides:
        `writeStartDocument` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeEndDocument

          public void writeEndDocument()
                                throws javax.xml.stream.XMLStreamException

      Specified by:
        `writeEndDocument` in interface `javax.xml.stream.XMLStreamWriter`
      Overrides:
        `writeEndDocument` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeStartArray

          public void writeStartArray(java.lang.String fieldName)
                               throws javax.xml.stream.XMLStreamException

      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeEndArray

          public void writeEndArray()
                             throws javax.xml.stream.XMLStreamException

      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### close

          public void close()
                     throws javax.xml.stream.XMLStreamException

      Specified by:
        `close` in interface `javax.xml.stream.XMLStreamWriter`
      Overrides:
        `close` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### flush

          public void flush()
                     throws javax.xml.stream.XMLStreamException

      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writePI

          protected void writePI(java.lang.String target,
                                 java.lang.String data)
                          throws javax.xml.stream.XMLStreamException

      Description copied from class: `AbstractXMLStreamWriter`
      Read processing instruction.
      Specified by:
        `writePI` in class `AbstractXMLStreamWriter<org.apache.synapse.commons.staxon.core.json.JsonXMLStreamWriter.ScopeInfo>`
      Parameters:
        `target` - PI target
        `data` - PI data (may be `null`)
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeNumber

          public void writeNumber(java.lang.Number value)
                           throws javax.xml.stream.XMLStreamException

      Write number value.
      Parameters:
        `value` -
      Throws:
        `javax.xml.stream.XMLStreamException`

    - #### writeBoolean

          public void writeBoolean(java.lang.Boolean value)
                            throws javax.xml.stream.XMLStreamException

      Write boolean value.
      Parameters:
        `value` -
      Throws:
        `javax.xml.stream.XMLStreamException`

Copyright © 2005-2022 Apache Software Foundation. All Rights Reserved.