aboutsummaryrefslogtreecommitdiffstats
path: root/gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-02-18 17:15:07 -0500
committerJim Hahn <jrh3@att.com>2021-02-18 17:24:26 -0500
commit758cd2e691950843415515ba6df2295679bd938b (patch)
tree1c3acdcf18a04f655b6e642fc80a7d8630934f24 /gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java
parent00ee2d5b4f1e847d8e272c79f66230aa30cb11d6 (diff)
Remove duplicate code from type adapters
Extracted a common superclass, StringTypeAdapter, out of the gson type adapter classes. Issue-ID: POLICY-2914 Change-Id: I4bbd2a6e8372e7f42c4952ba9ff03eed97473fb2 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java')
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java37
1 files changed, 2 insertions, 35 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java
index faf5ffd1..3f046b01 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java
@@ -20,49 +20,16 @@
package org.onap.policy.common.gson;
-import com.google.gson.JsonParseException;
-import com.google.gson.TypeAdapter;
-import com.google.gson.stream.JsonReader;
-import com.google.gson.stream.JsonToken;
-import com.google.gson.stream.JsonWriter;
-import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
-public class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
- private DateTimeFormatter formatter;
+public class OffsetDateTimeTypeAdapter extends StringTypeAdapter<OffsetDateTime> {
public OffsetDateTimeTypeAdapter() {
this(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) {
- this.formatter = formatter;
- }
-
- @Override
- public OffsetDateTime read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- return OffsetDateTime.parse(in.nextString(), formatter);
- }
-
- } catch (DateTimeParseException e) {
- throw new JsonParseException("invalid date", e);
- }
- }
-
- @Override
- public void write(JsonWriter out, OffsetDateTime value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(formatter);
- out.value(text);
- }
+ super("date", string -> OffsetDateTime.parse(string, formatter), value -> value.format(formatter));
}
}