From 64e336dadd4eb89dd0a8caf6bf62f0e86f4732a6 Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Fri, 3 Dec 2021 17:32:42 +0800 Subject: bugfix - date converter error for gson Issue-ID: HOLMES-492 Signed-off-by: Guangrong Fu Change-Id: I995ac283a00edb35ff5a08adc87acfd2b9043fb1 --- .../org/onap/holmes/common/utils/GsonUtil.java | 31 +++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'holmes-actions/src/main/java/org/onap') diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java index a5e6461..f5ad56c 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java @@ -1,12 +1,12 @@ /** - * Copyright 2018-2020 ZTE Corporation. - * + * Copyright 2018-2021 ZTE Corporation. + *

* 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 - * + *

+ * 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. @@ -15,13 +15,11 @@ */ package org.onap.holmes.common.utils; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonObject; +import com.google.gson.*; import com.google.gson.reflect.TypeToken; import org.apache.commons.lang3.StringUtils; +import java.util.Date; import java.util.List; import java.util.Map; @@ -38,6 +36,15 @@ public class GsonUtil { return 0; } }) + .registerTypeAdapter(Date.class, (JsonDeserializer) (jsonElement, type, jsonDeserializationContext) -> { + try { + return jsonElement == null ? null : new Date(jsonElement.getAsLong()); + } catch (NumberFormatException e) { + return null; + } + }) + .registerTypeAdapter(Date.class, (JsonSerializer) (date, type, jsonSerializationContext) + -> date == null ? null : new JsonPrimitive(date.getTime())) .create(); } } @@ -74,9 +81,9 @@ public class GsonUtil { List> list = null; if (gson != null) { list = gson.fromJson(gsonString, - TypeToken.getParameterized(List.class, - TypeToken.getParameterized(Map.class, String.class, cls).getType() - ).getType() + TypeToken.getParameterized(List.class, + TypeToken.getParameterized(Map.class, String.class, cls).getType() + ).getType() ); } return list; -- cgit 1.2.3-korg