org.apache.synapse.commons.json
Class JsonUtil

java.lang.Object
  extended by org.apache.synapse.commons.json.JsonUtil

public final class JsonUtil
extends Object


Field Summary
static String PRESERVE_JSON_STREAM
          If this property is set to true the input stream of the JSON payload will be reset after writing to the output stream within the #writeAsJson method.
 
Constructor Summary
JsonUtil()
           
 
Method Summary
static boolean cloneJsonPayload(org.apache.axis2.context.MessageContext sourceMc, org.apache.axis2.context.MessageContext targetMc)
          Clones the JSON stream payload contained in the source message context, if any, to the target message context.
static InputStream getJsonPayload(org.apache.axis2.context.MessageContext messageContext)
          Returns the READ-ONLY input stream of the JSON payload contained in the provided message context.
static javax.xml.stream.XMLStreamReader getReader(InputStream jsonStream)
          This method is useful when you need to get an XML reader directly for the input JSON stream
without adding any additional object wrapper elements such as 'jsonObject' and 'jsonArray'.
static javax.xml.stream.XMLStreamReader getReader(InputStream jsonStream, boolean pIs)
          Returns an XMLStreamReader for a JSON input stream
static boolean hasAJsonPayload(org.apache.axis2.context.MessageContext messageContext)
          Returns true if the payload stored in the provided message context is used as a JSON streaming payload.
static boolean hasAJsonPayload(org.apache.axiom.om.OMElement element)
          Returns whether the provided XML element is an element that stores a sourced JSON payload.
static boolean isAJsonPayloadElement(org.apache.axiom.om.OMElement element)
          Returns true if the element passed in as the parameter is an element that contains a JSON stream.
static byte[] jsonPayloadToByteArray(org.apache.axis2.context.MessageContext messageContext)
          Returns the JSON payload contained in the provided message context as a byte array.
static String jsonPayloadToString(org.apache.axis2.context.MessageContext messageContext)
          Returns the JSON payload contained in the provided message context as a String.
static Reader newJavaScriptSourceReader(org.apache.axis2.context.MessageContext messageContext)
          Returns a reader that can read from the JSON payload contained in the provided message context as a JavaScript source.
The reader returns the '(' character at the beginning of the stream and marks the end with the ')' character.
The reader returned by this method can be directly used with the JavaScript javax.script.ScriptEngine#eval(java.io.Reader) method.
static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, byte[] json, int offset, int length, boolean removeChildren, boolean addAsNewFirstChild)
          Builds and returns a new JSON payload for a message context with a byte array containing JSON.
static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, InputStream inputStream, boolean removeChildren, boolean addAsNewFirstChild)
          Builds and returns a new JSON payload for a message context with a stream of JSON content.
static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, String jsonString, boolean removeChildren, boolean addAsNewFirstChild)
          Builds and returns a new JSON payload for a message context with a JSON string.
static Reader newJsonPayloadReader(org.apache.axis2.context.MessageContext messageContext)
          Returns a new instance of a reader that can read from the JSON payload contained in the provided message context.
static boolean removeJsonPayload(org.apache.axis2.context.MessageContext messageContext)
          Removes the existing JSON payload of a message context if any.
This method can only remove a JSON payload that has been set with newJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean) and its variants.
static void setContentType(org.apache.axis2.context.MessageContext messageContext)
          Sets JSON media type 'application/json' as the message type to the current message context.
static InputStream toJsonStream(org.apache.axiom.om.OMElement element)
          Returns an input stream that contains the JSON representation of an XML element.
static StringBuilder toJsonString(org.apache.axiom.om.OMElement element)
          Converts an XML element to its JSON representation and returns it as a String.
static InputStream toReadOnlyStream(InputStream inputStream)
          Returns a read only, re-readable input stream for an input stream.
static org.apache.axiom.om.OMElement toXml(InputStream jsonStream, boolean pIs)
          Converts a JSON input stream to its XML representation.
static void transformElement(org.apache.axiom.om.OMElement element, boolean processAttrbs)
          Removes XML namespace declarations, and namespace prefixes from an XML element.
static void writeAsJson(org.apache.axis2.context.MessageContext messageContext, OutputStream out)
          Converts the XML payload of a message context into its JSON representation and writes it to an output stream.
If no XML payload is found, the existing JSON payload will be copied to the output stream.
Note that this method removes all existing namespace declarations and namespace prefixes of the payload that is
present in the provided message context.
static void writeAsJson(org.apache.axiom.om.OMElement element, OutputStream outputStream)
          Converts an XML element to its JSON representation and writes it to an output stream.
Note that this method removes all existing namespace declarations and namespace prefixes of the provided XML element
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRESERVE_JSON_STREAM

public static final String PRESERVE_JSON_STREAM
If this property is set to true the input stream of the JSON payload will be reset after writing to the output stream within the #writeAsJson method.

See Also:
Constant Field Values
Constructor Detail

JsonUtil

public JsonUtil()
Method Detail

writeAsJson

