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, XML is considered the high-level format, while JSON is the low-level format. Hence, the marshal/unmarshal semantics are assigned as follows:
OptionsThis 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.
Basic Usage with Java DSLExplicitly instantiating the data formatJust instantiate the XmlJsonDataFormat from package org.apache.camel.dataformat.xmljson. Make sure you have installed the XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat(); To tune the behaviour of the data format as per the options above, use the appropriate setters: 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"));
Once you've instantiated the data format, the next step is to actually use the it from within the // from XML to JSON
from("direct:marshal").marshal(xmlJsonFormat).to("mock:json");
// from JSON to XML
from("direct:unmarshal").unmarshal(xmlJsonFormat).to("mock:xml");
Defining the data format in-lineAlternatively, you can define the data format inline by using the // from XML to JSON - inline dataformat
from("direct:marshalInline").marshal().xmljson().to("mock:jsonInline");
// from JSON to XML - inline dataformat
from("direct:unmarshalInline").unmarshal().xmljson().to("mock:xmlInline");
If you wish, you can even pass in a Map<String, String> to the inline methods to provide custom options: Map<String, String> xmlJsonOptions = new HashMap<String, String>();
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ENCODING, "UTF-8");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ROOT_NAME, "newRoot");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.SKIP_NAMESPACES, "true");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.REMOVE_NAMESPACE_PREFIXES, "true");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.EXPANDABLE_PROPERTIES, "d e");
// from XML to JSON - inline dataformat w/ options
from("direct:marshalInlineOptions").marshal().xmljson(xmlJsonOptions).to("mock:jsonInlineOptions");
// form JSON to XML - inline dataformat w/ options
from("direct:unmarshalInlineOptions").unmarshal().xmljson(xmlJsonOptions).to("mock:xmlInlineOptions");
Basic usage with Spring or Blueprint DSLWithin the <dataFormats>
<xmljson id="xmljson"/>
<xmljson id="xmljsonWithOptions" forceTopLevelObject="true" trimSpaces="true" rootName="newRoot" skipNamespaces="true"
removeNamespacePrefixes="true" expandableProperties="d e"/>
</dataFormats>
Then you simply refer to the data format object within your <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>
Enabling XML DSL autocompletion for this component is easy: just refer to the appropriate Schema locations, depending on whether you're using Spring or Blueprint DSL. Remember that this data format is available from Camel 2.10 onwards, so only schemas from that version onwards will include these new XML elements and attributes. The syntax with Blueprint is identical to that of the Spring DSL. Just ensure the correct namespaces and schemaLocations are in use. Namespace mappingsXML has namespaces to fully qualify elements and attributes; JSON doesn't. You need to take this into account when performing XML-JSON conversions. To bridge the gap, Json-lib has an option to bind namespace declarations in the form of prefixes and namespace URIs to XML output elements while unmarshalling (i.e. converting from JSON to XML). For example, provided the following JSON string: { 'pref1:a': 'value1', 'pref2:b': 'value2 }
you can ask Json-lib to output namespace declarations on elements "pref1:a" and "pref2:b" to bind the prefixes "pref1" and "pref2" to specific namespace URIs. To use this feature, simply create The In order to define a default namespace, just leave the corresponding key field empty: Binding namespace declarations to an element name = empty string will attach those namespaces to the root element. The full code would look like that: XmlJsonDataFormat namespacesFormat = new XmlJsonDataFormat();
List<XmlJsonDataFormat.NamespacesPerElementMapping> namespaces = new ArrayList<XmlJsonDataFormat.NamespacesPerElementMapping>();
namespaces.add(new XmlJsonDataFormat.
NamespacesPerElementMapping("", "|ns1|http://camel.apache.org/test1||http://camel.apache.org/default|"));
namespaces.add(new XmlJsonDataFormat.
NamespacesPerElementMapping("surname", "|ns2|http://camel.apache.org/personalData|" +
"ns3|http://camel.apache.org/personalData2|"));
namespacesFormat.setNamespaceMappings(namespaces);
namespacesFormat.setRootElement("person");
And you can achieve the same in Spring DSL. ExampleUsing the namespace bindings in the Java snippet above on the following JSON string: { "name": "Raul", "surname": "Kripalani", "f": true, "g": null}
Would yield the following XML: <person xmlns="http://camel.apache.org/default" xmlns:ns1="http://camel.apache.org/test1">
<f>true</f>
<g null="true"/>
<name>Raul</name>
<surname xmlns:ns2="http://camel.apache.org/personalData" xmlns:ns3="http://camel.apache.org/personalData2">Kripalani</surname>
</person>
Remember that the JSON spec defines a JSON object as follows:
That's why the elements are in a different order in the output XML. |