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

Compare with Current View Page History

« Previous Version 13 Next »

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:

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

// lets 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

// lets 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

// lets 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.

        <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:

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

Excluding POJO fields from marshalling

As of Camel 2.10
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. First create one or more marker classes.

Error formatting macro: snippet: java.lang.NullPointerException

Use the marker classes with the @JsonView annotation to include/exclude certain fields. The annotation also works on getters.

Error formatting macro: snippet: java.lang.NullPointerException

Finally use the Camel JacksonDataFormat to marshall the above POJO to JSON.

Error formatting macro: snippet: java.lang.NullPointerException

Note that the weight field is missing in the resulting JSON:

{"age":30, "height":190}

The GSON library supports a similar feature through the notion of ExclusionStrategies:

Error formatting macro: snippet: java.lang.NullPointerException

The GsonDataFormat accepts an ExclusionStrategy in its constructor:

Error formatting macro: snippet: java.lang.NullPointerException

The line above will exclude fields annotated with @ExcludeAge when marshalling to JSON.

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).

<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).

<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).

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-gson</artifactId>
  <version>2.10.0</version>
</dependency>
  • No labels