Jump to: navigation, search

EclipseLink/Examples/MOXy/JSON CollectionProperties

< EclipseLink‎ | Examples‎ | MOXy
Revision as of 11:37, 5 April 2013 by Blaise.doughan.oracle.com (Talk | contribs) (New page: '''Domain Model''' By default a JAXB (JSR-222) implementation will not output a grouping element around collection data. This can be done through the use of the @XmlElementWrapper annot...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Domain Model

By default a JAXB (JSR-222) implementation will not output a grouping element around collection data. This can be done through the use of the @XmlElementWrapper annotation. This grouping element often has a plural name and is a better fit for the key of a JSON array than the repeating element defined by the @XmlElement annotation is.

package blog.json.collections;
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlType(propOrder={"name", "emailAddresses"})
public class Customer {
    private String name;
    private List<String> emailAddresses = new ArrayList<String>();
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public List<String> getEmailAddresses() {
        return emailAddresses;
    public void setEmailAddresses(List&lt'String> emailAddresses) {
        this.emailAddresses = emailAddresses;


We will specify the JSON_WRAPPER_AS_ARRAY_NAME property with a true value to tell MOXy that it should use the grouping element as the name for the JSON array value. Then we will use the same Marshaller to output the same object to both XML and JSON.

package blog.json.collections;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        // Output XML
        marshaller.marshal(customer, System.out);
        // Output JSON
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.marshal(customer, System.out);

XML Output

Below is the XML output from running the demo code. We see that email-addresses is marshalled as the grouping element which contains an email-address element for each item in the collection.

<?xml version="1.0" encoding="UTF-8"?>
   <name>Jane Doe</name>

JSON Output

The following JSON output is produced from the same metadata. The only difference is that we told MOXy to use the grouping element as the name for JSON array values.

   "customer" : {
      "name" : "Jane Doe",
      "email-addresses" : [ "jane.doe@example.com", "jdoe@example.org" ]


You can easily use MOXy as your JSON-binding provider in a JAX-RS environment. You can specify that the grouping element should be used as the JSON array name with the wrapperAsArrayName property on MOXyJsonProvider.

package blog.json.collections;
import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
public class CustomerApplication  extends Application {
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(1);
        return set;
    public Set<Object> getSingletons() {
        MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
        HashSet<Object> set = new HashSet<Object>(1);
        return set;