diff options
author | Guangrong Fu <fu.guangrong@zte.com.cn> | 2021-12-03 17:32:42 +0800 |
---|---|---|
committer | Guangrong Fu <fu.guangrong@zte.com.cn> | 2021-12-03 19:09:42 +0800 |
commit | 64e336dadd4eb89dd0a8caf6bf62f0e86f4732a6 (patch) | |
tree | 83fbde5f57fbe848e30e8ec1f34570700da24be4 /holmes-actions/src/main/java/org | |
parent | c03d170278c22f9c7ba271f1783811f3b0f5bda8 (diff) |
bugfix - date converter error for gson
Issue-ID: HOLMES-492
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
Change-Id: I995ac283a00edb35ff5a08adc87acfd2b9043fb1
Diffstat (limited to 'holmes-actions/src/main/java/org')
-rw-r--r-- | holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java | 31 |
1 files changed, 19 insertions, 12 deletions
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. + * <p> * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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<Date>) (jsonElement, type, jsonDeserializationContext) -> { + try { + return jsonElement == null ? null : new Date(jsonElement.getAsLong()); + } catch (NumberFormatException e) { + return null; + } + }) + .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) + -> date == null ? null : new JsonPrimitive(date.getTime())) .create(); } } @@ -74,9 +81,9 @@ public class GsonUtil { List<Map<String, T>> 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; |