JSON Jackson 2
Since Camel 2.0
Jackson is a Data Format that uses the Jackson Library
-
Java
-
XML
-
YAML
from("activemq:My.Queue")
.marshal().json(JsonLibrary.Jackson)
.to("mqseries:Another.Queue"); <route>
<from uri="activemq:My.Queue"/>
<marshal><json library="Jackson"/></marshal>
<to uri="mqseries:Another.Queue"/>
</route> - route:
from:
uri: activemq:My.Queue
steps:
- marshal:
json:
library: Jackson
- to:
uri: mqseries:Another.Queue Jackson Options
The JSON Jackson 2 dataformat supports the following options which are listed below.
| Name | Default | Java Type | Description |
|---|---|---|---|
| Lookup and use the existing ObjectMapper with the given id when using Jackson. | ||
|
| Whether to lookup and use default Jackson ObjectMapper from the registry. The registry is only searched when autoDiscoverObjectMapper is true as well. | |
|
| If set to true (and useDefaultObjectMapper is true) then Camel will look for a Jackson ObjectMapper in the registry, and use it when there is exactly one. The options of this data format that configure the mapper, such as include, prettyPrint, enableFeatures and disableFeatures, are not applied to that ObjectMapper. | |
|
| To enable pretty printing output nicely formatted. Is by default false. | |
|
| Force generator that outputs JSON content to combine surrogate pairs (if any) into 4-byte characters. This should be preferred when using 4-byte characters such as Japanese. | |
| Class name of the java type to use when unmarshalling. | ||
| When marshalling a POJO to JSON you might want to exclude certain fields from the JSON output. With Jackson you can use JSON views to accomplish this. This option is to refer to the class which has JsonView annotations. | ||
| If you want to marshal a POJO to JSON, and the POJO has some fields with null values. And you want to skip these null values, you can set this option to NON_NULL. | ||
|
| Used for JMS users to allow the JMSType header from the JMS spec to specify a FQN classname to use to unmarshal to. | |
| Refers to a custom collection type to lookup in the registry to use. This option should rarely be used, but allows using different collection types than java.util.Collection based as default. | ||
|
| To unmarshal to a List of Map or a List of Pojo. | |
| To use custom Jackson modules specified as a String with FQN class names. Multiple classes can be separated by comma. | ||
| To use custom Jackson modules referred from the Camel registry. Multiple modules can be separated by comma. | ||
| Set of features to enable on the Jackson ObjectMapper. Multiple features can be separated by comma. When using Jackson 3, a feature can be qualified with its enum class name (e.g. SerializationFeature.WRAP_ROOT_VALUE) to tell apart features with the same name. | ||
| Set of features to disable on the Jackson ObjectMapper. Multiple features can be separated by comma. When using Jackson 3, a feature can be qualified with its enum class name (e.g. SerializationFeature.WRAP_ROOT_VALUE) to tell apart features with the same name. | ||
|
| If enabled then Jackson, Gson or JSON-B is allowed to attempt to use the CamelJacksonUnmarshalType, CamelGsonUnmarshalType or CamelJsonbUnmarshallType header during the unmarshalling. This should only be enabled when desired to be used. | |
| If set then Jackson will use the Timezone when marshalling/unmarshalling. | ||
| Optional schema resolver used to lookup schemas for the data in transit. | ||
|
| When not disabled, the SchemaResolver will be looked up into the registry. | |
| If set then Jackson will use the defined Property Naming Strategy. Possible values are: LOWER_CAMEL_CASE, LOWER_DOT_CASE, LOWER_CASE, KEBAB_CASE, SNAKE_CASE and UPPER_CAMEL_CASE. | ||
|
| Whether the data format should set the Content-Type header with the type from the data format. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSON. | |
| Sets the maximum string length (in chars or bytes, depending on input context). The default is 20,000,000. |
Message Body
The message body at each side of the JSON Jackson 2 data format: Marshal input and Unmarshal input are the bodies marshal and unmarshal can take, and Marshal output and Unmarshal output the body after them.
| Side | Body | Description |
|---|---|---|
Marshal input |
| A Map, a List or a POJO to write as JSON. Text, bytes or a stream that is JSON already is written as it is. |
Marshal output |
| The JSON in the stream cache, or a byte array when stream caching is off. |
Unmarshal input |
| |
Unmarshal output |
| A Map (LinkedHashMap) for a JSON object, a List (ArrayList) for an array. With
Not known when |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
2 and 4 bytes characters
Jackson by default outputs 4-byte characters (in languages such as Japanese) as surrogate pair, escaped. This is compliant with Json specification. This can however frustrate users, because it garbles regular output, such as names and texts with Unicode escapes. To avoid this, users commonly use 4-bytes would need to turn on combineUnicodeSurrogates=true in the Camel dataformat.
A body that is already JSON
A body that already is the JSON text is written as it is, not serialized as a Java object: a file from a consumer such as file or ftp, an InputStream, a byte[], and a String whose text is a JSON object or array (it starts with { and ends with }, or [ and ]). So marshal: json after reading a JSON file, or after building the JSON with a template or a Simple expression, sends that JSON unchanged. Any other String is written as a JSON string, and POJOs, maps and lists are marshalled as usual. The Jackson, Gson, Jsonb and Fastjson JSON data formats do the same.
Using custom ObjectMapper
You can configure JacksonDataFormat to use a custom ObjectMapper in case you need more control of the mapping configuration.
Refer to your ObjectMapper with the objectMapper option, such as objectMapper=#myMapper.
By default, the data format does not look up an ObjectMapper in the registry. To look one up, set autoDiscoverObjectMapper=true (and leave useDefaultObjectMapper at its default, true). The data format then uses the ObjectMapper only when there is exactly one com.fasterxml.jackson.databind.ObjectMapper in the registry, and otherwise creates its own. Subclasses such as XmlMapper count too: for example, Spring Boot can provide an ObjectMapper bean, and an XmlMapper bean next to it makes two, so neither is used. Camel logs the ObjectMapper it found at DEBUG level.
Camel uses an ObjectMapper from the registry as it is: the options of the data format that configure the mapper, such as include, prettyPrint, enableFeatures, disableFeatures, moduleClassNames, timezone and namingStrategy, are not applied to it.
Using Jackson for automatic type conversion
The camel-jackson module allows integrating Jackson as a Type Converter.
This gives a set of out-of-the-box converters to/from the Jackson type JSonNode, such as converting from JSonNode to String or vice versa.
The type converter does not use the options of the data format. Its JsonNode converters, and the POJO conversion below, use the ObjectMapper in the registry when there is exactly one (subclasses such as XmlMapper count too), without autoDiscoverObjectMapper. Otherwise, the type converter creates its own ObjectMapper.
Enabling more type converters and support for POJOs
To enable POJO conversion support for camel-jackson then this must be enabled, which is done by setting the following options on the CamelContext global options, as shown:
// Enable Jackson JSON type converter for more types.
camelContext.getGlobalOptions().put("CamelJacksonEnableTypeConverter", "true");
// Allow Jackson JSON to convert to pojo types also
// (by default, Jackson only converts to String and other simple types)
getContext().getGlobalOptions().put("CamelJacksonTypeConverterToPojo", "true"); The camel-jackson type converter integrates with JAXB which means you can annotate POJO class with JAXB annotations that Jackson can use. You can also use Jackson’s own annotations in your POJO classes.
Dependencies
To use Jackson in your Camel routes, you need to add the dependency on camel-jackson, which implements this data format.
If you use Maven, you could add the following to your pom.xml, substituting the version number for the latest & greatest release:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>