summaryrefslogtreecommitdiffstats
path: root/models-interactions/model-impl/so
diff options
context:
space:
mode:
Diffstat (limited to 'models-interactions/model-impl/so')
-rw-r--r--models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java7
-rw-r--r--models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java68
2 files changed, 73 insertions, 2 deletions
diff --git a/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java
index 7224e665a..78506380d 100644
--- a/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java
+++ b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/Serialization.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* mso
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,11 +23,14 @@ package org.onap.policy.so.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import java.time.LocalDateTime;
public final class Serialization {
public static final Gson gsonPretty =
- new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
+ new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
+ .registerTypeAdapter(LocalDateTime.class, new SoLocalDateTimeTypeAdapter())
+ .create();
private Serialization() {
// utility class with explicit private constructor
diff --git a/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java
new file mode 100644
index 000000000..a0e835ffb
--- /dev/null
+++ b/models-interactions/model-impl/so/src/main/java/org/onap/policy/so/util/SoLocalDateTimeTypeAdapter.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.so.util;
+
+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;
+
+/*
+ * TODO: combine the functionality of this adapter with existing LocalDateTimeTypeAdapter and eliminate this class.
+ */
+
+/**
+ * GSON Type Adapter for "LocalDateTime" fields, that uses the standard RFC_1123_DATE_TIME
+ * formatter.
+ */
+public class SoLocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
+ private static final DateTimeFormatter FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME;
+
+ @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);
+ }
+ }
+}