...
Tip |
---|
title | 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
Code Block |
---|
|
// 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
Code Block |
---|
|
// 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
Code Block |
---|
|
// Let's turn Object messages into JSON then send to MQSeries
from("activemq:My.Queue")
.marshal().json(JsonLibrary.Gson)
.to("mqseries:Another.Queue");
|
...
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.:
Wiki Markup |
---|
{snippet:id=marker|lang=java|url=camel/trunk/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/Views.java} |
Use Second, use the marker classes with the
@JsonView
annotation to include/exclude certain fields. The annotation also works on getters
.: Wiki Markup |
---|
{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.
Wiki Markup |
---|
{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
:. Code Block |
---|
{"age":30, "weight":70}
|
...