question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

the XMLGregorianCalendar datatype is not supported by gson API

See original GitHub issue
What steps will reproduce the problem?
1. If a POJO has a variable of the XMLGregorianCalendar datatype, the result is 
null.
2.
3.

What is the expected output? What do you see instead?
It should be Date.

What version of the product are you using? On what operating system?
gSon 1.2.3 and Windows 7

Please provide any additional information below.


I have application from which XML can be converted to json and vice versa. The 
following variable in my POJO with the datatype of XMLGregorianCalendar for 
date is null in my output.

protected XMLGregorianCalendar doj;

Please help me ASAP.

Regards,
Harish

Original issue reported on code.google.com by swarab...@gmail.com on 10 Oct 2011 at 8:01

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
lgroussetcommented, Jan 15, 2016

With gson v2.5 and java 8

public class XMLGregorianCalendarConverter {
    public static class Serializer implements JsonSerializer<XMLGregorianCalendar> {
        @Override
        public JsonElement serialize(XMLGregorianCalendar xmlGregorianCalendar, Type type, JsonSerializationContext jsonSerializationContext) {
            return new JsonPrimitive(xmlGregorianCalendar.toXMLFormat());
        }
    }

    public static class Deserializer implements JsonDeserializer<XMLGregorianCalendar> {
        @Override
        public XMLGregorianCalendar deserialize(JsonElement jsonElement, Type type,
                JsonDeserializationContext jsonDeserializationContext) {
            try {
                return DatatypeFactory.newInstance().newXMLGregorianCalendar(jsonElement.getAsString());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}

and the registration

    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalendarConverter.Deserializer())
            .registerTypeAdapter(XMLGregorianCalendar.class, new XMLGregorianCalendarConverter.Serializer())
            .create();
5reactions
GoogleCodeExportercommented, Mar 19, 2015
I had an issue with the above example. The "jsonElement.getAsString()" method 
would throw an UnSupportedOperation exception and the adapter would always 
returned null. I made a slight modification that worked for me. Here is what I 
used to be able to DeSerialize the XMLGregorianCalendar class to json:

public class XMLGregorianCalendarConverter {
    public static class Serializer implements JsonSerializer {

        public Serializer() {
            super();
        }
        @Override
        public JsonElement serialize(Object t, Type type,
                JsonSerializationContext jsonSerializationContext) {
            XMLGregorianCalendar xgcal = (XMLGregorianCalendar) t;
            return new JsonPrimitive(xgcal.toXMLFormat());
        }

    }
    public static class Deserializer implements JsonDeserializer {

        @Override
        public Object deserialize(JsonElement jsonElement, Type type,
                JsonDeserializationContext jsonDeserializationContext) {
            try {
                JsonObject obj  = jsonElement.getAsJsonObject();
                XMLGregorianCalendar xmlGregCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(obj.get("year").getAsInt(), 
                                                                     obj.get("month").getAsInt(), 
                                                                     obj.get("day").getAsInt(), 
                                                                     obj.get("hour").getAsInt(), 
                                                                     obj.get("minute").getAsInt(),obj.get("second").getAsInt(),
                                                                     0, obj.get("timezone").getAsInt());
                return xmlGregCalendar;
            } catch (Exception e) {
                return null;
            }
        }

    }
}

Original comment by eric.b.f...@gmail.com on 18 Oct 2013 at 2:59

Read more comments on GitHub >

github_iconTop Results From Across the Web

the XMLGregorianCalendar datatype is not supported by gson ...
Issue 368 in google-gson: the XMLGregorianCalendar datatype is not supported by gson API. 587 views. Skip to first unread message.
Read more >
How to de-serialize XMLGregorianCalender with Gson?
Abstract class javax.xml.datatype.XMLGregorianCalendar can not be a instantiated by its default/no-args constructor which makes GSON to fail ...
Read more >
XMLGregorianCalendarAdapter (Prowide Core API Reference)
Method Summary ; javax.xml.datatype.XMLGregorianCalendar, deserialize(com.google.gson.JsonElement jsonElement, java.lang.reflect.Type type, com.google.gson.
Read more >
XMLGregorianCalendar is changing the format while using ...
The reason why Gson outputs the XMLGregorianCalendar as JSON object is because it does not have a built-in adapter for this type and...
Read more >
java.lang.RuntimeException: Failed to invoke public javax.xml ...
RuntimeException: Failed to invoke public javax.xml.datatype.XMLGregorianCalendar() with no args. g = (MessageType) gson.fromJson(s, MessageType. class );.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found