Code example for JsonPrimitive

0
 
public class DateJsonTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
 
@Override 
public JsonElement serialize(Date date, Type type, JsonSerializationContext context) {
	return new JsonPrimitive(date.getTime());
} 
 
@Override 
public Date deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
	long time = element.getAsJsonPrimitive().getAsLong();
	if (time > 0) {
		return new Date(time);
	} 
	return null; 
} 
}