Jump to: navigation, search

EclipseLink/Examples/MOXy/JSON Metadata

< EclipseLink‎ | Examples‎ | MOXy
Revision as of 13:27, 18 June 2012 by Rick.barkhouse.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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>