XML JSON Data Format (camel-xmljson)
Available as of Camel 2.10
Camel already supports a number of data formats to perform XML and JSON-related conversions, but all of them require a POJO either as an input (for marshalling) or produce a POJO as output (for unmarshalling). This data format provides the capability to convert from XML to JSON and viceversa directly, without stepping through intermediate POJOs.
This data format leverages the Json-lib library to achieve direct conversion. In this context, marshalling means xxx and unmarshalling means xxx.
Options
This data format supports the following options. You can set them via all DSLs. The defaults marked with (*) are determined by json-lib, rather than the code of the data format itself. They are reflected here for convenience so that you don't have to dot back and forth with the json-lib docs.
Name |
Type |
Default |
Description |
---|---|---|---|
|
|
UTF-8 (*) |
Used when unmarshalling (JSON to XML conversion). Sets the encoding for the call to |
|
|
'e' (*) |
Used when unmarshalling (JSON to XML conversion). Specifies the name of the XML elements representing each array element. See json-lib doc. |
|
|
'a' (*) |
Used when unmarshalling (JSON to XML conversion). Specifies the name of the top-level XML element. |
|
|
none (*) |
Used when unmarshalling (JSON to XML conversion). When converting any JSON construct (object, array, null) to XML (unmarshalling), it specifies the name of the top-level element. |
|
|
false (*) |
Used when marshalling (XML to JSON conversion). Determines whether the resulting JSON will start off with a top-most element whose name matches the XML root element. If disabled, XML string |
|
|
false (*) |
Used when unmarshalling (JSON to XML conversion). According to the json-lib docs: "Flag to be tolerant to incomplete namespace prefixes." In most cases, json-lib automatically changes this flag at runtime to match the processing. |
|
|
none |
Used when unmarshalling (JSON to XML conversion). Binds namespace prefixes and URIs to specific JSON elements. |
|
|
false (*) |
Used when marshalling (XML to JSON conversion). Determines whether white spaces between XML elements will be regarded as text values or disregarded. |
|
|
false (*) |
Used when marshalling (XML to JSON conversion). Determines whether leading and trailing white spaces will be omitted from String values. |
|
|
false (*) |
Used when marshalling (XML to JSON conversion). Signals whether namespaces should be ignored. By default they will be added to the JSON output using @xmlns elements. |
|
|
false (*) |
Used when marshalling (XML to JSON conversion). Removes the namespace prefixes from XML qualified elements, so that the resulting JSON string does not contain them. |
|
|
none |
Used when unmarshalling (JSON to XML conversion). With expandable properties, JSON array elements are converted to XML as a sequence of repetitive XML elements with the local name equal to the JSON key, for example: { number: 1,2,3 }, normally converted to: |
|
|
YES |
Used when unmarshalling (JSON to XML conversion). Adds type hints to the resulting XML to aid conversion back to JSON. See documentation here for an explanation.
|
Basic Usage
You can configure this data format both in any DSL.
Java DSL
With a default configuration:
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
Or with a more detailed configuration, as per the options above:
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat(); xmlJsonFormat.setEncoding("UTF-8"); xmlJsonFormat.setForceTopLevelObject(true); xmlJsonFormat.setTrimSpaces(true); xmlJsonFormat.setRootName("newRoot"); xmlJsonFormat.setSkipNamespaces(true); xmlJsonFormat.setRemoveNamespacePrefixes(true); xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));
In Spring DSL
Within the <dataFormats>
block, simply configure an xmljson
element with unique IDs:
<dataFormats> <xmljson id="xmljson"/> <xmljson id="xmljsonWithOptions" forceTopLevelObject="true" trimSpaces="true" rootName="newRoot" skipNamespaces="true" removeNamespacePrefixes="true" expandableProperties="d e"/> </dataFormats>
You then reuse these IDs from within the marshal
and unmarshal
DSL elements:
<dataFormats> <xmljson id="xmljson"/> <xmljson id="xmljsonWithOptions" forceTopLevelObject="true" trimSpaces="true" rootName="newRoot" skipNamespaces="true" removeNamespacePrefixes="true" expandableProperties="d e"/> </dataFormats>
To enable autocompletion for this component, you must be referring to the appropriate XML Schema files depending on whether Spring or Blueprint is being used. Remember that this data format is only available from Camel 2.10 onwards.
Then you simply refer to the data format object within your marshal() and unmarshal() DSLs:
<route> <from uri="direct:marshal"/> <marshal ref="xmljson"/> <to uri="mock:json" /> </route> <route> <from uri="direct:unmarshalWithOptions"/> <unmarshal ref="xmljsonWithOptions"/> <to uri="mock:xmlWithOptions"/> </route>
Namespace mappings
Dependencies
To use the XmlJson dataformat in your camel routes you need to add the following dependency to your pom.
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-xmljson</artifactId> <version>x.x.x</version> <!-- Use the same version as camel-core, but remember that this component is only available from 2.10 onwards --> </dependency>