DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. Extending JAXB - Representing Metadata as JSON

Extending JAXB - Representing Metadata as JSON

Blaise Doughan user avatar by
Blaise Doughan
·
May. 07, 12 · Interview
Like (0)
Save
Tweet
Share
5.86K Views

Join the DZone community and get the full member experience.

Join For Free
In previous posts I have described the XML mapping document and JSON-binding extensions in EclipseLink JAXB (MOXy).   Since MOXy has a JAXB model for its mapping document we were able to eat our own dog food, and now MOXy offers a JSON mapping document.

An external metadata representation is useful when:
  • You cannot modify the domain model (it may come from a 3rd 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.

Mapping File (binding.json)

The mapping file contains the same information as the JAXB annotations.  Like with annotations you only need to specify metadata to override default behavior.  The JSON mapping file can be used in place of, or in combination with the XML mapping file.  This JSON mapping file contains the same metadata as the XML mapping file from the following post:
  • Extending JAXB - Representing Metadata as XML
    {
       "package-name" : "blog.bindingfile",
       "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"}
                ]
             }
          } ]
       }
    }

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 blog.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 blog.bindingfile;
 
public class Address {
 
    private String street;
 
    public String getStreet() {
        return street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
}
PhoneNumber
package blog.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.json)


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 Code


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

package blog.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, "blog/bindingfile/binding.json");
        properties.put("eclipselink.media-type", "application/json");
        properties.put("eclipselink.json.include-root", false);
        JAXBContext jc = JAXBContext.newInstance("blog.bindingfile", Customer.class.getClassLoader() , properties);
 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource(new File("src/blog/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);
    }
 
Specifying the MOXy JAXB Implementation (jaxb.properties)


In order to use this extension, 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

 

 

 

Download the Source Code

The source code for this post is hosted on GitHub here.  You can download the source as a zip file here.

 

 

JSON Metadata

Published at DZone with permission of Blaise Doughan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Practical Example of Using CSS Layer
  • Understanding and Solving the AWS Lambda Cold Start Problem
  • Best CI/CD Tools for DevOps: A Review of the Top 10

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: