JSON

JSON is a Data Format to marshal and unmarshal Java objects to and from JSON.

For JSON to object marshalling, Camel provides integration with three popular JSON libraries:

Every library requires adding the special camel component (see "Dependency..." paragraphs further down). By default Camel uses the XStream library.

Direct, bi-directional JSON <=> XML conversions

As of Camel 2.10, Camel supports direct, bi-directional JSON <=> XML conversions via the camel-xmljson data format, which is documented separately.

Using JSON Data Format With the XStream Library

java// Let's turn Object messages into JSON then send to MQSeries from("activemq:My.Queue") .marshal().json() .to("mqseries:Another.Queue");

Using JSON Data Format With the Jackson Library

java// Let's turn Object messages into JSON then send to MQSeries from("activemq:My.Queue") .marshal().json(JsonLibrary.Jackson) .to("mqseries:Another.Queue");

Using JSON Data Format With the GSON Library

java// Let's turn Object messages into JSON then send to MQSeries from("activemq:My.Queue") .marshal().json(JsonLibrary.Gson) .to("mqseries:Another.Queue");

Using JSON in Spring DSL

When using Data Format in Spring DSL you need to declare the data formats first. This is done in the DataFormats XML tag.

xml<dataFormats> <!-- Here we define a Json data format with the id jack and that it should use the TestPojo as the class type when doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type. --> <json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"/> </dataFormats>

And then you can refer to this id in the route:

xml<route> <from uri="direct:back"/> <unmarshal ref="jack"/> <to uri="mock:reverse"/> </route>

Excluding POJO Fields From Marshalling

As of Camel 2.10
When marshaling 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.

First create one or more marker classes:{snippet:id=marker|lang=java|url=camel/trunk/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/Views.java}Second, use the marker classes with the @JsonView annotation to include/exclude certain fields. The annotation also works on getters:{snippet:id=jsonview|lang=java|url=camel/trunk/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestPojoView.java}Finally, use the Camel JacksonDataFormat to marshal the above POJO to JSON.{snippet:id=format|lang=java|url=camel/trunk/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalViewTest.java}Note: the height field is missing in the resulting JSON.

{"age":30, "weight":70}

The GSON library supports a similar feature through the notion of ExclusionStrategies:{snippet:id=strategy|lang=java|url=camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonMarshalExclusionTest.java}The GsonDataFormat accepts an ExclusionStrategy in its constructor:{snippet:id=format|lang=java|url=camel/trunk/components/camel-gson/src/test/java/org/apache/camel/component/gson/GsonMarshalExclusionTest.java}The line above will exclude fields annotated with @ExcludeAge when marshaling to JSON.

Configuring Field Naming Policy

Available as of Camel 2.11

The GSON library supports specifying policies and strategies for mapping from JSON to POJO fields. A common naming convention is to map JSON fields using lower case with underscores.

We may have this JSON string

{ "id" : 123, "first_name" : "Donald" "last_name" : "Duck" }

Which we want to map to a POJO that has getter/setters as:

javaPersonPojo.javaPersonPojo.javapublic class PersonPojo { private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }

Then we can configure the org.apache.camel.component.gson.GsonDataFormat in a Spring XML files as shown below. Notice we use fieldNamingPolicy property to set the field mapping. This property is an enum from GSon com.google.gson.FieldNamingPolicy which has a number of predefined mappings.

If you need full control you can use the property FieldNamingStrategy and implement a custom com.google.gson.FieldNamingStrategy where you can control the mapping.

Configuring GsonDataFormat in Spring XML filexmlConfiguring GsonDataFromat in Spring XML file<!-- define the gson data format, where we configure the data format using the properties --> <bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat"> <!-- we want to unmarshal to person pojo --> <property name="unmarshalType" value="org.apache.camel.component.gson.PersonPojo"/> <!-- we want to map fields to use lower case and underscores --> <property name="fieldNamingPolicy" value="LOWER_CASE_WITH_UNDERSCORES"/> </bean>

And use it in Camel routes by referring to its bean id as shown:

