Recent site activity

Gson Type Adapters for Common Classes

This page contains example code for Gson Type Adapters.


Type adapter for org.joda,time.DateTime

class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {
  // No need for an InstanceCreator since DateTime provides a no-args constructor
  @Override
  public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
    return new JsonPrimitive(src.toString());
  }
  @Override
  public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
      throws JsonParseException {
    return new DateTime(json.getAsString());
  }
}



Comments