org.milyn.json
Class JSONReader
java.lang.Object
org.milyn.json.JSONReader
- All Implemented Interfaces:
- ContentHandler, SmooksXMLReader, org.xml.sax.XMLReader
public class JSONReader
- extends java.lang.Object
- implements SmooksXMLReader
JSON to SAX event reader.
This JSON Reader can be plugged into Smooks in order to convert a
JSON based message stream into a stream of SAX events to be consumed by the other
Smooks resources.
Configuration
<resource-config selector="org.xml.sax.driver">
<resource>org.milyn.json.JSONReader</resource>
<!--
(Optional) The element name of the SAX document root. Default of 'json'.
-->
<param name="rootName"><root-name></param>
<!--
(Optional) The element name of a array element. Default of 'element'.
-->
<param name="arrayElementName"><array-element-name></param>
<!--
(Optional) The replacement string for JSON NULL values. Default is an empty string.
-->
<param name="nullValueReplacement"><null-value-replacement></param>
<!--
(Optional) The replacement character for whitespaces in a json map key. By default this not defined, so that the reader doesn't search for whitespaces.
-->
<param name="keyWhitspaceReplacement"><key-whitspace-replacement></param>
<!--
(Optional) The prefix character to add if the JSON node name starts with a number. By default this is not defined, so that the reader doesn't search for element names that start with a number.
-->
<param name="keyPrefixOnNumeric"><key-prefix-on-numeric></param>
<!--
(Optional) If illegal characters are encountered in a JSON element name then they are replaced with this value. By default this is not defined, so that the reader doesn't doesn't search for illegal characters.
-->
<param name="illegalElementNameCharReplacement"><illegal-element-name-char-replacement></param>
<!--
(Optional) Defines a map of keys and there replacement. The from key will be replaced with the to key or the contents of the element.
-->
<param name="keyMap">
<key from="fromKey" to="toKey" />
<key from="fromKey"><to></key>
</param>
<!--
(Optional) The encoding of the input stream. Default of 'UTF-8'
-->
<param name="encoding"><encoding></param>
</resource-config>
Example Usage
So the following configuration could be used to parse a JSON stream into
a stream of SAX events:
<resource-config selector="org.xml.sax.driver">
<resource>org.milyn.json.JSONReader</resource>
</smooks-resource>
The "Acme-Order-List" input JSON message:
[
{
"name" : "Maurice Zeijen",
"address" : "Netherlands",
"item" : "V1234",
"quantity" : 3
},
{
"name" : "Joe Bloggs",
"address" : "England",
"item" : "D9123",
"quantity" : 7
},
]
Within Smooks, the stream of SAX events generated by the "Acme-Order-List" message (and this reader) will generate
a DOM equivalent to the following:
<json>
<element>
<name>Maurice Zeijen</name>
<address>Netherlands</address>
<item>V1234</item>
<quantity>3</quantity>
<element>
<element>
<name>Joe Bloggs</name>
<address>England</address>
<item>D9123</item>
<quantity>7</quantity>
<element>
</json>
- Author:
- maurice@zeijen.net
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
CONFIG_PARAM_KEY_MAP
public static final java.lang.String CONFIG_PARAM_KEY_MAP
- See Also:
- Constant Field Values
XML_ROOT
public static final java.lang.String XML_ROOT
- See Also:
- Constant Field Values
XML_ARRAY_ELEMENT_NAME
public static final java.lang.String XML_ARRAY_ELEMENT_NAME
- See Also:
- Constant Field Values
DEFAULT_NULL_VALUE_REPLACEMENT
public static final java.lang.String DEFAULT_NULL_VALUE_REPLACEMENT
- See Also:
- Constant Field Values
JSONReader
public JSONReader()
initialize
@Initialize
public void initialize()
setExecutionContext
public void setExecutionContext(ExecutionContext request)
- Specified by:
setExecutionContext in interface SmooksXMLReader
parse
public void parse(org.xml.sax.InputSource csvInputSource)
throws java.io.IOException,
org.xml.sax.SAXException
- Specified by:
parse in interface org.xml.sax.XMLReader
- Throws:
java.io.IOException
org.xml.sax.SAXException
setContentHandler
public void setContentHandler(org.xml.sax.ContentHandler contentHandler)
- Specified by:
setContentHandler in interface org.xml.sax.XMLReader
getContentHandler
public org.xml.sax.ContentHandler getContentHandler()
- Specified by:
getContentHandler in interface org.xml.sax.XMLReader
getKeyMap
public java.util.HashMap<java.lang.String,java.lang.String> getKeyMap()
- Returns:
- the keyMap
setKeyMap
public void setKeyMap(java.util.HashMap<java.lang.String,java.lang.String> keyMap)
- Parameters:
keyMap - the keyMap to set
getRootName
public java.lang.String getRootName()
- Returns:
- the rootName
setRootName
public void setRootName(java.lang.String rootName)
- Parameters:
rootName - the rootName to set
getArrayElementName
public java.lang.String getArrayElementName()
- Returns:
- the arrayElementName
setArrayElementName
public void setArrayElementName(java.lang.String arrayElementName)
- Parameters:
arrayElementName - the arrayElementName to set
getKeyWhitspaceReplacement
public java.lang.String getKeyWhitspaceReplacement()
- Returns:
- the keyWhitspaceReplacement
setKeyWhitspaceReplacement
public void setKeyWhitspaceReplacement(java.lang.String keyWhitspaceReplacement)
- Parameters:
keyWhitspaceReplacement - the keyWhitspaceReplacement to set
getKeyPrefixOnNumeric
public java.lang.String getKeyPrefixOnNumeric()
- Returns:
- the keyPrefixOnNumeric
setKeyPrefixOnNumeric
public void setKeyPrefixOnNumeric(java.lang.String keyPrefixOnNumeric)
- Parameters:
keyPrefixOnNumeric - the keyPrefixOnNumeric to set
getIllegalElementNameCharReplacement
public java.lang.String getIllegalElementNameCharReplacement()
- Returns:
- the illegalElementNameCharReplacement
setIllegalElementNameCharReplacement
public void setIllegalElementNameCharReplacement(java.lang.String illegalElementNameCharReplacement)
- Parameters:
illegalElementNameCharReplacement - the illegalElementNameCharReplacement to set
getNullValueReplacement
public java.lang.String getNullValueReplacement()
- Returns:
- the nullValueReplacement
setNullValueReplacement
public void setNullValueReplacement(java.lang.String nullValueReplacement)
- Parameters:
nullValueReplacement - the nullValueReplacement to set
getEncoding
public java.nio.charset.Charset getEncoding()
- Returns:
- the encoding
setEncoding
public void setEncoding(java.nio.charset.Charset encoding)
- Parameters:
encoding - the encoding to set
setIndent
public void setIndent(boolean indent)
parse
public void parse(java.lang.String systemId)
throws java.io.IOException,
org.xml.sax.SAXException
- The following methods are currently unimplemnted...
- Specified by:
parse in interface org.xml.sax.XMLReader
- Throws:
java.io.IOException
org.xml.sax.SAXException
getFeature
public boolean getFeature(java.lang.String name)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
- Specified by:
getFeature in interface org.xml.sax.XMLReader
- Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
setFeature
public void setFeature(java.lang.String name,
boolean value)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
- Specified by:
setFeature in interface org.xml.sax.XMLReader
- Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
getDTDHandler
public org.xml.sax.DTDHandler getDTDHandler()
- Specified by:
getDTDHandler in interface org.xml.sax.XMLReader
setDTDHandler
public void setDTDHandler(org.xml.sax.DTDHandler arg0)
- Specified by:
setDTDHandler in interface org.xml.sax.XMLReader
getEntityResolver
public org.xml.sax.EntityResolver getEntityResolver()
- Specified by:
getEntityResolver in interface org.xml.sax.XMLReader
setEntityResolver
public void setEntityResolver(org.xml.sax.EntityResolver arg0)
- Specified by:
setEntityResolver in interface org.xml.sax.XMLReader
getErrorHandler
public org.xml.sax.ErrorHandler getErrorHandler()
- Specified by:
getErrorHandler in interface org.xml.sax.XMLReader
setErrorHandler
public void setErrorHandler(org.xml.sax.ErrorHandler arg0)
- Specified by:
setErrorHandler in interface org.xml.sax.XMLReader
getProperty
public java.lang.Object getProperty(java.lang.String name)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
- Specified by:
getProperty in interface org.xml.sax.XMLReader
- Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
setProperty
public void setProperty(java.lang.String name,
java.lang.Object value)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
- Specified by:
setProperty in interface org.xml.sax.XMLReader
- Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
Copyright © 2011. All Rights Reserved.