public static void writeAsJson(org.apache.axis2.context.MessageContext messageContext,
                               OutputStream out)
                        throws org.apache.axis2.AxisFault
Converts the XML payload of a message context into its JSON representation and writes it to an output stream.
If no XML payload is found, the existing JSON payload will be copied to the output stream.
Note that this method removes all existing namespace declarations and namespace prefixes of the payload that is
present in the provided message context.

Parameters:
messageContext - Axis2 Message context that holds the JSON/XML payload.
out - Output stream to which the payload(JSON) must be written.
Throws:
org.apache.axis2.AxisFault

toXml

public static org.apache.axiom.om.OMElement toXml(InputStream jsonStream,
                                                  boolean pIs)
                                           throws org.apache.axis2.AxisFault
Converts a JSON input stream to its XML representation.

Parameters:
jsonStream - JSON input stream
pIs - Whether or not to add XML processing instructions to the output XML.
This property is useful when converting JSON payloads with array objects.
Returns:
OMElement that is the XML representation of the input JSON data.
Throws:
org.apache.axis2.AxisFault

getReader

public static javax.xml.stream.XMLStreamReader getReader(InputStream jsonStream,
                                                         boolean pIs)
                                                  throws javax.xml.stream.XMLStreamException
Returns an XMLStreamReader for a JSON input stream

Parameters:
jsonStream - InputStream of JSON
pIs - Whether to add XML PIs to the XML output. This is used as an instruction to the returned XML Stream Reader.
Returns:
An XMLStreamReader
Throws:
javax.xml.stream.XMLStreamException

getReader

public static javax.xml.stream.XMLStreamReader getReader(InputStream jsonStream)
                                                  throws javax.xml.stream.XMLStreamException
This method is useful when you need to get an XML reader directly for the input JSON stream
without adding any additional object wrapper elements such as 'jsonObject' and 'jsonArray'.

Parameters:
jsonStream - InputStream of JSON
Returns:
An XMLStreamReader
Throws:
javax.xml.stream.XMLStreamException

writeAsJson

public static void writeAsJson(org.apache.axiom.om.OMElement element,
                               OutputStream outputStream)
                        throws org.apache.axis2.AxisFault
Converts an XML element to its JSON representation and writes it to an output stream.
Note that this method removes all existing namespace declarations and namespace prefixes of the provided XML element

Parameters:
element - XML element of which JSON representation is expected.
outputStream - Output Stream to write the JSON representation.
At the end of a successful conversion, its flush method will be called.
Throws:
org.apache.axis2.AxisFault

toJsonString

public static StringBuilder toJsonString(org.apache.axiom.om.OMElement element)
                                  throws org.apache.axis2.AxisFault
Converts an XML element to its JSON representation and returns it as a String.

Parameters:
element - OMElement to be converted to JSON.
Returns:
A String builder instance that contains the converted JSON string.
Throws:
org.apache.axis2.AxisFault

transformElement

public static void transformElement(org.apache.axiom.om.OMElement element,
                                    boolean processAttrbs)
Removes XML namespace declarations, and namespace prefixes from an XML element.

Parameters:
element - Source XML element
processAttrbs - Whether to remove the namespaces from attributes as well

newJsonPayload

public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
                                                           InputStream inputStream,
                                                           boolean removeChildren,
                                                           boolean addAsNewFirstChild)
Builds and returns a new JSON payload for a message context with a stream of JSON content.
This is the recommended way to build a JSON payload into an Axis2 message context.
A JSON payload built into a message context with this method can only be removed by calling removeJsonPayload(org.apache.axis2.context.MessageContext) method.

Parameters:
messageContext - Axis2 Message context to which the new JSON payload must be saved (if instructed with addAsNewFirstChild).
inputStream - JSON content as an input stream.
removeChildren - Whether to remove existing child nodes of the existing payload of the message context
addAsNewFirstChild - Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.
Setting this argument to true will have no effect if the value of the argument removeChildren is already false.
Returns:
Payload object that stores the input JSON content as a Sourced object (See OMSourcedElement) that can build the XML tree for contained JSON payload on demand.

newJsonPayload

public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
                                                           String jsonString,
                                                           boolean removeChildren,
                                                           boolean addAsNewFirstChild)
Builds and returns a new JSON payload for a message context with a JSON string.

Parameters:
messageContext - Axis2 Message context to which the new JSON payload must be saved (if instructed with addAsNewFirstChild).
jsonString - JSON content as a String.
removeChildren - Whether to remove existing child nodes of the existing payload of the message context
addAsNewFirstChild - Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.
Setting this argument to true will have no effect if the value of the argument removeChildren is already false.
Returns:
Payload object that stores the input JSON content as a Sourced object (See OMSourcedElement) that facilitates on demand building of the XML tree.
See Also:
newJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)

newJsonPayload

public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
                                                           byte[] json,
                                                           int offset,
                                                           int length,
                                                           boolean removeChildren,
                                                           boolean addAsNewFirstChild)
Builds and returns a new JSON payload for a message context with a byte array containing JSON.

