Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/DesignDocs/350483"

(Bug 350483:Object to JSON Support)
(PUBLIC API)
Line 6: Line 6:
 
=='''PUBLIC API'''==
 
=='''PUBLIC API'''==
 
Standard JAXB API will be used to marshal and unmarshal.  Users will need to set a property on the Marshaller and Unmarshaller to enable JSON mode.
 
Standard JAXB API will be used to marshal and unmarshal.  Users will need to set a property on the Marshaller and Unmarshaller to enable JSON mode.
 +
<source lang="java">
 
   Unmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller();
 
   Unmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller();
 
   jsonUnmarshaller.setProperty(JAXBContext.MEDIA_TYPE, org.eclipse.persistence.oxm.MEDIA_TYPE.APPLICATION_JSON);
 
   jsonUnmarshaller.setProperty(JAXBContext.MEDIA_TYPE, org.eclipse.persistence.oxm.MEDIA_TYPE.APPLICATION_JSON);
 
    
 
    
 
   Marshaller jsonMarshaller = jaxbContext.createMarshaller();
 
   Marshaller jsonMarshaller = jaxbContext.createMarshaller();
   jsonMarshaller .setProperty(JAXBContext.MEDIA_TYPE, org.eclipse.persistence.oxm.MEDIA_TYPE.APPLICATION_JSON);
+
   jsonMarshaller.setProperty(JAXBContext.MEDIA_TYPE, org.eclipse.persistence.oxm.MEDIA_TYPE.APPLICATION_JSON);
 
+
</source>
 
New Enum class - org.eclipse.persistence.oxm.MEDIA_TYPE will be added with initial enum values  (APPLICATION_XML, APPLICATION_JSON)
 
New Enum class - org.eclipse.persistence.oxm.MEDIA_TYPE will be added with initial enum values  (APPLICATION_XML, APPLICATION_JSON)
 
  
 
=='''JAXB Annotations'''==
 
=='''JAXB Annotations'''==

Revision as of 10:08, 5 August 2011

Bug 350483:Object to JSON Support

See http://www.json.org/ for more JSON reference

To support unmarshalling JSON antlr will be used. Bug 351113.

PUBLIC API

Standard JAXB API will be used to marshal and unmarshal. Users will need to set a property on the Marshaller and Unmarshaller to enable JSON mode.

  Unmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller();
  jsonUnmarshaller.setProperty(JAXBContext.MEDIA_TYPE, org.eclipse.persistence.oxm.MEDIA_TYPE.APPLICATION_JSON);
 
  Marshaller jsonMarshaller = jaxbContext.createMarshaller();
  jsonMarshaller.setProperty(JAXBContext.MEDIA_TYPE, org.eclipse.persistence.oxm.MEDIA_TYPE.APPLICATION_JSON);

New Enum class - org.eclipse.persistence.oxm.MEDIA_TYPE will be added with initial enum values (APPLICATION_XML, APPLICATION_JSON)

JAXB Annotations

  • XmlAccessOrder
  • XmlAccessorOrder
  • XmlAccessorType
  • XmlAccessType
  • XmlAnyAttribute
  • XmlAnyElement
  • XmlAttribute
  • XmlAttributeRef
  • XmlElement
  • XmlElementWrapper
  • XmlID
  • XmlIDREF
  • XmlJavaTypeAdapter
  • XmlJavaTypeAdapters
  • XmlList
  • XmlMimeType
  • XmlMixed
  • XmlNs
  • XmlNsForm
  • XmlRootElement
  • XmlSchema
  • XmlSeeAlso
  • XmlTransient
  • XmlType
  • XmlValue

MOXy Annotations

  • XmlAccessMethods
  • XmlCDATA
  • XmlClassExtractor
  • XmlContainerPolicy
  • XmlCustomizer
  • XMLDiscriminatorNode
  • XMLDiscriminatorValue
  • XmlElementsJoinNodes
  • XmlInverseReference
  • XmlIsSetNullPolicy
  • XmlJoinNode
  • XmlJoinNodes
  • XmlKey
  • XmlMashalNullRepresentation
  • XmlNameTransformer
  • XmlNullPolicy
  • XmlParameter
  • XmlPath
  • XmlPaths
  • XmlProperties
  • XmlProperty
  • XmlReadOnly
  • XmlReadTransformer
  • XmlTransformation
  • XmlVirtualAccessMethods
  • XmlVirtualAccessMethodsSchema
  • XmlWriteOnly
  • XmlWriteTransformer
  • XmlWriteTransformers

