Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/JSON Metadata"

 
(One intermediate revision by the same user not shown)
Line 223: Line 223:
 
The JSON bindings file is passed in via the '''properties''' parameter when the '''JAXBContext''' is instantiated.
 
The JSON bindings file is passed in via the '''properties''' parameter when the '''JAXBContext''' is instantiated.
  
<div style="width:950px">
+
<div style="width:1000px">
 
<source lang="java">
 
<source lang="java">
 
package org.example.bindingfile;
 
package org.example.bindingfile;
Line 254: Line 254:
 
</source>
 
</source>
 
</div>
 
</div>
 +
  
 
In order to use this feature, you must use MOXy as your JAXB provider.  To enable MOXy, simply add a file named '''jaxb.properties''' in the same package as your domain classes with the following entry:
 
In order to use this feature, you must use MOXy as your JAXB provider.  To enable MOXy, simply add a file named '''jaxb.properties''' in the same package as your domain classes with the following entry:

Latest revision as of 13:46, 18 June 2012

Representing MOXy Metadata in JSON

MOXy's support for JSON brings with it the ability to write MOXy External Bindings files in JSON. An external metadata representation is useful when:

  • You cannot modify the domain model (it may come from a third party)
  • You do not want to introduce compile dependencies on JAXB APIs (if you are using a version of Java prior to Java SE 6)
  • You want to apply multiple JAXB mappings to a domain model (you are limited to one representation with annotations)
  • Your object model already contains so many annotations from other technologies that adding more would make the class unreadable


Bindings File

The bindings file contains the same information as the JAXB annotations. Like with annotations you only need to specify metadata to override default behavior. The JSON bindings file can be used in place of, or in combination with, an XML bindings file. Below is an example JSON bindings file, as well as its XML equivalent:

bindings.json

{
   "package-name" : "org.example.bindingsfile",
   "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"}
            ]
         }
      } ]
   }
}

bindings.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="org.example.bindingsfile">
    <xml-schema
        namespace="http://www.example.com/customer"
        element-form-default="QUALIFIED"/>
    <java-types>
        <java-type name="Customer">
            <xml-root-element/>
            <xml-type prop-order="firstName lastName address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="firstName" name="first-name"/>
                <xml-element java-attribute="lastName" name="last-name"/>
                <xml-element java-attribute="phoneNumbers" name="phone-number"/>
            </java-attributes>
        </java-type>
        <java-type name="PhoneNumber">
            <java-attributes>
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>


Domain Model

The following domain model will be used in this example. Because the JAXB metadata is represented as JSON, no annotations are used on the classes.


Customer

package org.example.bindingfile;
 
import java.util.List;
 
public class Customer {
 
    private String firstName;
    private String lastName;
    private Address address;
    private List<PhoneNumber> phoneNumbers;
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
}


Address

package org.example.bindingfile;
 
public class Address {
 
    private String street;
 
    public String getStreet() {
        return street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
}


PhoneNumber

package org.example.bindingfile;
 
public class PhoneNumber {
 
    private String type;
    private String number;
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
 
}


JSON Input

The following JSON input will be used in this example:

{
   "first-name" : "Jane",
   "last-name" : "Doe",
   "address" : {
      "street" : "123 A Street"
   },
   "phone-number" : [ {
      "type" : "work",
      "value" : "555-1111"
   }, {
      "type" : "cell",
      "value" : "555-2222"
   } ]
}


Demo

The JSON bindings file is passed in via the properties parameter when the JAXBContext is instantiated.

package org.example.bindingfile;
 
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(3);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "org/example/bindingfile/bindings.json");
        properties.put("eclipselink.media-type", "application/json");
        properties.put("eclipselink.json.include-root", false);
        JAXBContext jc = JAXBContext.newInstance("org.example.bindingfile", Customer.class.getClassLoader() , properties);
 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource(new File("src/org/example/bindingfile/input.json"));
        Customer customer = (Customer) unmarshaller.unmarshal(json, Customer.class).getValue();
 
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }
 
}


In order to use this feature, you must use MOXy as your JAXB provider. To enable MOXy, simply add a file named jaxb.properties in the same package as your domain classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory