Permalink
Browse files

Initial check-in

  • Loading branch information...
0 parents commit be3260d242336716d7e1287d42af322aba4debac @bdoughan committed Apr 26, 2012
@@ -0,0 +1,59 @@
+Extending JAXB - Representing Metadata as JSON
+==============================================
+
+This is the complete source code for the following blog post:
+
+* http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html
+
+Summary
+-------
+
+In EclipseLink 2.4 the MOXY component includes support for representing the mapping metadata as JSON. Now you can have a binding file like:
+
+ {
+ "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"}
+ ]
+ }
+ } ]
+ }
+ }
+
+Compile the Example
+-------------------
+
+You can compile the example code using the following command. Maven will automatically fetch the required dependencies.
+
+ mvn compile
+
+Run the Example
+---------------
+
+You can run the example using the following command.
+
+ mvn exec:java
44 pom.xml
@@ -0,0 +1,44 @@
+<project
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.bdoughan.blog</groupId>
+ <artifactId>blog20120418</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0</version>
+ <name>Extending JAXB - Representing Metadata as JSON</name>
+ <url>http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html</url>
+ <dependencies>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>eclipselink</artifactId>
+ <version>2.4.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <id>EclipseLink Repo</id>
+ <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
+ </repository>
+ </repositories>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.2.1</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>java</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <mainClass>blog.bindingfile.Demo</mainClass>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,15 @@
+package blog.bindingfile;
+
+public class Address {
+
+ private String street;
+
+ public String getStreet() {
+ return street;
+ }
+
+ public void setStreet(String street) {
+ this.street = street;
+ }
+
+}
@@ -0,0 +1,44 @@
+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;
+ }
+
+}
@@ -0,0 +1,27 @@
+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/main/resources/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);
+ }
+
+}
@@ -0,0 +1,24 @@
+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;
+ }
+
+}
@@ -0,0 +1,33 @@
+{
+ "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"}
+ ]
+ }
+ } ]
+ }
+}
@@ -0,0 +1,14 @@
+{
+ "first-name" : "Jane",
+ "last-name" : "Doe",
+ "address" : {
+ "street" : "123 A Street"
+ },
+ "phone-number" : [ {
+ "type" : "work",
+ "value" : "555-1111"
+ }, {
+ "type" : "cell",
+ "value" : "555-2222"
+ } ]
+}
@@ -0,0 +1 @@
+javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

0 comments on commit be3260d

Please sign in to comment.