f5ad56c6947181b54315080a4dd11467b34a1c6d
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / GsonUtil.java
1 /**
2  * Copyright 2018-2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.common.utils;
17
18 import com.google.gson.*;
19 import com.google.gson.reflect.TypeToken;
20 import org.apache.commons.lang3.StringUtils;
21
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Map;
25
26 public class GsonUtil {
27     private static Gson gson = null;
28
29     static {
30         if (gson == null) {
31             gson = new GsonBuilder()
32                     .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
33                         try {
34                             return json.getAsInt();
35                         } catch (NumberFormatException e) {
36                             return 0;
37                         }
38                     })
39                     .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (jsonElement, type, jsonDeserializationContext) -> {
40                         try {
41                             return jsonElement == null ? null : new Date(jsonElement.getAsLong());
42                         } catch (NumberFormatException e) {
43                             return null;
44                         }
45                     })
46                     .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext)
47                             -> date == null ? null : new JsonPrimitive(date.getTime()))
48                     .create();
49         }
50     }
51
52     private GsonUtil() {
53     }
54
55
56     public static String beanToJson(Object object) {
57         String gsonString = null;
58         if (gson != null) {
59             gsonString = gson.toJson(object);
60         }
61         return gsonString;
62     }
63
64     public static <T> T jsonToBean(String gsonString, Class<T> cls) {
65         T t = null;
66         if (gson != null) {
67             t = gson.fromJson(gsonString, cls);
68         }
69         return t;
70     }
71
72     public static <T> List<T> jsonToList(String gsonString, Class<T> cls) {
73         List<T> list = null;
74         if (gson != null) {
75             list = gson.fromJson(gsonString, TypeToken.getParameterized(List.class, cls).getType());
76         }
77         return list;
78     }
79
80     public static <T> List<Map<String, T>> jsonToListMaps(String gsonString, Class<T> cls) {
81         List<Map<String, T>> list = null;
82         if (gson != null) {
83             list = gson.fromJson(gsonString,
84                     TypeToken.getParameterized(List.class,
85                             TypeToken.getParameterized(Map.class, String.class, cls).getType()
86                     ).getType()
87             );
88         }
89         return list;
90     }
91
92     public static <T> Map<String, T> jsonToMap(String gsonString, Class<T> cls) {
93         Map<String, T> map = null;
94         if (gson != null) {
95             map = gson.fromJson(gsonString, TypeToken.getParameterized(Map.class, String.class, cls).getType());
96         }
97         return map;
98     }
99
100     public static String getAsString(JsonObject o, String field) {
101         String ret = null;
102
103         if (field == null) {
104             field = StringUtils.EMPTY;
105         }
106
107         if (o.has(field) && !o.get(field).isJsonNull()) {
108             ret = o.get(field).getAsString();
109         }
110         return ret;
111     }
112
113     public static long getAsLong(JsonObject o, String field) {
114         long ret = 0;
115
116         if (field == null) {
117             field = StringUtils.EMPTY;
118         }
119
120         if (o.has(field) && !o.get(field).isJsonNull()) {
121             ret = o.get(field).getAsLong();
122         }
123         return ret;
124     }
125
126     public static int getAsInt(JsonObject o, String field) {
127         int ret = 0;
128
129         if (field == null) {
130             field = StringUtils.EMPTY;
131         }
132
133         if (o.has(field) && !o.get(field).isJsonNull()) {
134             ret = o.get(field).getAsInt();
135         }
136         return ret;
137     }
138 }