Using gson from Camel routesxmlUsing gson from Camel Routes<camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:inPojo"/> <marshal ref="gson"/> </route> <route> <from uri="direct:backPojo"/> <unmarshal ref="gson"/> </route> </camelContext>

Include/Exclude Fields Using the jsonView Attribute With JacksonDataFormat

Available as of Camel 2.12

As an example of using this attribute you can instead of:

javaJacksonDataFormat ageViewFormat = new JacksonDataFormat(TestPojoView.class, Views.Age.class); from("direct:inPojoAgeView") .marshal(ageViewFormat);

Directly specify your JSON view inside the Java DSL as:

javafrom("direct:inPojoAgeView") .marshal().json(TestPojoView.class, Views.Age.class);

And the same in XML DSL:

xml<from uri="direct:inPojoAgeView"/> <marshal> <json library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojoView" jsonView="org.apache.camel.component.jackson.Views$Age"/> </marshal>

Setting Serialization Include Option for Jackson Marshal

Available as of Camel 2.13.3/2.14

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, then you need to set either an annotation on the POJO, 

java@JsonInclude(Include.NON_NULL) public class MyPojo { // ... }

But this requires you to include that annotation in your POJO source code. You can also configure the Camel JsonDataFormat to set the include option, as shown below:

javaJacksonDataFormat format = new JacksonDataFormat(); format.setInclude("NON_NULL");

Or from XML DSL you configure this as

xml<dataFormats> <json id="json" library="Jackson" include="NON_NULL"/> </dataFormats>

Unmarshaling from JSON to POJO with Dynamic Class Name

Available as of Camel 2.14

If you use Jackson to unmarshal JSON to POJO, then you can now specify a header in the message that indicate which class name to unmarshal to. The header has key CamelJacksonUnmarshalType if that header is present in the message, then Jackson will use that as FQN for the POJO class to unmarshal the JSON payload as. Notice that behavior is enabled out of the box from Camel 2.14

 For JMS end users there is the JMSType header from the JMS spec that indicates that also. To enable support for JMSType you would need to turn that on, on the Jackson data format as shown:

javaJacksonDataFormat format = new JacksonDataFormat(); format.setAllowJmsType(true);

Or from XML DSL you configure this as

xml<dataFormats> <json id="json" library="Jackson" allowJmsType="true"/> </dataFormats>

Unmarshaling From JSON to List<Map> or List<pojo>

Available as of Camel 2.14

If you are using Jackson to unmarshal JSON to a list of map/POJO, you can now specify this by setting useList="true" or use the org.apache.camel.component.jackson.ListJacksonDataFormat.

For example, with Java you can do as shown below:

javaJacksonDataFormat format = new ListJacksonDataFormat(); // or JacksonDataFormat format = new JacksonDataFormat(); format.useList(); // and you can specify the pojo class type also format.setUnmarshalType(MyPojo.class);

And if you use XML DSL then you configure to use list using useList attribute as shown below:

xml<dataFormats> <json id="json" library="Jackson" useList="true"/> </dataFormats>

And you can specify the pojo type also

xml<dataFormats> <json id="json" library="Jackson" useList="true" unmarshalTypeName="com.foo.MyPojo"/> </dataFormats>

Using Custom Jackson's ObjectMapper

Available from Camel 2.17

You can use custom Jackson ObjectMapper instance, can be configured as shown below.

xml<dataFormats> <json id="json" library="Jackson" objectMapper="myMapper"/> </dataFormats>

Where myMapper is the id of the custom instance that Camel will lookup in the Registry.

Using Custom Jackson Modules

Available as of Camel 2.15

You can use custom Jackson modules by specifying the class names of those using the moduleClassNames option as shown below.

xml<dataFormats> <json id="json" library="Jackson" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleClassNames="com.foo.MyModule,com.foo.MyOtherModule"/> </dataFormats>

When using moduleClassNames then the custom Jackson modules are not configured, by created using default constructor and used as-is. If a custom module needs any custom configuration, then an instance of the module can be created and configured, and then use modulesRefs to refer to the module as shown below:

xml<bean id="myJacksonModule" class="com.foo.MyModule"> ... // configure the module as you want </bean>   <dataFormats> <json id="json" library="Jackson" useList="true" unmarshalTypeName="com.foo.MyPojo" moduleRefs="myJacksonModule"/> </dataFormats>

 Multiple modules can be specified separated by comma, such as moduleRefs="myJacksonModule,myOtherModule".

Enabling or Disable Features Using Jackson

Available as of Camel 2.15

Jackson has a number of features you can enable or disable, which its ObjectMapper uses. For example to disable failing on unknown properties when marshaling, you can configure this using the disableFeatures:

xml<dataFormats> <json id="json" library="Jackson" unmarshalTypeName="com.foo.MyPojo" disableFeatures="FAIL_ON_UNKNOWN_PROPERTIES"/> </dataFormats>

You can disable multiple features by separating the values using comma. The values for the features must be the name of the enums from Jackson from the following enum classes

  • com.fasterxml.jackson.databind.SerializationFeature
  • com.fasterxml.jackson.databind.DeserializationFeature
  • com.fasterxml.jackson.databind.MapperFeature

To enable a feature use the enableFeatures options instead.

From Java code you can use the type safe methods from camel-jackson module:

javaJacksonDataFormat df = new JacksonDataFormat(MyPojo.class); df.disableFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); df.disableFeature(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

Converting Maps to POJO Using Jackson

Available since Camel 2.16. Jackson ObjectMapper can be used to convert maps to POJO objects. Jackson component comes with the data converter that can be used to convert java.util.Map instance to non-String, non-primitive and non-Number objects.

javaMap<String, Object> invoiceData = new HashMap<String, Object>(); invoiceData.put("netValue", 500); producerTemplate.sendBody("direct:mapToInvoice", invoiceData); ... // Later in the processor Invoice invoice = exchange.getIn().getBody(Invoice.class);

If there is a single ObjectMapper instance available in the Camel registry, it will used by the converter to perform the conversion. Otherwise the default mapper will be used.  

Formatted JSON marshalling (pretty-printing)

Available as of Camel 2.16

Using the prettyPrint option one can output a well formatted JSON while marshaling:

xml<dataFormats> <json id="xstream" prettyPrint="true"/> <json id="jackson" prettyPrint="true" library="Jackson"/> <json id="gson" prettyPrint="true" library="Gson"/> </dataFormats>

And in Java DSL:

javafrom("direct:inPretty").marshal().json(true);   from("direct:inPretty").marshal().json(JsonLibrary.Jackson, true);   from("direct:inPretty").marshal().json(JsonLibrary.Gson, true);

Please note that as of Camel 2.16 there are five different overloaded json() DSL methods which support the prettyPrint option in combination with other settings for JsonLibrary, unmarshalType, jsonView etc. 

 Integrating Jackson with Camel's TypeConverters

 Available as of Camel 2.17

The camel-jackson module allows to integrate Jackson as a Type Converter in the Camel registry. This works in similar ways that camel-jaxb integrates with the type converter as well. However camel-jackson must be explicit enabled, which is done by setting some options on the CamelContext properties, as shown below:

java// Enable Jackson JSON type converter. getContext().getProperties().put("CamelJacksonEnableTypeConverter", "true"); // Allow Jackson JSON to convert to pojo types also (by default Jackson only converts to String and other simple types). getContext().getProperties().put("CamelJacksonTypeConverterToPojo", "true");

The camel-jackson type converter integrates with JAXB which means you can annotate POJO class with JAXB annotations that Jackson can leverage. 

Dependencies for XStream

To use JSON in your camel routes you need to add the a dependency on camel-xstream which implements this data format.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-xstream</artifactId> <version>2.9.2</version> </dependency>

Dependencies for Jackson

To use JSON in your camel routes you need to add the a dependency on camel-jackson which implements this data format.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jackson</artifactId> <version>2.9.2</version> </dependency>

Dependencies for GSON

To use JSON in your camel routes you need to add the a dependency on camel-gson which implements this data format.

If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-gson</artifactId> <version>2.10.0</version> </dependency>
© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram