Issue 368 in google-gson: the XMLGregorianCalendar datatype is not supported by gson API

675 views
Skip to first unread message

googl...@googlecode.com

unread,
Oct 10, 2011, 4:02:29 AM10/10/11
to google-gson...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 368 by swarab...@gmail.com: the XMLGregorianCalendar datatype is
not supported by gson API
http://code.google.com/p/google-gson/issues/detail?id=368

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

googl...@googlecode.com

unread,
Oct 10, 2011, 10:07:09 AM10/10/11
to google-gson...@googlegroups.com
Updates:
Status: WontFix

Comment #1 on issue 368 by limpbiz...@gmail.com: the XMLGregorianCalendar

Install your own type adapter! And you should avoid reporting bugs against
ancient versions of GSON.

googl...@googlecode.com

unread,
Mar 22, 2013, 12:15:23 PM3/22/13
to google-gson...@googlegroups.com

Comment #2 on issue 368 by mbreuer....@googlemail.com: the
XMLGregorianCalendar datatype is not supported by gson API
http://code.google.com/p/google-gson/issues/detail?id=368

nevertheless this feature is often needed. I found the following code for a
type adapter wheen googling
public class XMLGregorianCalendarConverter {
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 e) {
return null;
}
}
}
}
register this with
GsonBuilder gson_builder = new GsonBuilder();
gson_builder.registerTypeAdapter(XMLGregorianCalendar.class,
new XMLGregorianCalendarConverter.Serializer());
gson_builder.registerTypeAdapter(XMLGregorianCalendar.class,
new XMLGregorianCalendarConverter.Deserializer());
gson = gson_builder.create();



--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

googl...@googlecode.com

unread,
Oct 18, 2013, 10:56:07 AM10/18/13
to google-gson...@googlegroups.com

Comment #3 on issue 368 by eric.b.f...@gmail.com: the XMLGregorianCalendar
datatype is not supported by gson API
http://code.google.com/p/google-gson/issues/detail?id=368

I had issues with the above example. The "jsonElement.getAsString()" would
throw an UnSupportedOperation exception and the adapter 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;
//return
DatatypeFactory.newInstance().newXMLGregorianCalendar(obj.getAsString());
} catch (Exception e) {
return null;
}
}

}
}

googl...@googlecode.com

unread,
Oct 18, 2013, 10:59:28 AM10/18/13
to google-gson...@googlegroups.com

Comment #4 on issue 368 by eric.b.f...@gmail.com: the XMLGregorianCalendar
datatype is not supported by gson API
http://code.google.com/p/google-gson/issues/detail?id=368

I had an issue with the above example. The "jsonElement.getAsString()"
method would throw an UnSupportedOperation exception and the adapter would
Reply all
Reply to author
Forward
0 new messages