summaryrefslogtreecommitdiffstats
path: root/gson/src/main
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
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')
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java38
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/LocalDateTimeTypeAdapter.java39
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/LocalDateTypeAdapter.java37
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java37
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.java37
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/StringTypeAdapter.java76
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapter.java33
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java43
8 files changed, 99 insertions, 241 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java
index 9ebf2baa..bad66af3 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,41 +20,17 @@
package org.onap.policy.common.gson;
-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.Instant;
-import java.time.format.DateTimeParseException;
/**
* GSON Type Adapter for "Instant" fields, that uses the standard ISO_INSTANT formatter.
*/
-public class InstantTypeAdapter extends TypeAdapter<Instant> {
+public class InstantTypeAdapter extends StringTypeAdapter<Instant> {
- @Override
- public void write(JsonWriter out, Instant value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- out.value(value.toString());
- }
- }
-
- @Override
- public Instant read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- String text = in.nextString();
- return Instant.parse(text);
- }
-
- } catch (DateTimeParseException e) {
- throw new IOException("invalid date", e);
- }
+ /**
+ * Constructs an adapter.
+ */
+ public InstantTypeAdapter() {
+ super("date", Instant::parse, Instant::toString);
}
}
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 4d87ca46..5dc597e2 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
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,53 +20,20 @@
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.LocalDateTime;
import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
/**
* GSON Type Adapter for "LocalDateTime" fields, that uses the standard
* ISO_LOCAL_DATE_TIME formatter, by default.
*/
-public class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
- private DateTimeFormatter formatter;
+public class LocalDateTimeTypeAdapter extends StringTypeAdapter<LocalDateTime> {
public LocalDateTimeTypeAdapter() {
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
- this.formatter = formatter;
- }
-
- @Override
- public LocalDateTime read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- return LocalDateTime.parse(in.nextString(), formatter);
- }
-
- } catch (DateTimeParseException e) {
- throw new JsonParseException("invalid date", e);
- }
- }
-
- @Override
- public void write(JsonWriter out, LocalDateTime value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(formatter);
- out.value(text);
- }
+ super("date", string -> LocalDateTime.parse(string, formatter), value -> value.format(formatter));
}
}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/LocalDateTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/LocalDateTypeAdapter.java
index 8e33e464..0f666e5e 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/LocalDateTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/LocalDateTypeAdapter.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.LocalDate;
import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
-public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
- private DateTimeFormatter formatter;
+public class LocalDateTypeAdapter extends StringTypeAdapter<LocalDate> {
public LocalDateTypeAdapter() {
this(DateTimeFormatter.ISO_LOCAL_DATE);
}
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
- this.formatter = formatter;
- }
-
- @Override
- public LocalDate read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- return LocalDate.parse(in.nextString(), formatter);
- }
-
- } catch (DateTimeParseException e) {
- throw new JsonParseException("invalid date", e);
- }
- }
-
- @Override
- public void write(JsonWriter out, LocalDate value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(formatter);
- out.value(text);
- }
+ super("date", string -> LocalDate.parse(string, formatter), value -> value.format(formatter));
}
}
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));
}
}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.java
index 49a7d25d..895b9de6 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.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.OffsetTime;
import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
-public class OffsetTimeTypeAdapter extends TypeAdapter<OffsetTime> {
- private DateTimeFormatter formatter;
+public class OffsetTimeTypeAdapter extends StringTypeAdapter<OffsetTime> {
public OffsetTimeTypeAdapter() {
this(DateTimeFormatter.ISO_OFFSET_TIME);
}
public OffsetTimeTypeAdapter(DateTimeFormatter formatter) {
- this.formatter = formatter;
- }
-
- @Override
- public OffsetTime read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- return OffsetTime.parse(in.nextString(), formatter);
- }
-
- } catch (DateTimeParseException e) {
- throw new JsonParseException("invalid time", e);
- }
- }
-
- @Override
- public void write(JsonWriter out, OffsetTime value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(formatter);
- out.value(text);
- }
+ super("time", string -> OffsetTime.parse(string, formatter), value -> value.format(formatter));
}
}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/StringTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/StringTypeAdapter.java
new file mode 100644
index 00000000..22481697
--- /dev/null
+++ b/gson/src/main/java/org/onap/policy/common/gson/StringTypeAdapter.java
@@ -0,0 +1,76 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+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.util.function.Function;
+
+/**
+ * GSON Type Adapter for fields that are encoded as Strings.
+ */
+public class StringTypeAdapter<T> extends TypeAdapter<T> {
+ private final String exMessage;
+ private final Function<String, T> deserializer;
+ private final Function<T, String> serializer;
+
+ /**
+ * Constructs an adapter.
+ *
+ * @param type type of value, used in exception messages
+ * @param deserializer function used to deserialize a String into a value
+ * @param serializer function used to serialize a value into a String
+ */
+ public StringTypeAdapter(String type, Function<String, T> deserializer, Function<T, String> serializer) {
+ this.exMessage = "invalid " + type;
+ this.deserializer = deserializer;
+ this.serializer = serializer;
+ }
+
+ @Override
+ public T read(JsonReader in) throws IOException {
+ try {
+ if (in.peek() == JsonToken.NULL) {
+ in.nextNull();
+ return null;
+ } else {
+ return deserializer.apply(in.nextString());
+ }
+
+ } catch (RuntimeException e) {
+ throw new JsonParseException(exMessage, e);
+ }
+ }
+
+ @Override
+ public void write(JsonWriter out, T value) throws IOException {
+ if (value == null) {
+ out.nullValue();
+ } else {
+ String text = serializer.apply(value);
+ out.value(text);
+ }
+ }
+}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapter.java
index eb91ac4f..60758ff3 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapter.java
@@ -20,38 +20,11 @@
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.DateTimeException;
import java.time.ZoneOffset;
-public class ZoneOffsetTypeAdapter extends TypeAdapter<ZoneOffset> {
+public class ZoneOffsetTypeAdapter extends StringTypeAdapter<ZoneOffset> {
- @Override
- public ZoneOffset read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- return ZoneOffset.of(in.nextString());
- }
-
- } catch (DateTimeException e) {
- throw new JsonParseException("invalid zone", e);
- }
- }
-
- @Override
- public void write(JsonWriter out, ZoneOffset value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- out.value(value.toString());
- }
+ public ZoneOffsetTypeAdapter() {
+ super("zone", ZoneOffset::of, ZoneOffset::toString);
}
}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java
index 7777e702..928fae95 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java
@@ -20,63 +20,28 @@
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.ZonedDateTime;
import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
/**
* GSON Type Adapter for "ZonedDateTime" fields, that uses the standard
* ISO_ZONED_DATE_TIME formatter.
*/
-public class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
- private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME;
-
- private final DateTimeFormatter formatter;
-
+public class ZonedDateTimeTypeAdapter extends StringTypeAdapter<ZonedDateTime> {
/**
* Constructs an adapter that uses the ISO_ZONED_DATE_TIME formatter.
*/
public ZonedDateTimeTypeAdapter() {
- this(DEFAULT_FORMATTER);
+ this(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
/**
* Constructs an adapter that uses the specified formatter for reading and writing.
+ *
* @param formatter date-time formatter
*/
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
- this.formatter = formatter;
- }
-
- @Override
- public ZonedDateTime read(JsonReader in) throws IOException {
- try {
- if (in.peek() == JsonToken.NULL) {
- in.nextNull();
- return null;
- } else {
- return ZonedDateTime.parse(in.nextString(), formatter);
- }
-
- } catch (DateTimeParseException e) {
- throw new JsonParseException("invalid date", e);
- }
- }
-
- @Override
- public void write(JsonWriter out, ZonedDateTime value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(formatter);
- out.value(text);
- }
+ super("date", string -> ZonedDateTime.parse(string, formatter), value -> value.format(formatter));
}
}