camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > JSON
Date Mon, 27 May 2013 12:28:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/JSON">JSON</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bvahdat">Babak
Vahdat</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Directly specify <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">the</span>
<span class="diff-added-words"style="background-color: #dfd;">your</span> [JSON
view|http://wiki.fasterxml.com/JacksonJsonViews] inside the Java DSL as: <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code:java} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="JSON-JSON"></a>JSON</h2>
<p>JSON is a <a href="/confluence/display/CAMEL/Data+Format" title="Data Format">Data
Format</a> to marshal and unmarshal Java objects to and from <a href="http://www.json.org/"
class="external-link" rel="nofollow">JSON</a>. </p>

<p>For JSON to object marshalling, Camel provides integration with three popular JSON
libraries:</p>
<ul class="alternate" type="square">
	<li>The <a href="http://xstream.codehaus.org/" class="external-link" rel="nofollow">XStream
library</a> and <a href="http://jettison.codehaus.org/" class="external-link" rel="nofollow">Jettsion
</a></li>
	<li>The <a href="http://xircles.codehaus.org/projects/jackson" class="external-link"
rel="nofollow">Jackson library</a></li>
	<li><b>Camel 2.10:</b> The <a href="http://code.google.com/p/google-gson/"
class="external-link" rel="nofollow">GSon library</a></li>
</ul>


<p>By default Camel uses the XStream library. </p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Direct, bi-directional
JSON &lt;=&gt; XML conversions</b><br />As of Camel 2.10, Camel supports
direct, bi-directional JSON &lt;=&gt; XML conversions via the <a href="/confluence/display/CAMEL/XmlJson"
title="XmlJson">camel-xmljson</a> data format, which is documented separately.</td></tr></table></div>

<h3><a name="JSON-UsingJSONdataformatwiththeXStreamlibrary"></a>Using JSON
data format with the XStream library</h3>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// lets turn <span class="code-object">Object</span>
messages into json then send to MQSeries
</span>from(<span class="code-quote">"activemq:My.Queue"</span>).
  marshal().json().
  to(<span class="code-quote">"mqseries:Another.Queue"</span>);
</pre>
</div></div>

<h3><a name="JSON-UsingJSONdataformatwiththeJacksonlibrary"></a>Using JSON
data format with the Jackson library</h3>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// lets turn <span class="code-object">Object</span>
messages into json then send to MQSeries
</span>from(<span class="code-quote">"activemq:My.Queue"</span>).
  marshal().json(JsonLibrary.Jackson).
  to(<span class="code-quote">"mqseries:Another.Queue"</span>);
</pre>
</div></div>

<h3><a name="JSON-UsingJSONdataformatwiththeGSONlibrary"></a>Using JSON
data format with the GSON library</h3>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// lets turn <span class="code-object">Object</span>
messages into json then send to MQSeries
</span>from(<span class="code-quote">"activemq:My.Queue"</span>).
  marshal().json(JsonLibrary.Gson).
  to(<span class="code-quote">"mqseries:Another.Queue"</span>);
</pre>
</div></div>


<h4><a name="JSON-UsingJSONinSpringDSL"></a>Using JSON in Spring DSL</h4>
<p>When using <a href="/confluence/display/CAMEL/Data+Format" title="Data Format">Data
Format</a> in Spring DSL you need to declare the data formats first. This is done in
the <b>DataFormats</b> XML tag.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
        <span class="code-tag">&lt;dataFormats&gt;</span>
            &lt;!-- 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 --&gt;
            <span class="code-tag">&lt;json id=<span class="code-quote">"jack"</span>
library=<span class="code-quote">"Jackson"</span> unmarshalTypeName=<span class="code-quote">"org.apache.camel.component.jackson.TestPojo"</span>/&gt;</span>
        <span class="code-tag">&lt;/dataFormats&gt;</span>
</pre>
</div></div>

<p>And then you can refer to this id in the route:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
       <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:back"</span>/&gt;</span>
            <span class="code-tag">&lt;unmarshal ref=<span class="code-quote">"jack"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:reverse"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="JSON-ExcludingPOJOfieldsfrommarshalling"></a>Excluding POJO
fields from marshalling</h3>
<p><b>As of Camel 2.10</b><br/>
When marshalling a POJO to JSON you might want to exclude certain fields from the JSON output.
With Jackson you can use <a href="http://wiki.fasterxml.com/JacksonJsonViews" class="external-link"
rel="nofollow">JSON views</a> to accomplish this. First create one or more marker
classes.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Views
{

    <span class="code-keyword">static</span> class Weight { }
    <span class="code-keyword">static</span> class Age { }
}
</pre>
</div></div>

<p>Use the marker classes with the <tt>@JsonView</tt> annotation to include/exclude
certain fields. The annotation also works on getters.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@JsonView(Views.Age.class)
<span class="code-keyword">private</span> <span class="code-object">int</span>
age = 30;

<span class="code-keyword">private</span> <span class="code-object">int</span>
height = 190;

@JsonView(Views.Weight.class)
<span class="code-keyword">private</span> <span class="code-object">int</span>
weight = 70;
</pre>
</div></div>

<p>Finally use the Camel <tt>JacksonDataFormat</tt> to marshall the above
POJO to JSON.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">JacksonDataFormat ageViewFormat = <span class="code-keyword">new</span>
JacksonDataFormat(TestPojoView.class, Views.Age.class);
from(<span class="code-quote">"direct:inPojoAgeView"</span>).marshal(ageViewFormat);
</pre>
</div></div>

<p>Note that the weight field is missing in the resulting JSON:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{<span class="code-quote">"age"</span>:30, <span class="code-quote">"height"</span>:190}
</pre>
</div></div>

<p>The GSON library supports a similar feature through the notion of <a href="http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/ExclusionStrategy.html"
class="external-link" rel="nofollow">ExclusionStrategies</a>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">/**
 * Strategy to exclude {@link ExcludeAge} annotated fields
 */
<span class="code-keyword">protected</span> <span class="code-keyword">static</span>
class AgeExclusionStrategy <span class="code-keyword">implements</span> ExclusionStrategy
{

    @Override
    <span class="code-keyword">public</span> <span class="code-object">boolean</span>
shouldSkipField(FieldAttributes f) {
        <span class="code-keyword">return</span> f.getAnnotation(ExcludeAge.class)
!= <span class="code-keyword">null</span>;
    }

    @Override
    <span class="code-keyword">public</span> <span class="code-object">boolean</span>
shouldSkipClass(<span class="code-object">Class</span>&lt;?&gt; clazz)
{
        <span class="code-keyword">return</span> <span class="code-keyword">false</span>;
    }
}
</pre>
</div></div>

<p>The <tt>GsonDataFormat</tt> accepts an <tt>ExclusionStrategy</tt>
in its constructor:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">GsonDataFormat ageExclusionFormat = <span class="code-keyword">new</span>
GsonDataFormat(TestPojoExclusion.class, <span class="code-keyword">new</span>
AgeExclusionStrategy());
from(<span class="code-quote">"direct:inPojoExcludeAge"</span>).marshal(ageExclusionFormat);
</pre>
</div></div>
<p>The line above will exclude fields annotated with <tt>@ExcludeAge</tt>
when marshalling to JSON.</p>


<h3><a name="JSON-Configuringfieldnamingpolicy"></a>Configuring field naming
policy</h3>
<p><b>Available as of Camel 2.11</b></p>

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

<p>We may have this JSON string</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
{
  <span class="code-quote">"id"</span> : 123,
  <span class="code-quote">"first_name"</span> : <span class="code-quote">"Donald"</span>
  <span class="code-quote">"last_name"</span> : <span class="code-quote">"Duck"</span>
}
</pre>
</div></div>

<p>Which we want to map to a POJO that has getter/setters as</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class PersonPojo {

    <span class="code-keyword">private</span> <span class="code-object">int</span>
id;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
firstName;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
lastName;

    <span class="code-keyword">public</span> <span class="code-object">int</span>
getId() {
        <span class="code-keyword">return</span> id;
    }

    <span class="code-keyword">public</span> void setId(<span class="code-object">int</span>
id) {
        <span class="code-keyword">this</span>.id = id;
    }

    <span class="code-keyword">public</span> <span class="code-object">String</span>
getFirstName() {
        <span class="code-keyword">return</span> firstName;
    }

    <span class="code-keyword">public</span> void setFirstName(<span class="code-object">String</span>
firstName) {
        <span class="code-keyword">this</span>.firstName = firstName;
    }

    <span class="code-keyword">public</span> <span class="code-object">String</span>
getLastName() {
        <span class="code-keyword">return</span> lastName;
    }

    <span class="code-keyword">public</span> void setLastName(<span class="code-object">String</span>
lastName) {
        <span class="code-keyword">this</span>.lastName = lastName;
    }
}
</pre>
</div></div>

<p>Then we can configure the <tt>org.apache.camel.component.gson.GsonDataFormat</tt>
in a Spring XML files as shown below. Notice we use <tt>fieldNamingPolicy</tt>
property to set the field mapping. This property is an enum from GSon <tt>com.google.gson.FieldNamingPolicy</tt>
which has a number of pre defined mappings. If you need full control you can use the property
<tt>FieldNamingStrategy</tt> and implement a custom <tt>com.google.gson.FieldNamingStrategy</tt>
where you can control the mapping.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    &lt;!-- define the gson data format, where we configure the data format using the
properties --&gt;
    &lt;bean id=<span class="code-quote">"gson"</span> class=<span class="code-quote">"org.apache.camel.component.gson.GsonDataFormat"</span>&gt;
        &lt;!-- we want to unmarshal to person pojo --&gt;
        &lt;property name=<span class="code-quote">"unmarshalType"</span>
value=<span class="code-quote">"org.apache.camel.component.gson.PersonPojo"</span>/&gt;
        &lt;!-- we want to map fields to use lower <span class="code-keyword">case</span>
and underscores --&gt;
        &lt;property name=<span class="code-quote">"fieldNamingPolicy"</span>
value=<span class="code-quote">"LOWER_CASE_WITH_UNDERSCORES"</span>/&gt;
    &lt;/bean&gt;
</pre>
</div></div>

<p>And use it in Camel routes by referring to its bean id as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   &lt;camelContext xmlns=<span class="code-quote">"http:<span class="code-comment">//camel.apache.org/schema/spring"</span>&gt;
</span>
        &lt;route&gt;
            &lt;from uri=<span class="code-quote">"direct:inPojo"</span>/&gt;
            &lt;marshal ref=<span class="code-quote">"gson"</span>/&gt;
        &lt;/route&gt;

        &lt;route&gt;
            &lt;from uri=<span class="code-quote">"direct:backPojo"</span>/&gt;
            &lt;unmarshal ref=<span class="code-quote">"gson"</span>/&gt;
        &lt;/route&gt;

    &lt;/camelContext&gt;
</pre>
</div></div>


<h3><a name="JSON-Include%2FExcludefieldsusingthe%7B%7BjsonView%7D%7Dattributewith%7B%7BJacksonDataFormat%7D%7D"></a>Include/Exclude
fields using the <tt>jsonView</tt> attribute with <tt>JacksonDataFormat</tt></h3>
<p><b>Available as of Camel 2.12</b></p>

<p>As an example of using this attribute you can instead of:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JacksonDataFormat ageViewFormat = <span class="code-keyword">new</span> JacksonDataFormat(TestPojoView.class,
Views.Age.class);
from(<span class="code-quote">"direct:inPojoAgeView"</span>).
  marshal(ageViewFormat);
</pre>
</div></div>

<p>Directly specify your <a href="http://wiki.fasterxml.com/JacksonJsonViews" class="external-link"
rel="nofollow">JSON view</a> inside the Java DSL as:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:inPojoAgeView"</span>).
  marshal().json(TestPojoView.class, Views.Age.class);
</pre>
</div></div>

<p>And the same in XML DSL:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;from uri=<span class="code-quote">"direct:inPojoAgeView"</span>/&gt;
  &lt;marshal&gt;
    &lt;json library=<span class="code-quote">"Jackson"</span> unmarshalTypeName=<span
class="code-quote">"org.apache.camel.component.jackson.TestPojoView"</span> jsonView=<span
class="code-quote">"org.apache.camel.component.jackson.Views$Age"</span>/&gt;
  &lt;/marshal&gt;
</pre>
</div></div>

<h3><a name="JSON-DependenciesforXStream"></a>Dependencies for XStream</h3>

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

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>camel-xstream<span
class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>2.9.2<span class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="JSON-DependenciesforJackson"></a>Dependencies for Jackson</h3>

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

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>camel-jackson<span
class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>2.9.2<span class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="JSON-DependenciesforGSON"></a>Dependencies for GSON</h3>

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

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>camel-gson<span class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>2.10.0<span class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/JSON">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=101227&revisedVersion=16&originalVersion=15">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/JSON?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message