Skip to content

Commit

Permalink
Initial check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
bdoughan committed Apr 26, 2012
0 parents commit be3260d
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
@@ -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 changes: 44 additions & 0 deletions 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>
15 changes: 15 additions & 0 deletions src/main/java/blog/bindingfile/Address.java
@@ -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;
}

}
44 changes: 44 additions & 0 deletions src/main/java/blog/bindingfile/Customer.java
@@ -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;
}

}
27 changes: 27 additions & 0 deletions src/main/java/blog/bindingfile/Demo.java
@@ -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);
}

}
24 changes: 24 additions & 0 deletions src/main/java/blog/bindingfile/PhoneNumber.java
@@ -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;
}

}
33 changes: 33 additions & 0 deletions src/main/resources/blog/bindingfile/binding.json
@@ -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"}
]
}
} ]
}
}
14 changes: 14 additions & 0 deletions src/main/resources/blog/bindingfile/input.json
@@ -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"
} ]
}
1 change: 1 addition & 0 deletions src/main/resources/blog/bindingfile/jaxb.properties
@@ -0,0 +1 @@
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

0 comments on commit be3260d

Please sign in to comment.