You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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.

Unknown macro: {div}

Name

Type

Default

Description

encoding

String

UTF-8 (*)

*Used when unmarshalling (JSON to XML conversion). *Sets the encoding for the call to XMLSerializer.write() method, hence it is only used when producing XML.
When producing JSON, the encoding is determined by the input String being processed. If the conversion is performed on an InputStream, json-lib uses the platform's default encoding (e.g. determined by the file.encoding system property).

elementName

String

'e' (*)

Used when unmarshalling (JSON to XML conversion). Specifies the name of the XML elements representing each array element. See json-lib doc.

arrayName

String

'a' (*)

*Used when unmarshalling (JSON to XML conversion). *Specifies the name of the top-level XML element.
For example, when converting [1, 2, 3], it will be output by default as <a><e>1</e><e>2</e><e>3</e></a>. By setting this option or rootName, you can alter the name of element 'a'.

rootName

String

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.
If not set, json-lib will use arrayName or objectName (default value: 'o', at the current time it is not configurable in this data format). If set to 'root', the JSON string { 'x': 'value1', 'y' : 'value2' } would turn into <root><x>value1</x><y>value2</y></root>, otherwise the 'root' element would be named 'o'.

forceTopLevelObject

Boolean

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 <a><x>1</x><y>2</y></a> turns into {{{ 'x: '1', 'y': '2' }}}. Otherwise, it turns into {{{ 'a': { 'x: '1', 'y': '2' }}}}.

namespaceLenient

Boolean

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.

namespaceMappings

List<NamespacesPerElementMapping>

none

*Used when unmarshalling (JSON to XML conversion). *Binds namespace prefixes and URIs to specific JSON elements. NamespacesPerElementMapping is a wrapper around an element name + a Map of prefixes against URIs.

skipWhitespace

Boolean

false (*)

Used when marshalling (XML to JSON conversion). Determines whether white spaces between XML elements will be regarded as text values or disregarded.

trimSpaces

Boolean

false (*)

Used when marshalling (XML to JSON conversion). Determines whether leading and trailing white spaces will be omitted from String values.

skipNamespaces

Boolean

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.

removeNamespacePrefixes

Boolean

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.

expandableProperties

List<String>

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: <number><e>1</e><e>2</e><e>3</e></number> (where e can be modified by setting elementName), would instead translate to <number>1</number><number>2</number><number>3</number>, if "number" is set as an expandable property

typeHints

TypeHintsEnum

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. TypeHintsEnum comprises the following values, which lead to different combinations of the underlying XMLSerializer's typeHintsEnabled and typeHintsCompatibility flags:

  • TypeHintsEnum.NO => typeHintsEnabled = false
  • TypeHintsEnum.YES =>  typeHintsEnabled = true,  typeHintsCompatibility = true
  • TypeHintsEnum.WITH_PREFIX =>  typeHintsEnabled = true,  typeHintsCompatibility = false

Basic Usage

In Spring the dataformat is configured first and then used in routes


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>2.10</version>
</dependency>

See Also

  • No labels