public final class JsonUtil extends Object
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
JsonUtil() |
Modifier and Type | Method and Description |
---|---|
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 org.apache.axiom.om.OMElement |
getNewJsonPayload(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 |
getNewJsonPayload(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 |
getNewJsonPayload(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 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 XMLStreamReader |
getReader(InputStream jsonStream,
boolean pIs)
Returns an XMLStreamReader for a JSON input stream
|
static boolean |
hasAJsonArray(org.apache.axis2.context.MessageContext messageContext)
Returns true if the message context contains a JSON payload that is a JSON Array.
|
static boolean |
hasAJsonObject(org.apache.axis2.context.MessageContext messageContext)
Returns true if the message context contains a JSON payload that is a JSON Object.
|
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 boolean |
isPiEnabled()
Returns the configured value of the parameter (synapse.json.to.xml.process.instruction.enabled).
|
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 ScriptEngine method. |
static org.apache.axiom.om.OMElement |
newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
byte[] json,
int offset,
int length,
boolean removeChildren,
boolean addAsNewFirstChild)
Deprecated.
|
static org.apache.axiom.om.OMElement |
newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
InputStream inputStream,
boolean removeChildren,
boolean addAsNewFirstChild)
Deprecated.
|
static org.apache.axiom.om.OMElement |
newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
String jsonString,
boolean removeChildren,
boolean addAsNewFirstChild)
Deprecated.
|
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 getNewJsonPayload(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 |
public static final String PRESERVE_JSON_STREAM
public static void writeAsJson(org.apache.axis2.context.MessageContext messageContext, OutputStream out) throws org.apache.axis2.AxisFault
messageContext
- Axis2 Message context that holds the JSON/XML payload.out
- Output stream to which the payload(JSON) must be written.org.apache.axis2.AxisFault
public static org.apache.axiom.om.OMElement toXml(InputStream jsonStream, boolean pIs) throws org.apache.axis2.AxisFault
jsonStream
- JSON input streampIs
- Whether or not to add XML processing instructions to the output XML.org.apache.axis2.AxisFault
public static XMLStreamReader getReader(InputStream jsonStream, boolean pIs) throws XMLStreamException
jsonStream
- InputStream of JSONpIs
- Whether to add XML PIs to the XML output. This is used as an instruction to the returned XML Stream Reader.XMLStreamException
public static XMLStreamReader getReader(InputStream jsonStream) throws XMLStreamException
jsonStream
- InputStream of JSONXMLStreamException
public static void writeAsJson(org.apache.axiom.om.OMElement element, OutputStream outputStream) throws org.apache.axis2.AxisFault
element
- XML element of which JSON representation is expected.outputStream
- Output Stream to write the JSON representation.org.apache.axis2.AxisFault
public static StringBuilder toJsonString(org.apache.axiom.om.OMElement element) throws org.apache.axis2.AxisFault
element
- OMElement to be converted to JSON.org.apache.axis2.AxisFault
public static void transformElement(org.apache.axiom.om.OMElement element, boolean processAttrbs)
element
- Source XML elementprocessAttrbs
- Whether to remove the namespaces from attributes as well@Deprecated public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, InputStream inputStream, boolean removeChildren, boolean addAsNewFirstChild)
removeJsonPayload(org.apache.axis2.context.MessageContext)
method. This added to avoid code breaks
for previous implementations. For new implementations use getNewJsonPayload method.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 contextaddAsNewFirstChild
- Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.OMSourcedElement
) that can build the XML tree for contained JSON payload on demand.public static org.apache.axiom.om.OMElement getNewJsonPayload(org.apache.axis2.context.MessageContext messageContext, InputStream inputStream, boolean removeChildren, boolean addAsNewFirstChild) throws org.apache.axis2.AxisFault
removeJsonPayload(org.apache.axis2.context.MessageContext)
method.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 contextaddAsNewFirstChild
- Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.OMSourcedElement
) that can build the XML tree for contained JSON payload on demand.org.apache.axis2.AxisFault
@Deprecated public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, String jsonString, boolean removeChildren, boolean addAsNewFirstChild)
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 contextaddAsNewFirstChild
- Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.OMSourcedElement
) that facilitates on demand building of the XML tree.getNewJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)
public static org.apache.axiom.om.OMElement getNewJsonPayload(org.apache.axis2.context.MessageContext messageContext, String jsonString, boolean removeChildren, boolean addAsNewFirstChild) throws org.apache.axis2.AxisFault
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 contextaddAsNewFirstChild
- Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.OMSourcedElement
) that facilitates on demand building of the XML tree.org.apache.axis2.AxisFault
getNewJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)
@Deprecated public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, byte[] json, int offset, int length, boolean removeChildren, boolean addAsNewFirstChild)
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 arraylength
- how many bytes to read starting from the offset provided.removeChildren
- Whether to remove existing child nodes of the existing payload of the message contextaddAsNewFirstChild
- Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.OMSourcedElement
) that facilitates on demand building of the XML tree.getNewJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)
public static org.apache.axiom.om.OMElement getNewJsonPayload(org.apache.axis2.context.MessageContext messageContext, byte[] json, int offset, int length, boolean removeChildren, boolean addAsNewFirstChild) throws org.apache.axis2.AxisFault
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 arraylength
- how many bytes to read starting from the offset provided.removeChildren
- Whether to remove existing child nodes of the existing payload of the message contextaddAsNewFirstChild
- Whether to add the new JSON payload as the first child of this message context *after* removing the existing first child element.OMSourcedElement
) that facilitates on demand building of the XML tree.org.apache.axis2.AxisFault
getNewJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)
public static boolean removeJsonPayload(org.apache.axis2.context.MessageContext messageContext)
getNewJsonPayload(org.apache.axis2.context.MessageContext, java.io.InputStream, boolean, boolean)
and its variants.messageContext
- Axis2 Message context from which the JSON stream must be removed.public static InputStream getJsonPayload(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextInputStream
of JSON payload contained in the message context. Null otherwise.public static Reader newJsonPayloadReader(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextReader
if a JSON payload is found in the message context. null otherwise.public static byte[] jsonPayloadToByteArray(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextpublic static String jsonPayloadToString(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextpublic static boolean hasAJsonPayload(org.apache.axiom.om.OMElement element)
element
- XML elementpublic static boolean isAJsonPayloadElement(org.apache.axiom.om.OMElement element)
element
- XML elementpublic static boolean hasAJsonPayload(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextpublic static boolean cloneJsonPayload(org.apache.axis2.context.MessageContext sourceMc, org.apache.axis2.context.MessageContext targetMc)
sourceMc
- Where to get the payloadtargetMc
- Where to clone and copy the payloadpublic static void setContentType(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextpublic static InputStream toReadOnlyStream(InputStream inputStream)
inputStream
- Input stream to be wrappedInputStream
public static InputStream toJsonStream(org.apache.axiom.om.OMElement element)
element
- XML element of which JSON representation is expected.InputStream
public static Reader newJavaScriptSourceReader(org.apache.axis2.context.MessageContext messageContext)
ScriptEngine
method.messageContext
- Axis2 Message contextInputStreamReader
public static boolean hasAJsonObject(org.apache.axis2.context.MessageContext messageContext)
hasAJsonArray(MessageContext)
messageContext
- request message contextpublic static boolean hasAJsonArray(org.apache.axis2.context.MessageContext messageContext)
hasAJsonObject(MessageContext)
messageContext
- request message contextpublic static boolean isPiEnabled()
Copyright © 2005–2017 Apache Software Foundation. All rights reserved.