Comments (4)
Install your own type adapter! And you should avoid reporting bugs against
ancient versions of GSON.
Original comment by limpbizkit
on 10 Oct 2011 at 2:07
- Changed state: WontFix
- Added labels: ****
- Removed labels: ****
from google-gson.
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();
Original comment by [email protected]
on 22 Mar 2013 at 4:15
- Added labels: ****
- Removed labels: ****
from google-gson.
[deleted comment]
from google-gson.
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 [email protected]
on 18 Oct 2013 at 2:59
- Added labels: ****
- Removed labels: ****
from google-gson.
Related Issues (20)
- JsonArray has no remove/set HOT 11
- Threading issue in FieldAttributes? HOT 15
- Deserialization of a specified Json into an object of type Map<String, Set<Long>> HOT 2
- Decide what to do about '/' HOT 2
- links in user guide broken HOT 1
- Patch for /trunk/examples/android-proguard-example/proguard.cfg HOT 2
- JsonObject#getAsString() throws UnsupportedOperationException HOT 2
- Error in JsonSerializer and JsonDeserializer documentation. HOT 2
- Strange example in the user guide HOT 1
- Gson.toJson(Object) emits surprising results for JsonElement values HOT 1
- Cannot map a field value to an enum value if value is a keyword HOT 3
- final transient list initializers aren't executed HOT 2
- Maven artifacts for release 1.7.1 HOT 1
- transient access modifier is hidden to Gson object HOT 2
- Fix @Since and @Until in GSON 2.0 HOT 1
- NPE calling toJson() with a null UUID field HOT 2
- Double braces initialization seems to break toJson HOT 2
- The javadoc for JsonDeserializer is out of date or incorrect (I'm pretty sure) HOT 3
- GsonBuilder.setLenient(false) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from google-gson.