I recently switched some of our serialization from Jackson to Gson. Found out that Jackson serializes dates to longs.
But, Gson serializes Date
You can do both direction with one type adapter:
public class DateLongFormatTypeAdapter extends TypeAdapter {
@Override
public void write(JsonWriter out, Date value) throws IOException {
if(value != null) out.value(value.getTime());
else out.nullValue();
}
@Override
public Date read(JsonReader in) throws IOException {
return new Date(in.nextLong());
}
}
Gson builder:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateLongFormatTypeAdapter())
.create();