aboutsummaryrefslogtreecommitdiffstats
path: root/gson/src
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2020-11-30 14:34:29 -0500
committerJim Hahn <jrh3@att.com>2020-11-30 14:46:56 -0500
commit371821c3191999dfc90f77569b184172e598f431 (patch)
tree37c93bf1e881f6d6a1b8140ed90b7f77d2473955 /gson/src
parentbd384dfe6344a691a0276a2ac1a920a2feb9328e (diff)
Generalize LocalDateTimeTypeAdapter
The GSON DateTime type adapter uses a hard-coded formatter. Generalized it to allow a different formatter to be provided to the constructor. Issue-ID: POLICY-2903 Change-Id: Icdd51190f465bc4c73cd593d6d1b2003165a4b60 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src')
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/LocalDateTimeTypeAdapter.java16
1 files changed, 12 insertions, 4 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/LocalDateTimeTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/LocalDateTimeTypeAdapter.java
index 2b297cbf..4d87ca46 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/LocalDateTimeTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/LocalDateTimeTypeAdapter.java
@@ -32,10 +32,18 @@ import java.time.format.DateTimeParseException;
/**
* GSON Type Adapter for "LocalDateTime" fields, that uses the standard
- * ISO_LOCAL_DATE_TIME formatter.
+ * ISO_LOCAL_DATE_TIME formatter, by default.
*/
public class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
- private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
+ private DateTimeFormatter formatter;
+
+ public LocalDateTimeTypeAdapter() {
+ this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
+ }
+
+ public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
+ this.formatter = formatter;
+ }
@Override
public LocalDateTime read(JsonReader in) throws IOException {
@@ -44,7 +52,7 @@ public class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
in.nextNull();
return null;
} else {
- return LocalDateTime.parse(in.nextString(), FORMATTER);
+ return LocalDateTime.parse(in.nextString(), formatter);
}
} catch (DateTimeParseException e) {
@@ -57,7 +65,7 @@ public class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
if (value == null) {
out.nullValue();
} else {
- String text = value.format(FORMATTER);
+ String text = value.format(formatter);
out.value(text);
}
}