blob: 9f8760926afaad818ca54af071fc85df13e584e6 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>Specifying JSON Bindings | EclipseLink 2.7 EclipseLink MOXy</title>
<meta name="generator" content="Oracle DARB XHTML Converter (Mode = document) - Version 1.0.22 Build 1" />
<meta name="date" content="2014-06-10T10:25:33Z" />
<meta name="robots" content="noarchive" />
<meta name="doctitle" content="Specifying JSON Bindings" />
<meta name="relnum" content="Release 2.7" />
<meta name="partnum" content="" />
<link rel="stylesheet" type="text/css" href="../../dcommon/style.css" media="screen" />
<link rel="copyright" href="../../dcommon/html/cpyr.htm" title="Copyright" type="text/html" />
<link rel="start" href="../../index.htm" title="Home" type="text/html" />
<link rel="contents" href="toc.htm" title="Contents" type="text/html" />
<link rel="prev" href="json002.htm" title="Previous" type="text/html" />
<!-- START: Disqus --><script type="text/javascript"> var disqus_developer = 0; </script><!-- END: Disqus --><!-- START: Sharethis --><script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script><script type="text/javascript" src="http://s.sharethis.com/loader.js"></script> <!-- END: Sharethis --></head>
<body bgcolor="#FFFFFF"><iframe id="docheader" frameborder="0" framemargin="0" scrolling="no" src="../../dcommon/header.html"></iframe><script src="http://www.google.com/jsapi" type="text/javascript"></script><script type="text/javascript"> google.load('search', '1', {language : 'en'}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var googleAnalyticsOptions = {}; googleAnalyticsOptions['queryParameter'] = 'q'; googleAnalyticsOptions['categoryParameter'] = ''; customSearchOptions['googleAnalyticsOptions'] = googleAnalyticsOptions; var customSearchControl = new google.search.CustomSearchControl( '016171230611334810008:1hjujor5_fg', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); var options = new google.search.DrawOptions(); options.setSearchFormRoot('cse-search-form'); customSearchControl.draw('cse', options); }, true);</script><link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" /><div id="cse" style="width:100%;"></div>
<div class="header"><a id="top" name="top"></a>
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td align="left" valign="top"><div class="booktitle">Developing JAXB Applications Using EclipseLink MOXy,
<b>Release 2.7</b><br /></font></td>
<td valign="bottom" align="right" width="144">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>&nbsp;</td>
<td align="center" valign="top"><a href="toc.htm"><img src="../../dcommon/images/contents.png" alt="Go To Table Of Contents" border="0" height="16" width="16" /><br />
</td><td>&nbsp;</td><td align="center"><a href="../../" target="_top" class="external text" title="Search" rel="nofollow"><img src="../../dcommon/images/search.png" alt="Search" style="border:0;" /><br /><span class="mini"></span></a></td><td>&nbsp;</td><td align="center"><a href="../eclipselink_moxy.pdf" title="PDF" target="_blank"><img src="../../dcommon/images/pdf_icon.png" style="padding-right:5px;border:0" alt="PDF"></a></td>
</tr>
</table>
</td>
</tr>
</table>
<hr />
<table class="navigation simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100" align="center">
<tr>
<td align="center"><a href="json002.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<!-- class="header" -->
<div class="ind"><!-- End Header --><a id="BABEADAH" name="BABEADAH"></a><a id="TLJAX471" name="TLJAX471"></a>
<div class="sect1"><!-- infolevel="all" infotype="General" -->
<h1 class="sect1"><font face="arial, helvetica, sans-serif" color="#330099">Specifying JSON Bindings</font></h1>
<p><a href="#BABICGHC">Example 10-4</a> shows a basic JSON binding that does not require compile time dependencies in addition to those required for normal JAXB usage. This example shows how to unmarshal JSON from a <code>StreamSource</code> into the user object <code>SearchResults</code>, add a new <code>Result</code> to the collection, and then marshal the new collection to <code>System.out</code>.</p>
<div class="example"><a id="BABICGHC" name="BABICGHC"></a><a id="TLJAX472" name="TLJAX472"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-4 Using Basic JSON Binding</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
package org.example;
import org.example.model.Result;
import org.example.model.SearchResults;
import java.util.Date;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(SearchResults.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty("eclipselink.media-type", "application/json");
StreamSource source = new StreamSource("http://search.twitter.com/search.json?q=jaxb");
JAXBElement&lt;SearchResults&gt; jaxbElement = unmarshaller.unmarshal(source, SearchResults.class);
Result result = new Result();
result.setCreatedAt(new Date());
result.setFromUser("bsmith");
result.setText("You can now use EclipseLink JAXB (MOXy) with JSON :)");
jaxbElement.getValue().getResults().add(result);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.marshal(jaxbElement, System.out);
}
}
</pre></div>
<!-- class="example" -->
<p>You can also write MOXy External Bindings files as JSON documents. <a href="#BABBEFHI">Example 10-5</a> shows how to use <code>bindings.json</code> to map <strong>Customer</strong> and <strong>PhoneNumber</strong> classes to JSON.</p>
<div class="example"><a id="BABBEFHI" name="BABBEFHI"></a><a id="TLJAX473" name="TLJAX473"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-5 Using External Bindings</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
{
"package-name" : "org.example",
"xml-schema" : {
"element-form-default" : "QUALIFIED",
"namespace" : "http://www.example.com/customer"
},
"java-types" : {
"java-type" : [ {
"name" : "Customer",
"xml-type" : {
"prop-order" : "firstName lastName address phoneNumbers"
},
"xml-root-element" : {},
"java-attributes" : {
"xml-element" : [
{"java-attribute" : "firstName","name" : "first-name"},
{"java-attribute" : "lastName", "name" : "last-name"},
{"java-attribute" : "phoneNumbers","name" : "phone-number"}
]
}
}, {
"name" : "PhoneNumber",
"java-attributes" : {
"xml-attribute" : [
{"java-attribute" : "type"}
],
"xml-value" : [
{"java-attribute" : "number"}
]
}
} ]
}
}
</pre></div>
<!-- class="example" -->
<p><a href="#BABBAADH">Example 10-6</a> shows how to use the JSON file (created in <a href="#BABBEFHI">Example 10-5</a>) when bootstrapping a <code>JAXBContext</code>.</p>
<div class="example"><a id="BABBAADH" name="BABBAADH"></a><a id="TLJAX474" name="TLJAX474"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-6 Using JSON to Bootstrap a JAXBContext</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;(2);
properties.put("eclipselink-oxm-xml", "org/example/binding.json");
properties.put("eclipselink.media-type", "application/json");
JAXBContext context = JAXBContext.newInstance("org.example", Customer.class.getClassLoader() , properties);
Unmarshaller unmarshaller = context.createUnmarshaller();
StreamSource json = new StreamSource(new File("src/org/example/input.json"));
...
</pre></div>
<!-- class="example" -->
<a id="TLJAX477" name="TLJAX477"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref205" name="sthref205"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Specifying JSON Data Types</font></h2>
<p>Although XML has a single datatype, JSON differentiates between strings, numbers, and booleans. EclipseLink supports these datatypes automatically, as shown in <a href="#BABGFFEH">Example 10-7</a></p>
<div class="example"><a id="BABGFFEH" name="BABGFFEH"></a><a id="TLJAX478" name="TLJAX478"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-7 Using JSON Data Types</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
public class Address {
private int id;
private String city;
private boolean isMailingAddress;
}
</pre>
<pre xml:space="preserve" class="oac_no_warn">
{
"id" : 1,
"city" : "Ottawa",
"isMailingAddress" : true
}
</pre></div>
<!-- class="example" --></div>
<!-- class="sect2" -->
<a id="TLJAX479" name="TLJAX479"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref206" name="sthref206"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Supporting Attributes</font></h2>
<p>JSON does not use attributes; anything mapped with a <code>@XmlAttribute</code> annotation will be marshalled as an element. By default, EclipseLink triggers <em>both</em> the attribute and element events, thereby allowing either the mapped attribute or element to handle the value.</p>
<p>You can override this behavior by using the <code>JSON_ATTRIBUTE_PREFIX</code> property to specify an attribute prefix, as shown in <a href="#BABEFCED">Example 10-8</a>. EclipseLink prepends the prefix to the attribute name during marshal and will recognize it during unmarshal.</p>
<p>In the example below the <code>number</code> field is mapped as an attribute with the prefix <strong>@</strong>.</p>
<div class="example"><a id="BABEFCED" name="BABEFCED"></a><a id="TLJAX480" name="TLJAX480"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-8 Using a Prefix</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, "<strong>@</strong>");
jsonMarshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, "<strong>@</strong>") ;
</pre>
<pre xml:space="preserve" class="oac_no_warn">
{
"phone" : {
"area-code" : "613",
"<strong>@number</strong>" : "1234567"
}
}
</pre></div>
<!-- class="example" -->
<p>You can also set the <code>JSON_ATTRIBUTE_PREFIX</code> property in the Map used when creating the <code>JAXBContext</code>, as shown in <a href="#BABGGBJD">Example 10-9</a>. All marshallers and unmarshalers created from the context will use the specified prefix.</p>
<div class="example"><a id="BABGGBJD" name="BABGGBJD"></a><a id="TLJAX481" name="TLJAX481"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-9 Setting a Prefix in a Map</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
JAXBContext ctx = JAXBContext.newInstance(new Class[] { Phone.class }, properties);
</pre></div>
<!-- class="example" --></div>
<!-- class="sect2" -->
<a id="TLJAX482" name="TLJAX482"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref207" name="sthref207"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Supporting no Root Element</font></h2>
<p>EclipseLink supports JSON documents without a root element. By default, if no <code>@XmlRootElement</code> annotation exists, the marshalled JSON document will not have a root element. You can override this behavior (that is omit the root element from the JSON output, even if the <code>@XmlRootElement</code> <em>is</em> specified) by setting the <code>JSON_INCLUDE_ROOT</code> property when marshalling a document, as shown in <a href="#BABECFAI">Example 10-10</a>.</p>
<div class="example"><a id="BABECFAI" name="BABECFAI"></a><a id="TLJAX483" name="TLJAX483"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-10 Marshalling no Root Element Documents</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
</pre></div>
<!-- class="example" -->
<p>When unmarshaling a document with no root elements, you should set the <code>JSON_INCLUDE_ROOT</code> property as shown in <a href="#BABECFAI">Example 10-10</a>.</p>
<div class="example"><a id="TLJAX484" name="TLJAX484"></a><a id="sthref208" name="sthref208"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-11 Unmarshalling no Root Element Documents</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
JAXBElement&lt;SearchResults&gt; jaxbElement = unmarshaller.unmarshal(source, SearchResults.class);
</pre></div>
<!-- class="example" -->
<div align="center">
<div class="inftblnote"><br />
<table class="Note oac_no_warn" summary="" border="1" width="80%" frame="hsides" rules="groups" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<td align="left">
<p class="note"><img src="../../dcommon/images/note_icon.png" width="16" height="16" alt="Note" style="vertical-align:middle;padding-right:5px;" />Note:</p>
<p>If the document has no root element, you must specify the class to unmarshal to.</p>
</td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblnote" --></div>
</div>
<!-- class="sect2" -->
<a id="TLJAX485" name="TLJAX485"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref209" name="sthref209"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Using Namespaces</font></h2>
<p>Because JSON does not use namespces, by default all namespaces and prefixes are ignored when marshaling and unmarshaling. In some cases, this may be an issue if you have multiple mappings with the same local name &ndash; there will be no way to distinguish between the mappings.</p>
<p>With EclipseLink, you can supply a Map of namespace-to-prefix (or an instance of <code>NamespacePrefixMapper</code>) to the Marshaller and Unmarshaller. The namespace prefix will appear in the marshalled document prepended to the element name. EclipseLink will recognize the prefix during an unmarshal operation and the resulting Java objects will be placed in the proper namespaces.</p>
<p><a href="#BABEAJFJ">Example 10-12</a> shows how to use the <code>NAMESPACE_PREFIX_MAPPER</code> property.</p>
<div class="example"><a id="BABEAJFJ" name="BABEAJFJ"></a><a id="TLJAX486" name="TLJAX486"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-12 Using Namesapces</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
Map&lt;String, String&gt; namespaces = new HashMap&lt;String, String&gt;();
namespaces.put("namespace1", "ns1");
namespaces.put("namespace2", "ns2");
jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces);
</pre></div>
<!-- class="example" -->
<p>The <code>MarshallerProperties.NAMESPACE_PREFIX_MAPPER</code> applies to <em>both</em> XML and JSON; <code>UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER</code> is a <em>JSON-only</em> property. XML unmarshalling can obtain the namespace information directly from the document.</p>
<p>When JSON is marshalled, the namespaces will be given the prefix from the Map separated by a dot (&nbsp;<strong>.</strong>&nbsp;):</p>
<pre xml:space="preserve" class="oac_no_warn">
{
"ns1.employee : {
"ns2.id" : 123
}
}
</pre>
<p>The dot separator can be set to any custom character by using the <code>JSON_NAMESPACE_SEPARATOR</code> property. Here, a colon (&nbsp;<strong>:</strong>&nbsp;) will be used instead:</p>
<pre xml:space="preserve" class="oac_no_warn">
jsonMarshaller.setProperty(MarshallerProperties.JSON_NAMESPACE_SEPARATOR, ':');
jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR, ':');
</pre></div>
<!-- class="sect2" -->
<a id="TLJAX487" name="TLJAX487"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref210" name="sthref210"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Using Collections</font></h2>
<p>By default, when marshalling to JSON, EclipseLink marshals empty collections as <code>[&nbsp;]</code>, as shown in <a href="#BABFJGGE">Example 10-13</a>.</p>
<div class="example"><a id="BABFJGGE" name="BABFJGGE"></a><a id="TLJAX488" name="TLJAX488"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-13</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
{
"phone" : {
"myList" : [ ]
}
}
</pre></div>
<!-- class="example" -->
<p>Use the <code>JSON_MARSHAL_EMPTY_COLLECTIONS</code> property to override this behavior (so that empty collections are not marshalled at all).</p>
<pre xml:space="preserve" class="oac_no_warn">
jsonMarshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE) ;
</pre>
<pre xml:space="preserve" class="oac_no_warn">
{
"phone" : {
}
}
</pre></div>
<!-- class="sect2" -->
<a id="TLJAX489" name="TLJAX489"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref211" name="sthref211"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Mapping Root-Level Collections</font></h2>
<p>If you use the <code>@XmlRootElement(name="root")</code> annotation to specify a root level, the JSON document can be marshaled as:</p>
<pre xml:space="preserve" class="oac_no_warn">
marshaller.marshal(myListOfRoots, System.out);
</pre>
<pre xml:space="preserve" class="oac_no_warn">
[ {
"root" : {
"name" : "aaa"
}
}, {
"root" : {
"name" : "bbb"
}
} ]
</pre>
<p>Because the root element <em>is</em> present in the document, you can unmarsal it using:</p>
<pre xml:space="preserve" class="oac_no_warn">
unmarshaller.unmarshal(json);
</pre>
<p>If the class <em>does not</em> have an <code>@XmlRootElement</code> (or if <code>JSON_INCLUDE_ROOT</code> = false), the marshal would produce:</p>
<pre xml:space="preserve" class="oac_no_warn">
[ {
"name":"aaa"
}, {
"name":"bbb"
} ]
</pre>
<p>Because the root element <em>is not</em> present, you must indicate the class to unmarshal to:</p>
<pre xml:space="preserve" class="oac_no_warn">
unmarshaller.unmarshal(json, Root.class);
</pre></div>
<!-- class="sect2" -->
<a id="TLJAX490" name="TLJAX490"></a>
<div class="sect2"><!-- infolevel="all" infotype="General" --><a id="sthref212" name="sthref212"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Wrapping XML Values</font></h2>
<p>JAXB supports one or more <code>@XmlAttributes</code> on <code>@XmlValue classes</code>, as shown in <a href="#BABCDADE">Example 10-14</a></p>
<div class="example"><a id="BABCDADE" name="BABCDADE"></a><a id="TLJAX491" name="TLJAX491"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-14 Using @XmlAttributes</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
public class Phone {
@XmlValue
public String number;
@XmlAttribute
public String areaCode;
public Phone() {
this("", "");
}
public Phone(String num, String code) {
this.number = num;
this.areaCode = code;
}
}
</pre></div>
<!-- class="example" -->
<p>To produce a valid JSON document, EclipseLink uses a <code>value</code> wrapper, as shown in <a href="#BABGIAAJ">Example 10-15</a>.</p>
<div class="example"><a id="BABGIAAJ" name="BABGIAAJ"></a><a id="TLJAX492" name="TLJAX492"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-15 Using a value Wrapper</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
{
"employee" : {
"name" : "Bob Smith",
"mainPhone" : {
"areaCode" : "613",
"value" : "555-5555"
},
"otherPhones" : [ {
"areaCode" : "613",
"value" : "123-1234"
}, {
"areaCode" : "613",
"value" : "345-3456"
} ]
}
}
</pre></div>
<!-- class="example" -->
<p>By default, EclipseLink uses <strong>value</strong> as the name of the wrapper. Use the <code>JSON_VALUE_WRAPPER</code> property to customize the name of the value wrapper, as shown in <a href="#BABBHBFB">Example 10-16</a>.</p>
<div class="example"><a id="BABBHBFB" name="BABBHBFB"></a><a id="TLJAX493" name="TLJAX493"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-16</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
jsonMarshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, "$");
jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_VALUE_WRAPPER, "$");
</pre>
<p>Would produce:</p>
<pre xml:space="preserve" class="oac_no_warn">
{
"employee" : {
"name" : "Bob Smith",
"mainPhone" : {
"areaCode" : "613",
"$" : "555-5555"
},
"otherPhones" : [ {
"areaCode" : "613",
"$" : "123-1234"
}, {
"areaCode" : "613",
"$" : "345-3456"
} ]
}
}
</pre></div>
<!-- class="example" -->
<p>You can also specify the <code>JSON_VALUE_WRAPPER</code> property in the <code>Map</code> of the properties used when you create the <code>JAXBContext</code>, as shown in <a href="#BABCBAFB">Example 10-17</a>.</p>
<div class="example"><a id="BABCBAFB" name="BABCBAFB"></a><a id="TLJAX494" name="TLJAX494"></a>
<p><strong><em><font face="arial, helvetica, sans-serif">Example 10-17 Using a Map</font></em></strong></p>
<pre xml:space="preserve" class="oac_no_warn">
Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
properties.put(JAXBContextProperties.JSON_VALUE_WRAPPER, "$");
JAXBContext ctx = JAXBContext.newInstance(new Class[] { Employee.class }, properties);
Marshaller jsonMarshaller = ctx.createMarshaller();
Unmarshaller jsonUnmarshaller = ctx.createUnmarshaller();
</pre></div>
<!-- class="example" -->
<p>When specified in a Map, the Marshallers and Unmarshallers created from the <code>JAXBContent</code> will automatically use the specified value wrapper.</p>
</div>
<!-- class="sect2" --></div>
<!-- class="sect1" --></div>
<!-- class="ind" -->
<!-- Start Footer -->
<div class="footer">
<hr />
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<col width="33%" />
<col width="*" />
<col width="33%" />
<tr>
<td valign="bottom">
<table class="navigation simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100" align="center">
<col width="*" />
<col width="48%" />
<col width="48%" />
<tr>
<td>&nbsp;</td>
<td align="center"><a href="json002.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
</tr>
</table>
</td>
<td align="center" width="34%"><a href="http://www.eclipse.org/eclipselink/" title="EclipseLink home"><img src="../../dcommon/images/ellogo.png" alt="EclipseLink" width="150" border="0" /></a><br />
<td valign="bottom" align="right">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="225">
<tr>
<td>&nbsp;</td>
<td align="center" valign="top"><a href="toc.htm"><img src="../../dcommon/images/contents.png" alt="Go To Table Of Contents" border="0" height="16" width="16" /><br />
</td><td>&nbsp;</td><td align="center"><a href="../../" target="_top" class="external text" title="Search" rel="nofollow"><img src="../../dcommon/images/search.png" alt="Search" style="border:0;" /><br /><span class="mini"></span></a></td><td>&nbsp;</td><td align="center"><a href="../eclipselink_moxy.pdf" title="PDF" target="_blank"><img src="../../dcommon/images/pdf_icon.png" style="padding-right:5px;border:0" alt="PDF"></a></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!-- class="footer" -->
<div id="copyright">Copyright &copy; 2013 by The Eclipse Foundation under the <a href="http://www.eclipse.org/org/documents/epl-v10.php">Eclipse Public License (EPL)</a><br /> <script type="text/javascript">var LastUpdated = document.lastModified;document.writeln ("Updated: " + LastUpdated);</script> </div><!-- START: Analytics --><script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1608008-2']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script><!-- END: Analytics --><!-- START: Sharethis --><script>var options={ "publisher": "e2fe9e07-fab6-4f84-83ea-0991b429842c", "position": "right", "ad": { "visible": false, "openDelay": 5, "closeDelay": 0}};var st_hover_widget = new sharethis.widgets.hoverbuttons(options);</script><!-- END: Sharethis --></body>
</html>