Parameters:
messageContext - Axis2 Message context to which the new JSON payload must be saved (if instructed with addAsNewFirstChild).
json - JSON content as a byte array.
offset - starting position of the JSON content in the provided array
length - how many bytes to read starting from the offset provided.
removeChildren - Whether to remove existing child nodes of the existing payload of the message context
addAsNewFirstChild - Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.
Setting this argument to true will have no effect if the value of the argument removeChildren is already false.
Returns:
Payload object that stores the input JSON content as a Sourced object (See OMSourcedElement) that facilitates on demand building of the XML tree.
See Also:
newJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)

removeJsonPayload

public static boolean removeJsonPayload(org.apache.axis2.context.MessageContext messageContext)
Removes the existing JSON payload of a message context if any.
This method can only remove a JSON payload that has been set with newJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean) and its variants.

Parameters:
messageContext - Axis2 Message context from which the JSON stream must be removed.
Returns:
true if the operation is successful.

getJsonPayload

public static InputStream getJsonPayload(org.apache.axis2.context.MessageContext messageContext)
Returns the READ-ONLY input stream of the JSON payload contained in the provided message context.

Parameters:
messageContext - Axis2 Message context
Returns:
InputStream of JSON payload contained in the message context. Null otherwise.
It is possible to read from this stream right away. This InputStream cannot be closed, marked, or skipped.
If close() is invoked on this input stream, it will be reset to the beginning.

newJsonPayloadReader

public static Reader newJsonPayloadReader(org.apache.axis2.context.MessageContext messageContext)
Returns a new instance of a reader that can read from the JSON payload contained in the provided message context.

Parameters:
messageContext - Axis2 Message context
Returns:
Reader if a JSON payload is found in the message context. null otherwise.

jsonPayloadToByteArray

public static byte[] jsonPayloadToByteArray(org.apache.axis2.context.MessageContext messageContext)
Returns the JSON payload contained in the provided message context as a byte array.

Parameters:
messageContext - Axis2 Message context
Returns:
byte array containing the JSON payload. Empty array if no JSON payload found or invalid message context is passed in.

jsonPayloadToString

public static String jsonPayloadToString(org.apache.axis2.context.MessageContext messageContext)
Returns the JSON payload contained in the provided message context as a String.

Parameters:
messageContext - Axis2 Message context
Returns:
java.lang.String representation of the JSON payload. Returns "{}" if no JSON payload found or invalid message context is passed in.

hasAJsonPayload

public static boolean hasAJsonPayload(org.apache.axiom.om.OMElement element)
Returns whether the provided XML element is an element that stores a sourced JSON payload.

Parameters:
element - XML element
Returns:
true if the element is a sourced JSON object (ie. an OMSourcedElement instance containing a JSON stream).

isAJsonPayloadElement

public static boolean isAJsonPayloadElement(org.apache.axiom.om.OMElement element)
Returns true if the element passed in as the parameter is an element that contains a JSON stream.

Parameters:
element - XML element
Returns:
true if the element has the local name of a sourced (ie. an OMSourcedElement) JSON object.

hasAJsonPayload

public static boolean hasAJsonPayload(org.apache.axis2.context.MessageContext messageContext)
Returns true if the payload stored in the provided message context is used as a JSON streaming payload.

Parameters:
messageContext - Axis2 Message context
Returns:
true if the message context contains a Streaming JSON payload.

cloneJsonPayload

public static boolean cloneJsonPayload(org.apache.axis2.context.MessageContext sourceMc,
                                       org.apache.axis2.context.MessageContext targetMc)
Clones the JSON stream payload contained in the source message context, if any, to the target message context.

Parameters:
sourceMc - Where to get the payload
targetMc - Where to clone and copy the payload
Returns:
true if the cloning was successful.

setContentType

public static void setContentType(org.apache.axis2.context.MessageContext messageContext)
Sets JSON media type 'application/json' as the message type to the current message context.

Parameters:
messageContext - Axis2 Message context

toReadOnlyStream

public static InputStream toReadOnlyStream(InputStream inputStream)
Returns a read only, re-readable input stream for an input stream.
The returned input stream cannot be closed, marked, or skipped, but it can be reset to the beginning of the stream.

Parameters:
inputStream - Input stream to be wrapped
Returns:
InputStream

toJsonStream

public static InputStream toJsonStream(org.apache.axiom.om.OMElement element)
Returns an input stream that contains the JSON representation of an XML element.

Parameters:
element - XML element of which JSON representation is expected.
Returns:
InputStream

newJavaScriptSourceReader

public static Reader newJavaScriptSourceReader(org.apache.axis2.context.MessageContext messageContext)
Returns a reader that can read from the JSON payload contained in the provided message context as a JavaScript source.
The reader returns the '(' character at the beginning of the stream and marks the end with the ')' character.
The reader returned by this method can be directly used with the JavaScript javax.script.ScriptEngine#eval(java.io.Reader) method.

Parameters:
messageContext - Axis2 Message context
Returns:
InputStreamReader


Copyright © 2005–2015 Apache Software Foundation. All rights reserved.