dsl_json.java.util.DateDslJsonConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
The newest version!
package dsl_json.java.util;
import com.dslplatform.json.*;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.ZoneId;
public class DateDslJsonConverter implements Configuration {
@Override
public void configure(DslJson json) {
json.registerReader(java.util.Date.class, new JsonReader.ReadObject() {
@Nullable
@Override
public java.util.Date read(JsonReader reader) throws IOException {
return reader.wasNull() ? null : java.util.Date.from(JavaTimeConverter.deserializeDateTime(reader).toInstant());
}
});
json.registerWriter(java.util.Date.class, new JsonWriter.WriteObject() {
@Override
public void write(JsonWriter writer, @Nullable java.util.Date value) {
if (value == null) writer.writeNull();
else if (value instanceof java.sql.Date) JavaTimeConverter.serialize(((java.sql.Date)value).toLocalDate(), writer);
else JavaTimeConverter.serialize(OffsetDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()), writer);
}
});
}
}