org.milyn.json
Class JSONReader

java.lang.Object
  extended by org.milyn.json.JSONReader
All Implemented Interfaces:
org.milyn.delivery.ContentHandler, org.milyn.xml.SmooksXMLReader, XMLReader

public class JSONReader
extends Object
implements org.milyn.xml.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

Field Summary
static String CONFIG_PARAM_KEY_MAP
           
static String DEFAULT_NULL_VALUE_REPLACEMENT
           
static String XML_ARRAY_ELEMENT_NAME
           
static String XML_ROOT
           
 
Constructor Summary
JSONReader()
           
 
Method Summary
 String getArrayElementName()
           
 ContentHandler getContentHandler()
           
 DTDHandler getDTDHandler()
           
 Charset getEncoding()
           
 EntityResolver getEntityResolver()
           
 ErrorHandler getErrorHandler()
           
 boolean getFeature(String name)
           
 String getIllegalElementNameCharReplacement()
           
 HashMap<String,String> getKeyMap()
           
 String getKeyPrefixOnNumeric()
           
 String getKeyWhitspaceReplacement()
           
 String getNullValueReplacement()
           
 Object getProperty(String name)
           
 String getRootName()
           
 void initialize()
           
 void parse(InputSource csvInputSource)
           
 void parse(String systemId)
          The following methods are currently unimplemnted...
 void setArrayElementName(String arrayElementName)
           
 void setContentHandler(ContentHandler contentHandler)
           
 void setDTDHandler(DTDHandler arg0)
           
 void setEncoding(Charset encoding)
           
 void setEntityResolver(EntityResolver arg0)
           
 void setErrorHandler(ErrorHandler arg0)
           
 void setExecutionContext(org.milyn.container.ExecutionContext request)
           
 void setFeature(String name, boolean value)
           
 void setIllegalElementNameCharReplacement(String illegalElementNameCharReplacement)
           
 void setIndent(boolean indent)
           
 void setKeyMap(HashMap<String,String> keyMap)
           
 void setKeyPrefixOnNumeric(String keyPrefixOnNumeric)
           
 void setKeyWhitspaceReplacement(String keyWhitspaceReplacement)
           
 void setNullValueReplacement(String nullValueReplacement)
           
 void setProperty(String name, Object value)
           
 void setRootName(String rootName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONFIG_PARAM_KEY_MAP

public static final String CONFIG_PARAM_KEY_MAP
See Also:
Constant Field Values

XML_ROOT

public static final String XML_ROOT
See Also:
Constant Field Values

XML_ARRAY_ELEMENT_NAME

public static final String XML_ARRAY_ELEMENT_NAME
See Also:
Constant Field Values

DEFAULT_NULL_VALUE_REPLACEMENT

public static final String DEFAULT_NULL_VALUE_REPLACEMENT
See Also:
Constant Field Values
Constructor Detail

JSONReader

public JSONReader()
Method Detail

initialize

@Initialize
public void initialize()

setExecutionContext

public void setExecutionContext(org.milyn.container.ExecutionContext request)
Specified by:
setExecutionContext in interface org.milyn.xml.SmooksXMLReader

parse

public void parse(InputSource csvInputSource)
           throws IOException,
                  SAXException
Specified by:
parse in interface XMLReader
Throws:
IOException
SAXException

setContentHandler

public void setContentHandler(ContentHandler contentHandler)
Specified by:
setContentHandler in interface XMLReader

getContentHandler

public ContentHandler getContentHandler()
Specified by:
getContentHandler in interface XMLReader

getKeyMap

public HashMap<String,String> getKeyMap()
Returns:
the keyMap

setKeyMap

public void setKeyMap(HashMap<String,String> keyMap)
Parameters:
keyMap - the keyMap to set

getRootName

public String getRootName()
Returns:
the rootName

setRootName

public void setRootName(String rootName)
Parameters:
rootName - the rootName to set

getArrayElementName

public String getArrayElementName()
Returns:
the arrayElementName

setArrayElementName

public void setArrayElementName(String arrayElementName)
Parameters:
arrayElementName - the arrayElementName to set

getKeyWhitspaceReplacement

public String getKeyWhitspaceReplacement()
Returns:
the keyWhitspaceReplacement

setKeyWhitspaceReplacement

public void setKeyWhitspaceReplacement(String keyWhitspaceReplacement)
Parameters:
keyWhitspaceReplacement - the keyWhitspaceReplacement to set

getKeyPrefixOnNumeric

public String getKeyPrefixOnNumeric()
Returns:
the keyPrefixOnNumeric

setKeyPrefixOnNumeric

public void setKeyPrefixOnNumeric(String keyPrefixOnNumeric)
Parameters:
keyPrefixOnNumeric - the keyPrefixOnNumeric to set

getIllegalElementNameCharReplacement

public String getIllegalElementNameCharReplacement()
Returns:
the illegalElementNameCharReplacement

setIllegalElementNameCharReplacement

public void setIllegalElementNameCharReplacement(String illegalElementNameCharReplacement)
Parameters:
illegalElementNameCharReplacement - the illegalElementNameCharReplacement to set

getNullValueReplacement

public String getNullValueReplacement()
Returns:
the nullValueReplacement

setNullValueReplacement

public void setNullValueReplacement(String nullValueReplacement)
Parameters:
nullValueReplacement - the nullValueReplacement to set

getEncoding

public Charset getEncoding()
Returns:
the encoding

setEncoding

public void setEncoding(Charset encoding)
Parameters:
encoding - the encoding to set

setIndent

public void setIndent(boolean indent)

parse

public void parse(String systemId)
The following methods are currently unimplemnted...

Specified by:
parse in interface XMLReader

getFeature

public boolean getFeature(String name)
Specified by:
getFeature in interface XMLReader

setFeature

public void setFeature(String name,
                       boolean value)
Specified by:
setFeature in interface XMLReader

getDTDHandler

public DTDHandler getDTDHandler()
Specified by:
getDTDHandler in interface XMLReader

setDTDHandler

public void setDTDHandler(DTDHandler arg0)
Specified by:
setDTDHandler in interface XMLReader

getEntityResolver

public EntityResolver getEntityResolver()
Specified by:
getEntityResolver in interface XMLReader

setEntityResolver

public void setEntityResolver(EntityResolver arg0)
Specified by:
setEntityResolver in interface XMLReader

getErrorHandler

public ErrorHandler getErrorHandler()
Specified by:
getErrorHandler in interface XMLReader

setErrorHandler

public void setErrorHandler(ErrorHandler arg0)
Specified by:
setErrorHandler in interface XMLReader

getProperty

public Object getProperty(String name)
Specified by:
getProperty in interface XMLReader

setProperty

public void setProperty(String name,
                        Object value)
Specified by:
setProperty in interface XMLReader


Copyright © 2018. All rights reserved.