Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB Eclipse Link Moxy: ClassCastException while Unmarshalling JSON using schema validation
    primarykey
    data
    text
    <p>I am using eclipse link(v2.5.1) Dynamic JAXB to convert XML to JSON and viceversa using a multiple schemas. While umarshalling the JSON to XML,I want to validate the JSON(against the XSD) and Dynamic Moxy is unable to validate the generated JSON and results in classcast exception.</p> <p>emp.xsd</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:emp="Employee:2:0" targetNamespace="Employee:2:0" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="2.0"&gt; &lt;xsd:element name="searchManager" type="emp:SearchManager" /&gt; &lt;xsd:complexType name="SearchManager"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="CompanyName" type="xsd:string" /&gt; &lt;xsd:element name="objects" type="emp:Employee" minOccurs="0" maxOccurs="unbounded" /&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Employee"&gt; &lt;xsd:complexContent&gt; &lt;xsd:extension base="emp:Organization"&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="EmpId" type="xsd:string" minOccurs="0" /&gt; &lt;/xsd:sequence&gt; &lt;/xsd:extension&gt; &lt;/xsd:complexContent&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Projects"&gt; &lt;xsd:complexContent&gt; &lt;xsd:extension base="emp:Organization"/&gt; &lt;/xsd:complexContent&gt; &lt;/xsd:complexType&gt; &lt;xsd:complexType name="Organization"&gt; &lt;xsd:annotation&gt; &lt;xsd:documentation&gt;Abstract base class &lt;/xsd:documentation&gt; &lt;/xsd:annotation&gt; &lt;/xsd:complexType&gt; &lt;/xsd:schema&gt; </code></pre> <p>Manager.xsd</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema targetNamespace="Manager:1:0" xmlns:emp="Employee:2:0" xmlns:manager="Manager:1:0" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.0"&gt; &lt;!-- schema imports --&gt; &lt;xs:import namespace="Employee:2:0" schemaLocation="emp.xsd" /&gt; &lt;xs:complexType name="Manager"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt; Definition of class Employee &lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="emp:Employee"&gt; &lt;xs:sequence&gt; &lt;xs:element name="teamSize" type="xsd:int" minOccurs="0" /&gt; &lt;xs:element name="project1" type="manager:Project1" minOccurs="0" maxOccurs="unbounded" /&gt; &lt;/xs:sequence&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Project1"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="manager:Developement"&gt; &lt;xs:sequence&gt; &lt;xs:element name="type" type="xsd:int" minOccurs="0" /&gt; &lt;/xs:sequence&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Developement"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt; Abstract base class for an Development &lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="emp:Projects"/&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>sample.xml</p> <pre><code>&lt;emp:searchManager xmlns:emp="Employee:2:0" xmlns:manager="Manager:1:0"&gt; &lt;CompanyName&gt;Test&lt;/CompanyName&gt; &lt;objects xmlns:ns2="Manager:1:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Manager"&gt; &lt;EmpId&gt;123456&lt;/EmpId&gt; &lt;teamSize&gt;10&lt;/teamSize&gt; &lt;project1&gt; &lt;type&gt;1&lt;/type&gt; &lt;/project1&gt; &lt;/objects&gt; &lt;/emp:searchManager&gt; </code></pre> <p>The driver program to test is as follows</p> <pre><code>public class XMLToJSON { /** * @param args */ public static void main(String[] args) { FileInputStream xsdInputStream; try { xsdInputStream = new FileInputStream("Manager.xsd"); DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, new MyEntityResolver(), null, null); FileInputStream xmlInputStream = new FileInputStream("sample.xml"); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement&lt;DynamicEntity&gt; manager = (JAXBElement) unmarshaller.unmarshal(xmlInputStream); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //input xml to print marshaller.marshal(manager, System.out); Map namespaces = new HashMap(); namespaces.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); namespaces.put("Employee:2:0", "ns1"); namespaces.put("Manager:1:0", "ns2"); // XML to JSON marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces); FileOutputStream jsonOutputStream = new FileOutputStream("sample.json"); marshaller.marshal(manager, jsonOutputStream); marshaller.marshal(manager, System.out); //JSON to XML JAXBUnmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller(); jsonUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces); jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true); jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, "@"); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("Manager.xsd")); jsonUnmarshaller.setSchema(schema); StreamSource json = new StreamSource("sample.json"); JAXBElement&lt;DynamicEntity&gt; myroot = (JAXBElement) jsonUnmarshaller.unmarshal(json); Marshaller m = jaxbContext.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); m.marshal(myroot, System.out); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>Generated Sample JSON</p> <pre><code> { "ns1.searchManager" : { "CompanyName" : "Test", "objects" : [ { "xsi.type" : "ns2.Manager", "EmpId" : "123456", "teamSize" : 10, "project1" : [ { "type" : 1 } ] } ] } } </code></pre> <p>While unmarshalling the below exception was seen</p> <pre><code>Exception in thread "main" java.lang.ClassCastException: org.eclipse.persistence.internal.oxm.record.XMLReaderAdapter$ExtendedContentHandlerAdapter cannot be cast to org.eclipse.persistence.internal.oxm.record.UnmarshalRecord at org.eclipse.persistence.internal.oxm.record.json.JSONReader.isTextValue(JSONReader.java:454) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:350) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:244) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:430) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:299) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:166) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:125) at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:140) at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:778) at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:666) at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:593) at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:287) at XMLToJSON.main(XMLToJSON.java:74) </code></pre> <p>EDIT:Corrected the generated sample json</p> <p>Question</p> <p>The intention is to check whether JSON conforms to the XML schema. Does Dynamic JAXB support JSON Validation while unmarshalling? Is this a bug in Dynamic JAXB Moxy?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload