How to use XmlGregorianCalendar with json and toJson methods for gson?

The theme of my project is to provide an XML data format and get a Json format using google-gson, and my JAXB generated java-POJO from an XML schema in which I have a variable of type XMLGregorianCalendar.

I give the following XML input and get the json format from the gson.toJson () method;

<?xml version="1.0" encoding="UTF-8"?> <EmpRequest xmlns="http://java.com/Employee"> <EmplIn> <EmpID>12</EmpID> <Empname>sara</Empname> <Designation>SA</Designation> <DOJ>2002-05-30T09:30:10+06:00</DOJ> </EmplIn> </EmpRequest> 

But at the output, I got the following.

 {"emplIn":{"empID":"12","empname":"sara","designation":"SA","doj":{}}} 

I looked at google and got a suggestion to add to the xml schema and change the XmlGregorianCalendar data type with a string. But I do not want to achieve this from both paths.

I mean, how to get the correct output using the XmlGregorianCalendar data type via the Json and toJson methods for gson?

Thank you so much Harish Raj.

+2
source share
2 answers

Note. I am EclipseLink JAXB (MOXy) and a member of the JAXB 2 group (JSR-222) .

You can use MOXy to handle both aspects of the XML binding and JSON in this use case. As I mentioned in my comment, MOXy supports the XMLGregorianCalendar type. Metadata will look like this:

EmpRequest

 package forum7725188; import javax.xml.bind.annotation.*; @XmlRootElement(name="EmpRequest") @XmlAccessorType(XmlAccessType.FIELD) public class EmpRequest { @XmlElement(name="EmplIn") private EmplIn emplIn; } 

EmplIn

 package forum7725188; import javax.xml.bind.annotation.*; import javax.xml.datatype.XMLGregorianCalendar; @XmlAccessorType(XmlAccessType.FIELD) public class EmplIn { @XmlElement(name="EmpID") private long empId; @XmlElement(name="Empname") private String name; @XmlElement(name="Designation") private String designation; @XmlElement(name="DOJ") private XMLGregorianCalendar doj; } 

package info

 @XmlSchema(namespace="http://java.com/Employee", elementFormDefault=XmlNsForm.QUALIFIED) @XmlAccessorType(XmlAccessType.FIELD) package forum7725188; import javax.xml.bind.annotation.*; 

Demo

You can configure the MOXy implementation of Marshaller to output JSON by setting the eclipselink.media-type property to application/json .

 package forum7725188; import java.io.File; import javax.xml.bind.*; import javax.xml.namespace.QName; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(EmpRequest.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum7725188/input.xml"); EmpRequest empRequest = (EmpRequest) unmarshaller.unmarshal(xml); JAXBElement<EmpRequest> jaxbElement = new JAXBElement<EmpRequest>(new QName(""), EmpRequest.class, empRequest); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty("eclipselink.media-type", "application/json"); marshaller.marshal(jaxbElement, System.out); } } 

Input.xml

 <?xml version="1.0" encoding="UTF-8"?> <EmpRequest xmlns="http://java.com/Employee"> <EmplIn> <EmpID>12</EmpID> <Empname>sara</Empname> <Designation>SA</Designation> <DOJ>2002-05-30T09:30:10+06:00</DOJ> </EmplIn> </EmpRequest> 

Output

 {"EmplIn" : {"EmpID" : "12", "Empname" : "sara", "Designation" : "SA", "DOJ" : "2002-05-30T09:30:10+06:00"}} 

Additional Information

+2
source

Hope this can solve my google-gson problem .

(When creating a Gson object, add the following)

Step 1:

  Gson gson = new GsonBuilder().registerTypeAdapter(XMLGregorianCalendar.class, new XGCalConverter.Serializer()).registerTypeAdapter(XMLGregorianCalendar.class, new XGCalConverter.Deserializer()).create(); 

Step 2. And we need to create the XGCalConverter class as follows.

  import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import java.lang.reflect.Type; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; public class XGCalConverter { public static class Serializer implements JsonSerializer { public Serializer() { super(); } public JsonElement serialize(Object t, Type type, JsonSerializationContext jsonSerializationContext) { XMLGregorianCalendar xgcal=(XMLGregorianCalendar)t; return new JsonPrimitive(xgcal.toXMLFormat()); } } public static class Deserializer implements JsonDeserializer { public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(jsonElement.getAsString()); } catch(Exception ex) { ex.printStackTrace(); return null; } } } } 
+8
source

All Articles