blob: 73dfbd0718eb2979e85b77ad8ef0e610b91e79e3 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Apache Wink : 7.1 JSON</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
<tr>
<td valign="top" class="pagebody">
<div class="pageheader">
<span class="pagetitle">
Apache Wink : 7.1 JSON
</span>
</div>
<div class="pagesubheading">
This page last changed on Oct 13, 2009 by <font color="#0050B2">michael</font>.
</div>
<h3><a name="7.1JSON-JSONProviders"></a>JSON Providers</h3>
<p>Apache Wink provides a set providers that are capable of serializing a number of data models into JSON representations. There are currently 3 Apache Wink extensions that provide JSON support. Each has unique features that may make one more suitable for your particular application.</p>
<h3><a name="7.1JSON-winkjsonprovider%28org.json%29"></a>wink-json-provider (org.json)</h3>
<p>The wink-json-provider extension is provided in the binary distribution and uses the <a href="http://json.org">JSON.org</a> classes to provide JSON support. Include the wink-json-provider-&lt;VERSION&gt;.jar in the classpath and the providers will automatically be registered. You will also need the org.json JAR which is provided in the ext/wink-json-provider/lib folder.</p>
<h5><a name="7.1JSON-org.apache.wink.providers.json.JsonProvider"></a>org.apache.wink.providers.json.JsonProvider</h5>
<p>Handles reading and writing of org.json.JSONObject classes for the application/json and application/javascript media types.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>&nbsp;</th>
<th class='confluenceTh'> Supported </th>
<th class='confluenceTh'> Media Types <br clear="all" /> </th>
<th class='confluenceTh'> Entity <br clear="all" /> </th>
</tr>
<tr>
<td class='confluenceTd'> Read <br clear="all" /> </td>
<td class='confluenceTd'> Yes <br clear="all" /> </td>
<td class='confluenceTd'> application/json , application/javascript <br clear="all" /> </td>
<td class='confluenceTd'> org.json.JSONObject </td>
</tr>
<tr>
<td class='confluenceTd'> Write <br clear="all" /> </td>
<td class='confluenceTd'> Yes </td>
<td class='confluenceTd'> application/json , application/javascript <br clear="all" /> </td>
<td class='confluenceTd'> org.json.JSONObject </td>
</tr>
</tbody></table>
<h5><a name="7.1JSON-org.apache.wink.providers.json.JsonArrayProvider"></a>org.apache.wink.providers.json.JsonArrayProvider</h5>
<p>Handles reading and writing of org.json.JSONArray classes for the application/json and application/javascript media types.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>&nbsp;</th>
<th class='confluenceTh'> Supported </th>
<th class='confluenceTh'> Media Types <br clear="all" /> </th>
<th class='confluenceTh'> Entity <br clear="all" /> </th>
</tr>
<tr>
<td class='confluenceTd'> Read <br clear="all" /> </td>
<td class='confluenceTd'> Yes <br clear="all" /> </td>
<td class='confluenceTd'> application/json , application/javascript <br clear="all" /> </td>
<td class='confluenceTd'> org.json.JSONArray </td>
</tr>
<tr>
<td class='confluenceTd'> Write <br clear="all" /> </td>
<td class='confluenceTd'> Yes </td>
<td class='confluenceTd'> application/json , application/javascript <br clear="all" /> </td>
<td class='confluenceTd'> org.json.JSONArray </td>
</tr>
</tbody></table>
<h5><a name="7.1JSON-org.apache.wink.providers.json.JsonJAXBProvider"></a>org.apache.wink.providers.json.JsonJAXBProvider</h5>
<p>Handles reading and writing of JAXBElement and JAXB annotated classes for the application/json and application/javascript media types.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>&nbsp;</th>
<th class='confluenceTh'> Supported </th>
<th class='confluenceTh'> Media Types <br clear="all" /> </th>
<th class='confluenceTh'> Entity <br clear="all" /> </th>
</tr>
<tr>
<td class='confluenceTd'> Read <br clear="all" /> </td>
<td class='confluenceTd'> Yes <br clear="all" /> </td>
<td class='confluenceTd'> application/json , application/javascript <br clear="all" /> </td>
<td class='confluenceTd'> JAXB object, <br clear="all" />
JAXBElement&lt;?&gt;&gt;&gt; </td>
</tr>
<tr>
<td class='confluenceTd'> Write <br clear="all" /> </td>
<td class='confluenceTd'> Yes </td>
<td class='confluenceTd'> application/json , application/javascript <br clear="all" /> </td>
<td class='confluenceTd'> JAXB object, <br clear="all" />
JAXBElement&lt;?&gt;&gt;&gt; </td>
</tr>
</tbody></table>
<h3><a name="7.1JSON-ProducingandConsumingJSONExample"></a>Producing and Consuming JSON Example</h3>
<p>The following example demonstrates the usage of a JSON provider for reading and writing JSON representations.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
<span class="code-keyword">public</span> JSONObject postJSON(JSONObject requestJSON) {
<span class="code-object">String</span> property = requestJSON.getString(<span class="code-quote">"property"</span>);
JSONObject jobj = <span class="code-keyword">new</span> JSONObject();
<span class="code-keyword">return</span> jobj;
}
/* Book is a JAXB annotated class */
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
<span class="code-keyword">public</span> Book postJSONBook(Book requestBookEntity) {
<span class="code-object">String</span> title = requestBookEntity.getTitle();
/* other code */
Book response = <span class="code-keyword">new</span> Book();
<span class="code-keyword">return</span> response;
}
</pre>
</div></div>
<h3><a name="7.1JSON-winkjettisonprovider%28org.codehaus.jettison%29"></a>wink-jettison-provider (org.codehaus.jettison)</h3>
<p>The wink-jettison-provider extension is provided in the binary distribution and uses the <a href="http://jettison.codehaus.org/">Jettison</a> code to provide JSON support. Include the wink-jettison-provider-&lt;VERSION&gt;.jar in the classpath and the providers will automatically be registered. You will also need the Jettison library JARs which are provided in the ext/wink-jettison-provider/lib folder.</p>
<p>By default, reading is currently disabled due to potential issues with the reader. You can enable it by calling setUseAsReader(boolean) on each provider and registering as a singleton in the JAX-RS Application sub-class.</p>
<h5><a name="7.1JSON-rg.apache.wink.providers.jettison.JettisonJAX..."></a>org.apache.wink.providers.jettison.JettisonJAXBElementProvider</h5>
<p>Handles reading and writing of JAXBElement classes for the application/json media type.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>&nbsp;</th>
<th class='confluenceTh'> Supported </th>
<th class='confluenceTh'> Media Types <br clear="all" /> </th>
<th class='confluenceTh'> Entity <br clear="all" /> </th>
</tr>
<tr>
<td class='confluenceTd'> Read <br clear="all" /> </td>
<td class='confluenceTd'> Yes <br clear="all" /> </td>
<td class='confluenceTd'> application/json <br clear="all" /> </td>
<td class='confluenceTd'> JAXBElement&lt;?&gt;&gt;&gt; </td>
</tr>
<tr>
<td class='confluenceTd'> Write <br clear="all" /> </td>
<td class='confluenceTd'> Yes </td>
<td class='confluenceTd'> application/json <br clear="all" /> </td>
<td class='confluenceTd'> JAXBElement&lt;?&gt;&gt;&gt; </td>
</tr>
</tbody></table>
<h5><a name="7.1JSON-rg.apache.wink.providers.jettison.JettisonJAX..."></a>org.apache.wink.providers.jettison.JettisonJAXBElementProvider</h5>
<p>Handles reading and writing of JAXB annotated classes for the application/json media type.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>&nbsp;</th>
<th class='confluenceTh'> Supported </th>
<th class='confluenceTh'> Media Types <br clear="all" /> </th>
<th class='confluenceTh'> Entity <br clear="all" /> </th>
</tr>
<tr>
<td class='confluenceTd'> Read <br clear="all" /> </td>
<td class='confluenceTd'> Yes <br clear="all" /> </td>
<td class='confluenceTd'> application/json <br clear="all" /> </td>
<td class='confluenceTd'> JAXB object </td>
</tr>
<tr>
<td class='confluenceTd'> Write <br clear="all" /> </td>
<td class='confluenceTd'> Yes </td>
<td class='confluenceTd'> application/json <br clear="all" /> </td>
<td class='confluenceTd'> JAXB object </td>
</tr>
</tbody></table>
<h3><a name="7.1JSON-Example"></a>Example</h3>
<p>The following examples demonstrates the usage of a Jettison provider for producing and consuming JSON.</p>
<h4><a name="7.1JSON-JettisonProviderRegistration"></a>Jettison Provider Registration</h4>
<p>The following code example demonstrates the way to register a Jettison provider within a JAX-RS application.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class MyApp <span class="code-keyword">extends</span> Application {
<span class="code-keyword">public</span> Set getClasses() {
Set s = <span class="code-keyword">new</span> HashSet();
s.add(MyResource.class);
<span class="code-keyword">return</span> s;
}
<span class="code-keyword">public</span> Set&lt;<span class="code-object">Object</span>&gt; getSingletons() {
Set s = <span class="code-keyword">new</span> HashSet();
JettisonJAXBProvider jaxbProvider = <span class="code-keyword">new</span> JettisonJAXBProvider();
jaxbProvider.setUseAsReader(<span class="code-keyword">true</span>);
<span class="code-keyword">return</span> s;
}
}
</pre>
</div></div>
<h4><a name="7.1JSON-ProducingandConsumingJSON"></a>Producing and Consuming JSON</h4>
<p>The following code example demonstrates the reading and writting of JAXB objects into a JSON representation by using a Jettison provider.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/* Book is a JAXB annotated class */
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
<span class="code-keyword">public</span> Book postJSONBook(Book requestBookEntity) {
<span class="code-object">String</span> title = requestBookEntity.getTitle();
/* other code */
Book response = <span class="code-keyword">new</span> Book();
<span class="code-keyword">return</span> response;
}
</pre>
</div></div>
<h4><a name="7.1JSON-JacksonJSONProcessor"></a>Jackson JSON Processor</h4>
<p><a href="http://jackson.codehaus.org/">Jackson JSON Processor</a> may also suit your needs and can be used. They provide their own JAX-RS entity provider. See their documentation for more information.</p>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td height="12" background="http://cwiki.apache.org/confluence/images/border/border_bottom.gif"><img src="images/border/spacer.gif" width="1" height="1" border="0"/></td>
</tr>
<tr>
<td align="center"><font color="grey">Document generated by Confluence on Nov 11, 2009 06:57</font></td>
</tr>
</table>
</body>
</html>