Hi Richard

Remember to add a note in the documentation that this is available as of
Camel 2.10.


On Mon, Apr 2, 2012 at 10:12 PM, <conflue...@apache.org> wrote:

>    JSON <https://cwiki.apache.org/confluence/display/CAMEL/JSON> Page *
> edited* by Richard 
> Kettelerij<https://cwiki.apache.org/confluence/display/~rkettelerij>
> *Comment:* CAMEL-5135
>
> Changes (2)
>  ...
> By default Camel uses the XStream library.
>
> h3. Using JSson data format with the XStream library
>  {code}
> // lets turn Object messages into json then send to MQSeries
> ...
> {code}
>
> h3. Excluding POJO fields from marshalling
> When marshalling a POJO to JSON you might want to exclude certain fields
> from the JSON output. With Jackson you can use [JSON views|
> http://wiki.fasterxml.com/JacksonJsonViews] 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}
>
>
> 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 marshall 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 that the weight field in missing in the resulting JSON:
> {code}
> {"age":30, "height":190}
> {code}
>
> The GSON library supports a similar feature through the notion of
> [ExclusionStrategies|
> http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/ExclusionStrategy.html]:
>
> {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}} during
> JSON marshalling.
>
> h3. Dependencies for XStream
>
> ...
>  Full Content
> JSON
>
> JSON is a Data 
> Format<https://cwiki.apache.org/confluence/display/CAMEL/Data+Format>to 
> marshal and unmarshal Java objects to and from
> JSON <http://www.json.org/>.
>
> In Camel 1.6 its only the XStream library that is supported and its
> default.
>
> In Camel 2.0 we added support for more libraries:
> Camel provides integration with two popular JSon libraries:
>
>    - The XStream library <http://xstream.codehaus.org/> and Jettsion
>    <http://jettison.codehaus.org/>
>    - The Jackson library <http://xircles.codehaus.org/projects/jackson>
>    - *Camel 2.10:* The GSon library<http://code.google.com/p/google-gson/>
>
> By default Camel uses the XStream library.
> Using Json data format with the XStream library
>
> // lets turn Object messages into json then send to 
> MQSeriesfrom("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 
> MQSeriesfrom("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 
> MQSeriesfrom("activemq:My.Queue").
>   marshal().json(JsonLibrary.Gson).
>   to("mqseries:Another.Queue");
>
>  Using Json in Spring DSL
>
> When using Data 
> Format<https://cwiki.apache.org/confluence/display/CAMEL/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
>
> When marshalling a POJO to JSON you might want to exclude certain fields
> from the JSON output. With Jackson you can use JSON 
> views<http://wiki.fasterxml.com/JacksonJsonViews>to accomplish this. First 
> create one or more marker classes.
>
> public class Views {
>
>     static class Weight { }
>     static class Age { }
> }
>
>  Use the marker classes with the @JsonView annotation to include/exclude
> certain fields. The annotation also works on getters.
>
> @JsonView(Views.Age.class)private int age = 30;
> private int height = 190;
>
> @JsonView(Views.Weight.class)private int weight = 70;
>
>  Finally use the Camel JacksonDataFormat to marshall the above POJO to
> JSON.
>
>                 JacksonDataFormat ageViewFormat = new 
> JacksonDataFormat(TestPojoView.class, Views.Age.class);
>                 from("direct:inPojoAgeView").marshal(ageViewFormat);
>                 
> from("direct:backPojoAgeView").unmarshal(ageViewFormat).to("mock:reversePojoAgeView");
>
>                 JacksonDataFormat weightViewFormat = new 
> JacksonDataFormat(TestPojoView.class, Views.Weight.class);
>
>                 from("direct:inPojoWeightView").marshal(weightViewFormat);
>                 
> from("direct:backPojoWeightView").unmarshal(weightViewFormat).to("mock:reversePojoWeightView");
>             }
>         };
>     }
>
> }
>
>  Note that the weight field in missing in the resulting JSON:
>
> {"age":30, "height":190}
>
>  The GSON library supports a similar feature through the notion of
> ExclusionStrategies<http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/ExclusionStrategy.html>
> :
>
> /**
>  * Strategy to exclude {@link ExcludeAge} annotated fields
>  */protected static class AgeExclusionStrategy implements ExclusionStrategy {
>
>     @Override
>     public boolean shouldSkipField(FieldAttributes f) {
>         return f.getAnnotation(ExcludeAge.class) != null;
>     }
>
>     @Override
>     public boolean shouldSkipClass(Class<?> clazz) {
>         return false;
>     }
> }
>
>  The GsonDataFormat accepts an ExclusionStrategy in its constructor:
>
> GsonDataFormat ageExclusionFormat = new 
> GsonDataFormat(TestPojoExclusion.class, new AgeExclusionStrategy());
>
>  The line above will exclude fields annotated with @ExcludeAge during
> JSON marshalling.
> 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<https://cwiki.apache.org/confluence/display/CAMEL/Download>
> ).
>
> <dependency>
>   <groupId>org.apache.camel</groupId>
>   <artifactId>camel-xstream</artifactId>
>   <version>2.0</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<https://cwiki.apache.org/confluence/display/CAMEL/Download>
> ).
>
> <dependency>
>   <groupId>org.apache.camel</groupId>
>   <artifactId>camel-jackson</artifactId>
>   <version>2.0</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<https://cwiki.apache.org/confluence/display/CAMEL/Download>
> ).
>
> <dependency>
>   <groupId>org.apache.camel</groupId>
>   <artifactId>camel-gson</artifactId>
>   <version>2.10</version></dependency>
>
>   Change Notification 
> Preferences<https://cwiki.apache.org/confluence/users/viewnotifications.action>
> View Online <https://cwiki.apache.org/confluence/display/CAMEL/JSON> | View
> Changes<https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=101227&revisedVersion=6&originalVersion=5>|
>  Add
> Comment<https://cwiki.apache.org/confluence/display/CAMEL/JSON?showComments=true&showCommentArea=true#addcomment>
>



-- 
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cib...@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Reply via email to