Inheritance

XML - <prefix:vehicle xsi:type="prefix:car-type"> JSON-Can unmarshal "type":"prefix:car-type" or "type":"car-type", Should it marshal "type":"prefix:car-type" or "type":"car-type",

Namespaces

Bug 351588 By default namespaces/prefixes will be ignored during marshal and unmarshal operations. This default behavior is a problem if there are multiple mappings with the same local name in different namespaces as there would be no way to distinguish between those mappings. Users will be able to provide a Map of namespaces to customize the behavior.

 Map namespaces = new HashMap<String, String>();
 namespaces.put("ns1", "namespace1");
 namespaces.put("ns2", "namespace2");
 jsonUnmarshaller.setProperty(JAXBContext.JSON_NAMESPACES, namespaces);

If the namespace map is set on the marshaller it will be used to prefix elements during the marshal operation.

 jsonMarshaller.setProperty(JAXBContext.JSON_USE_NAMESPACES, true);

The namespaces will be give the prefix from the map separated with a `.` ie:

 {"ns0.employee:{
     "ns0.id":123
    }
 }

Date Types

XSI type attribute

ie:CompositeObjectMapping to Object.class attributes of type java.lang.Object (or Collection of Objects).

    • Equivalent XML -
  <responsibilities>
     <responsibility xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xsi:type="xsd:string">Fix Bugs</responsibility>

no "root element" support

Bug 353938 JSON supports documents with no root element

{"area-code":"613",
 "number":"1234567"}

During marshal if there is no @XmlRootElement specified then the JSON document won't have a root element (as above). During an unmarshal operation if the document has more than one child element it will be treated as an object without a root element. The only unmarshal methods that will be supported for the non root element case will be those that take a Class argument that specifies the class to unmarshal to. If there is an object with no root element that only has 1 mapped field it will marshal fine but will not unmarshal correctly. This is because the check to determine if this is a root vs. non-root case is to check if there is more than one child element at the root level. To override this behavior users will be able to specify a property on the JAXBUnmarshaller to specify that it is a non-root element case.

 jaxbUnmarshaller.setProperty(JAXBContext.JSON_HAS_ROOT_ELEMENT, false);

Attributes

JSON doesn't have the concept as attributes so by default when marshaling anything mapped as an attribute will be marshalled as an element. During unmarshal elements will trigger both the attribute and element events to allow either the mapped attribute or element to handle the value. If there is an element and attribute with the same name this will cause problems. Additionally there would likely be issues if an AnyAttribute or Any existed as all items would probably be duplicated in the AnyAttribute mapping and the Any Mapping.

Users will be able to override the default behaviors by providing a prefix to marshal with attributes and to recognize during unmarshal. In the example below the number attribute is mapped as an attribute.

 jsonUnmarshaller.setProperty(JAXBContext.JSON_ATTRIBUTE_PREFIX, "@");
 jsonMarshaller.setProperty(JAXBContext.JSON_ATTRIBUTE_PREFIX, "@") ;
{"phone":{
  "area-code":"613",
  "@number":"1234567"
  }
}

Null support

Bug 351587 When marshaling if the getMarshalNullRepresentation setting on nullpolicy is ABSENT_NODE we don't write that pair to JSON. If the getMarshalNullRepresentation is NIL we should write "null" If the getMarshalNullRepresentation is EMPTY_NODE we should write "null"

Complex Object example employee.setAddress(null);

     {"emp":{
          "address":null
       }

Complex Object example employee.setAddress(new Address());

      {"emp":{
          "address":{}
       }
     "address":{} for a complex thing 

Simple example address.setCity(null);

       {"address":{
            "city":null
          }
       }

String vs non-String values

Bug 351119 In JSON String values are in "" but not string values are not. {"address":

   "id":1,
   "city":"Ottawa",
   "isMailingAddress":true

}

Back to the top