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

EclipseLink/DesignDocs/459464

Redesign of type property in JSON processing

Bug 459464

Document History

Date Author Version Description & Notes
February 9th 2015 Martin Vojtek Initial revision

Overview

Special handling of JSON type property is deprecated. If there is need to identify type of JSON object - due to missing root element or some special inheritance requirements, it is necessary to specify fully qualified type property with http://www.w3.org/2001/XMLSchema-instance namespace.

Namespace prefix needs to be specified as JAXBContext property in Marshaller or Unmarshaller:

 unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
 Map<String, String> namespaces = new HashMap<>();
 namespaces.put(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi");
 unmarshaller.setProperty(JAXBContextProperties.NAMESPACE_PREFIX_MAPPER, namespaces);

Json in MOXy 2.5.x:

 {
   "person": {
     "type": "developer",
     "name": "Martin Vojtek"
   }
 }

Json in MOXy 2.6.0:

 {
   "person": {
     "xsi.type": "developer",
     "name": "Martin Vojtek"
   }
 }

There is also change in default handling of simple xsd types when using namespaces. To specify simple xsd type as type value, there is no need to specify xsd prefix. Example:

Json in MOXy 2.5.x:

 {
   "foo": {
     "field": {
       "xsi.type": "xsd.int",
       "value": 10
     }
   }
 }

Json in MOXy 2.6.0:

 {
   "foo": {
     "field": {
       "xsi.type": "int",
       "value": 10
     }
   }
 }  

Backward Compatibility

To provide backward compatibility options, new system properties and JAXBContext/Marshaller/Unmarshaller properties are introduced.

System properties:

org.eclipse.persistence.json.type-compatibility

org.eclipse.persistence.json.use-xsd-types-prefix

JAXBContext properties:

 JSON_USE_XSD_TYPES_WITH_PREFIX = "eclipselink.json.use-xsd-types-with-prefix"
 JSON_TYPE_COMPATIBILITY = "eclipselink.json.type-compatibility"

When migrating from 2.5.x to 2.6, the simplest way is to set system property org.eclipse.persistence.json.type-compatibility to true. Alternatively there is option to set this property for Marshaller or Unmarshaller:

 JAXBContext context = JAXBContext.newInstance(Foo.class);
 Unmarshaller unmarshaller = context.createUnmarshaller();
 unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
 unmarshaller.setProperty(JAXBContextProperties.JSON_TYPE_COMPATIBILITY, true);
 Marshaller marshaller = context.createMarshaller();
 marshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
 marshaller.setProperty(JAXBContextProperties.JSON_TYPE_COMPATIBILITY, true);

To override default behavior for simple xsd types, set JAXBContextProperties.JSON_USE_XSD_TYPES_WITH_PREFIX property to true.

Back to the top