public final class JsonUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.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 JsonXMLInputFactory |
generateJSONInputFactory(java.util.Properties props)
Generate factory that is used to create JSON Readers
It uses Configuration that is used to produce XML that has processing instructions in it.
|
static JsonXMLOutputFactory |
generateJSONOutputFactory(java.util.Properties props)
Generate factory that is used to create JSON Writers
It uses configuration that is used to format the JSON output produced by the JSON writer.
|
static JsonXMLOutputFactory |
generateJSONOutputFactoryWithOveride(java.util.Properties props)
Generate factory that is used to create JSON Writers
This is will override the global configration
|
static JsonXMLInputFactory |
generateJsonXMLInputFactory(java.util.Properties props)
Generate factory that is used to create JSON Readers.
|
static java.io.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,
java.io.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,
java.lang.String jsonString,
boolean removeChildren,
boolean addAsNewFirstChild)
Builds and returns a new JSON payload for a message context with a JSON string.
|
static javax.xml.stream.XMLStreamReader |
getReader(java.io.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(java.io.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 java.lang.String |
jsonPayloadToString(org.apache.axis2.context.MessageContext messageContext)
Returns the JSON payload contained in the provided message context as a String.
|
static java.io.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)
Deprecated.
|
static org.apache.axiom.om.OMElement |
newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
java.io.InputStream inputStream,
boolean removeChildren,
boolean addAsNewFirstChild)
Deprecated.
|
static org.apache.axiom.om.OMElement |
newJsonPayload(org.apache.axis2.context.MessageContext messageContext,
java.lang.String jsonString,
boolean removeChildren,
boolean addAsNewFirstChild)
Deprecated.
|
static java.io.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 |
removeJsonStream(org.apache.axis2.context.MessageContext messageContext)
Removes the JSON stream from the the MessageContext
This method is used to remove the outdated JSON stream
|
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 java.io.InputStream |
setJsonStream(org.apache.axis2.context.MessageContext messageContext,
java.io.InputStream inputStream)
Sets the input JSON stream as a property of the MessageContext
|
static java.io.InputStream |
toJsonStream(org.apache.axiom.om.OMElement element)
Returns an input stream that contains the JSON representation of an XML element.
|
static java.lang.StringBuilder |
toJsonString(org.apache.axiom.om.OMElement element) |
static java.lang.StringBuilder |
toJsonString(org.apache.axiom.om.OMElement element,
JsonXMLOutputFactory jsonOutputFactory)
Converts an XML element to its JSON representation and returns it as a String.
|
static java.io.InputStream |
toReadOnlyStream(java.io.InputStream inputStream)
Returns a read only, re-readable input stream for an input stream.
|
static org.apache.axiom.om.OMElement |
toXml(java.io.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 |
transformElement(org.apache.axiom.om.OMElement element,
boolean processAttrbs,
java.util.Map properties,
JsonXMLOutputFactory jsonOutputFactory)
Removes XML namespace declarations, and namespace prefixes from an XML element.
|
static void |
writeAsJson(org.apache.axis2.context.MessageContext messageContext,
java.io.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,
java.io.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 |
static void |
writeAsJson(org.apache.axiom.om.OMElement element,
java.io.OutputStream outputStream,
java.util.Map properties,
JsonXMLOutputFactory jsonOutputFactory)
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 java.lang.String PRESERVE_JSON_STREAM
public static JsonXMLInputFactory generateJsonXMLInputFactory(java.util.Properties props)
props
- Properties that are loadedpublic static JsonXMLInputFactory generateJSONInputFactory(java.util.Properties props)
props
- Properties that are loadedpublic static JsonXMLOutputFactory generateJSONOutputFactory(java.util.Properties props)
props
- Properties that are loadedpublic static JsonXMLOutputFactory generateJSONOutputFactoryWithOveride(java.util.Properties props)
props
- Properties that are loadedpublic static void writeAsJson(org.apache.axis2.context.MessageContext messageContext, java.io.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(java.io.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 javax.xml.stream.XMLStreamReader getReader(java.io.InputStream jsonStream, boolean pIs) throws javax.xml.stream.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.javax.xml.stream.XMLStreamException
public static javax.xml.stream.XMLStreamReader getReader(java.io.InputStream jsonStream) throws javax.xml.stream.XMLStreamException
jsonStream
- InputStream of JSONjavax.xml.stream.XMLStreamException
public static void writeAsJson(org.apache.axiom.om.OMElement element, java.io.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 void writeAsJson(org.apache.axiom.om.OMElement element, java.io.OutputStream outputStream, java.util.Map properties, JsonXMLOutputFactory jsonOutputFactory) throws org.apache.axis2.AxisFault
element
- XML element of which JSON representation is expected.outputStream
- Output Stream to write the JSON representation.properties
- Message context propertiesorg.apache.axis2.AxisFault
public static java.lang.StringBuilder toJsonString(org.apache.axiom.om.OMElement element) throws org.apache.axis2.AxisFault
org.apache.axis2.AxisFault
public static java.lang.StringBuilder toJsonString(org.apache.axiom.om.OMElement element, JsonXMLOutputFactory jsonOutputFactory) 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 wellpublic static void transformElement(org.apache.axiom.om.OMElement element, boolean processAttrbs, java.util.Map properties, JsonXMLOutputFactory jsonOutputFactory)
element
- Source XML elementprocessAttrbs
- Whether to remove the namespaces from attributes as wellproperties
- Message context propertiesjsonOutputFactory
- JsonOutputFactory with all the configurations.
Pass null to use global configuration@Deprecated public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, java.io.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, java.io.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
public static java.io.InputStream setJsonStream(org.apache.axis2.context.MessageContext messageContext, java.io.InputStream inputStream)
messageContext
- the axis2MessageContextinputStream
- the JSON inputStreampublic static void removeJsonStream(org.apache.axis2.context.MessageContext messageContext)
messageContext
- the axis2MessageContext@Deprecated public static org.apache.axiom.om.OMElement newJsonPayload(org.apache.axis2.context.MessageContext messageContext, java.lang.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, java.lang.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 java.io.InputStream getJsonPayload(org.apache.axis2.context.MessageContext messageContext)
messageContext
- Axis2 Message contextInputStream
of JSON payload contained in the message context. Null otherwise.public static java.io.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 java.lang.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 java.io.InputStream toReadOnlyStream(java.io.InputStream inputStream)
inputStream
- Input stream to be wrappedInputStream
public static java.io.InputStream toJsonStream(org.apache.axiom.om.OMElement element)
element
- XML element of which JSON representation is expected.InputStream
public static java.io.Reader newJavaScriptSourceReader(org.apache.axis2.context.MessageContext messageContext)
javax.script.ScriptEngine#eval(java.io.Reader)
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-2020 Apache Software Foundation. All Rights Reserved.