Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: for the plan.

Jump to: navigation, search

EclipseLink/Examples/MOXy/JSON Metadata

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:


   "package-name" : "org.example.bindingsfile",
   "xml-schema" : {
      "element-form-default" : "QUALIFIED",
      "namespace" : ""
   "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"}
      } ]


<?xml version="1.0"?>
        <java-type name="Customer">
            <xml-type prop-order="firstName lastName address phoneNumbers"/>
                <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-type name="PhoneNumber">
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>

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.


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;


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


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"
   } ]


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

package org.example.bindingfile;
import java.util.*;
import javax.xml.bind.*;
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("", "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 in the same package as your domain classes with the following entry:


Copyright © Eclipse Foundation, Inc. All Rights Reserved.