aboutsummaryrefslogtreecommitdiffstats
path: root/gson/src/main/java/org/onap/policy/common/gson
diff options
context:
space:
mode:
Diffstat (limited to 'gson/src/main/java/org/onap/policy/common/gson')
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/DoubleConverter.java13
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java31
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java10
-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/JacksonFieldAdapterFactory.java4
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/JacksonMethodAdapterFactory.java4
-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.java35
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/MapDoubleAdapterFactory.java4
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java35
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.java35
-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.java30
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapter.java45
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java15
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java14
-rw-r--r--gson/src/main/java/org/onap/policy/common/gson/internal/JacksonTypeAdapter.java9
17 files changed, 287 insertions, 150 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/DoubleConverter.java b/gson/src/main/java/org/onap/policy/common/gson/DoubleConverter.java
index 81803ff2..4d10bd13 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/DoubleConverter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/DoubleConverter.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 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.
@@ -25,18 +25,17 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
/**
* Converter for Double values. By default, GSON treats all Objects that are numbers, as
* Double. This converts Doubles to Integer or Long, if possible. It converts stand-alone
* Doubles, as well as those found within Arrays and Maps.
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DoubleConverter {
- private DoubleConverter() {
- // do nothing
- }
-
/**
* Performs in-place conversion of all values in a list.
*
@@ -98,7 +97,7 @@ public class DoubleConverter {
}
Double num = (Double) value;
- long longval = num.longValue();
+ var longval = num.longValue();
if (Double.compare(num.doubleValue(), longval) != 0) {
// it isn't integral - return unchanged value
@@ -106,7 +105,7 @@ public class DoubleConverter {
}
// it's integral - determine if it's an integer or a long
- int intval = (int) longval;
+ var intval = (int) longval;
if (intval == longval) {
return intval;
diff --git a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java
index d6e36b39..a693b7f4 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/GsonMessageBodyHandler.java
@@ -2,7 +2,8 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +23,13 @@ package org.onap.policy.common.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.MultivaluedMap;
+import jakarta.ws.rs.ext.MessageBodyReader;
+import jakarta.ws.rs.ext.MessageBodyWriter;
+import jakarta.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -31,15 +39,12 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
+import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.OffsetTime;
+import java.time.ZoneOffset;
import java.time.ZonedDateTime;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
-import javax.ws.rs.ext.Provider;
import lombok.AccessLevel;
import lombok.Getter;
import org.slf4j.Logger;
@@ -91,7 +96,11 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag
return builder.disableHtmlEscaping().registerTypeAdapterFactory(new MapDoubleAdapterFactory())
.registerTypeAdapter(Instant.class, new InstantTypeAdapter())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter())
- .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter());
+ .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter())
+ .registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter())
+ .registerTypeAdapter(OffsetTime.class, new OffsetTimeTypeAdapter())
+ .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
+ .registerTypeAdapter(ZoneOffset.class, new ZoneOffsetTypeAdapter());
}
@Override
@@ -108,7 +117,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
- try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
+ try (var writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
Type jsonType = (type.equals(genericType) ? type : genericType);
gson.toJson(object, jsonType, writer);
}
@@ -144,7 +153,7 @@ public class GsonMessageBodyHandler implements MessageBodyReader<Object>, Messag
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
- try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
+ try (var streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
Type jsonType = (type.equals(genericType) ? type : genericType);
return gson.fromJson(streamReader, jsonType);
}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java
index 6bcfaac3..c38a3e9b 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/InstantAsMillisTypeAdapter.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.
@@ -26,7 +26,6 @@ import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.time.Instant;
-import java.util.concurrent.TimeUnit;
/**
* GSON Type Adapter for "Instant" fields, that encodes them as milliseconds.
@@ -38,10 +37,7 @@ public class InstantAsMillisTypeAdapter extends TypeAdapter<Instant> {
if (value == null) {
out.nullValue();
} else {
- long epochMillis = TimeUnit.MILLISECONDS.convert(value.getEpochSecond(), TimeUnit.SECONDS);
- long nanoMillis = TimeUnit.MILLISECONDS.convert(value.getNano(), TimeUnit.NANOSECONDS);
- long millis = epochMillis + nanoMillis;
- out.value(millis);
+ out.value(value.toEpochMilli());
}
}
@@ -51,7 +47,7 @@ public class InstantAsMillisTypeAdapter extends TypeAdapter<Instant> {
in.nextNull();
return null;
} else {
- long millis = in.nextLong();
+ var millis = in.nextLong();
return Instant.ofEpochMilli(millis);
}
}
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/JacksonFieldAdapterFactory.java b/gson/src/main/java/org/onap/policy/common/gson/JacksonFieldAdapterFactory.java
index 3458a590..18157b03 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/JacksonFieldAdapterFactory.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/JacksonFieldAdapterFactory.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 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.
@@ -54,7 +54,7 @@ public class JacksonFieldAdapterFactory implements TypeAdapterFactory {
return null;
}
- ClassWalker data = new ClassWalker();
+ var data = new ClassWalker();
data.walkClassHierarchy(clazz);
if (data.getInProps(Field.class).isEmpty() && data.getOutProps(Field.class).isEmpty()) {
diff --git a/gson/src/main/java/org/onap/policy/common/gson/JacksonMethodAdapterFactory.java b/gson/src/main/java/org/onap/policy/common/gson/JacksonMethodAdapterFactory.java
index de962316..b7414004 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/JacksonMethodAdapterFactory.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/JacksonMethodAdapterFactory.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 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.
@@ -58,7 +58,7 @@ public class JacksonMethodAdapterFactory implements TypeAdapterFactory {
return null;
}
- ClassWalker data = new ClassWalker();
+ var data = new ClassWalker();
data.walkClassHierarchy(clazz);
if (data.getInProps(Method.class).isEmpty() && data.getOutProps(Method.class).isEmpty()
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..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,45 +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.
+ * ISO_LOCAL_DATE_TIME formatter, by default.
*/
-public class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
- private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
+public class LocalDateTimeTypeAdapter extends StringTypeAdapter<LocalDateTime> {
- @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);
- }
+ public LocalDateTimeTypeAdapter() {
+ this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
- @Override
- public void write(JsonWriter out, LocalDateTime value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(FORMATTER);
- out.value(text);
- }
+ public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
+ 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
new file mode 100644
index 00000000..0f666e5e
--- /dev/null
+++ b/gson/src/main/java/org/onap/policy/common/gson/LocalDateTypeAdapter.java
@@ -0,0 +1,35 @@
+/*-
+ * ============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 java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+
+public class LocalDateTypeAdapter extends StringTypeAdapter<LocalDate> {
+
+ public LocalDateTypeAdapter() {
+ this(DateTimeFormatter.ISO_LOCAL_DATE);
+ }
+
+ public LocalDateTypeAdapter(DateTimeFormatter formatter) {
+ super("date", string -> LocalDate.parse(string, formatter), value -> value.format(formatter));
+ }
+}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/MapDoubleAdapterFactory.java b/gson/src/main/java/org/onap/policy/common/gson/MapDoubleAdapterFactory.java
index 575c41ee..057e97f8 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/MapDoubleAdapterFactory.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/MapDoubleAdapterFactory.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 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.
@@ -112,7 +112,7 @@ public class MapDoubleAdapterFactory implements TypeAdapterFactory {
@Override
public T read(JsonReader in) throws IOException {
- T value = delegate.read(in);
+ var value = delegate.read(in);
DoubleConverter.convertFromDouble(value);
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
new file mode 100644
index 00000000..3f046b01
--- /dev/null
+++ b/gson/src/main/java/org/onap/policy/common/gson/OffsetDateTimeTypeAdapter.java
@@ -0,0 +1,35 @@
+/*-
+ * ============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 java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+
+public class OffsetDateTimeTypeAdapter extends StringTypeAdapter<OffsetDateTime> {
+
+ public OffsetDateTimeTypeAdapter() {
+ this(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
+ }
+
+ public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) {
+ 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
new file mode 100644
index 00000000..895b9de6
--- /dev/null
+++ b/gson/src/main/java/org/onap/policy/common/gson/OffsetTimeTypeAdapter.java
@@ -0,0 +1,35 @@
+/*-
+ * ============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 java.time.OffsetTime;
+import java.time.format.DateTimeFormatter;
+
+public class OffsetTimeTypeAdapter extends StringTypeAdapter<OffsetTime> {
+
+ public OffsetTimeTypeAdapter() {
+ this(DateTimeFormatter.ISO_OFFSET_TIME);
+ }
+
+ public OffsetTimeTypeAdapter(DateTimeFormatter formatter) {
+ 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
new file mode 100644
index 00000000..60758ff3
--- /dev/null
+++ b/gson/src/main/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapter.java
@@ -0,0 +1,30 @@
+/*-
+ * ============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 java.time.ZoneOffset;
+
+public class ZoneOffsetTypeAdapter extends StringTypeAdapter<ZoneOffset> {
+
+ 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 147fb03d..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
@@ -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,45 +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 FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME;
+public class ZonedDateTimeTypeAdapter extends StringTypeAdapter<ZonedDateTime> {
- @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);
- }
+ /**
+ * Constructs an adapter that uses the ISO_ZONED_DATE_TIME formatter.
+ */
+ public ZonedDateTimeTypeAdapter() {
+ this(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
- @Override
- public void write(JsonWriter out, ZonedDateTime value) throws IOException {
- if (value == null) {
- out.nullValue();
- } else {
- String text = value.format(FORMATTER);
- out.value(text);
- }
+ /**
+ * Constructs an adapter that uses the specified formatter for reading and writing.
+ *
+ * @param formatter date-time formatter
+ */
+ public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
+ super("date", string -> ZonedDateTime.parse(string, formatter), value -> value.format(formatter));
}
}
diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java b/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java
index 64a51d0b..af4a746c 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/internal/Adapter.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-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.
@@ -24,11 +24,12 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
+import com.google.re2j.Pattern;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.function.Supplier;
-import java.util.regex.Pattern;
+import lombok.Getter;
import org.onap.policy.common.gson.annotation.GsonJsonProperty;
/**
@@ -49,6 +50,7 @@ public class Adapter {
/**
* Name of the property within the json structure containing the item.
*/
+ @Getter
private final String propName;
/**
@@ -69,6 +71,7 @@ public class Adapter {
/**
* Name of the item being lifted - used when throwing exceptions.
*/
+ @Getter
private final String fullName;
/**
@@ -143,14 +146,6 @@ public class Adapter {
return reader.getConverter().fromJsonTree(tree);
}
- public final String getPropName() {
- return propName;
- }
-
- public final String getFullName() {
- return fullName;
- }
-
/**
* Makes an error message, appending the item's full name to the message prefix.
*
diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java b/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java
index ef4eaae3..954d3f4c 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/internal/ClassWalker.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 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.
@@ -30,6 +30,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import lombok.Getter;
import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
@@ -79,23 +80,16 @@ public class ClassWalker {
* Method having {@link GsonJsonAnyGetter} annotation. Overwritten as new "any-getters"
* are identified.
*/
+ @Getter
private Method anyGetter = null;
/**
* Method having {@link GsonJsonAnySetter} annotation. Overwritten as new "any-setters"
* are identified.
*/
+ @Getter
private Method anySetter = null;
-
- public Method getAnyGetter() {
- return anyGetter;
- }
-
- public Method getAnySetter() {
- return anySetter;
- }
-
/**
* Gets the names of input properties that are not being ignored.
*
diff --git a/gson/src/main/java/org/onap/policy/common/gson/internal/JacksonTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/internal/JacksonTypeAdapter.java
index 1171fd4d..34d61f47 100644
--- a/gson/src/main/java/org/onap/policy/common/gson/internal/JacksonTypeAdapter.java
+++ b/gson/src/main/java/org/onap/policy/common/gson/internal/JacksonTypeAdapter.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 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.
@@ -22,7 +22,6 @@ package org.onap.policy.common.gson.internal;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
@@ -78,7 +77,7 @@ public class JacksonTypeAdapter<T> extends TypeAdapter<T> {
JsonElement tree = delegate.toJsonTree(value);
if (tree.isJsonObject()) {
- JsonObject jsonObj = tree.getAsJsonObject();
+ var jsonObj = tree.getAsJsonObject();
// serialize each item from the value into the target tree
for (Serializer serializer : serializers) {
@@ -92,10 +91,10 @@ public class JacksonTypeAdapter<T> extends TypeAdapter<T> {
@Override
public T read(JsonReader in) throws IOException {
JsonElement tree = elementAdapter.read(in);
- T object = delegate.fromJsonTree(tree);
+ var object = delegate.fromJsonTree(tree);
if (tree.isJsonObject()) {
- JsonObject jsonObj = tree.getAsJsonObject();
+ var jsonObj = tree.getAsJsonObject();
// deserialize each item from the tree into the target object
for (Deserializer dser : deserializers) {