JSON
Skip to end of metadata
Go to start of metadata

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

Icon

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

Using JSON data format with the Jackson library

Using JSON data format with the GSON library

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.

And then you can refer to this id in the 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.

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

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

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

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

The GsonDataFormat accepts an ExclusionStrategy in its constructor:

The line above will exclude fields annotated with @ExcludeAge when marshalling 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

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

PersonPojo.java

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 pre defined 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 GsonDataFromat in Spring XML file

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

Using gson from Camel Routes

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:

Directly specify your JSON view inside the Java DSL as:

And the same in XML DSL:

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

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

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

Labels
  • No labels