This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • XmlJson

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 unmarshallingun-marshaling). This data format provides the capability to convert from XML to JSON and viceversa directly, without stepping through intermediate POJOs.

...

  • marshalling => converting from XML to JSON
  • unmarshalling un-marshaling => converting from JSON to XML.

...

Div
classconfluenceTableSmall

Option

Type

Default

Description

encoding

String

UTF-8 (*)

Used when unmarshalling un-marshaling (JSON to XML conversion).

Sets the encoding for the call to XMLSerializer.write() method, hence it is only used when producing XML.
However, when producing JSON, the encoding is determined by the input String being processed.

If the conversion is performed on an InputStreamjson-lib uses the platform's default encoding, e.g., determined by the file.encoding system property.

elementName

String

'e' (*)

Used when unmarshalling un-marshaling (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 un-marshaling (JSON to XML conversion).

Specifies the name of the top-level XML element.

For example, when converting:

[1, 2, 3]

it is, by default, translated as:

<a><e>1</e><e>2</e><e>3</e></a>

By setting this option or rootName, you can alter the name of the element a.

rootName

String

none (*)

Used when unmarshalling un-marshaling (JSON to XML conversion).

When converting any JSON construct (object, array, null) to XML (unmarshallingun-marshaling), this option specifies the name of the top-level XML 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" } 

is translated as:

<root><x>value1</x><y>value2</y></root>

otherwise the root element would be named o.

namespaceLenient

Boolean

false (*)

Used when unmarshalling un-marshaling (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 un-marshaling (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.

expandableProperties

List<String>

none

Used when unmarshalling un-marshaling (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, the following JSON: 

{ "number": 1,2,3 }

is normally translated as:

<number><e>1</e><e>2</e><e>3</e></number>

where e can be modified by setting elementName.

However, if number is set as an expandable property, it's translated as:

<number>1</number><number>2</number><number>3</number>

typeHints

TypeHintsEnum

YES

Used when unmarshalling un-marshaling (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,  typeHintsCompatibilitytrue

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

forceTopLevelObject

Boolean

false (*)

Used when marshalling marshaling (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 this option is false, the XML string:

<a><x>1</x><y>2</y></a>

is translated as:

{"x": "1", "y": "2"}

If true, it's translated as:

{ "a":  { "x": "1", "y": "2" }}

skipWhitespace

Boolean

false (*)

Used when marshalling marshaling (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 marshaling (XML to JSON conversion).

Determines whether leading and trailing white spaces will be omitted from String values.

skipNamespaces

Boolean

false (*)

Used when marshalling marshaling (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 marshaling (XML to JSON conversion).

Removes the namespace prefixes from XML qualified elements, so that the resulting JSON string does not contain them.

...

Code Block
langjava
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");

// FrommFrom JSON to XML - inline dataformat w/options
from("direct:unmarshalInlineOptions")
  .unmarshal()
  .xmljson(xmlJsonOptions)
  .to("mock:xmlInlineOptions");

...

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 unmarshallingun-marshaling, e.g., converting from JSON to XML.

...