Added Some Tools to GsonUtil
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / GsonUtil.java
1 /**
2  * Copyright 2018-2020 ZTE Corporation.
3  *
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  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.Gson;
19 import com.google.gson.GsonBuilder;
20 import com.google.gson.JsonDeserializer;
21 import com.google.gson.JsonObject;
22 import com.google.gson.reflect.TypeToken;
23 import org.apache.commons.lang3.StringUtils;
24
25 import java.util.List;
26 import java.util.Map;
27
28 public class GsonUtil {
29     private static Gson gson = null;
30
31     static {
32         if (gson == null) {
33             gson = new GsonBuilder()
34                     .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
35                         try {
36                             return json.getAsInt();
37                         } catch (NumberFormatException e) {
38                             return 0;
39                         }
40                     })
41                     .create();
42         }
43     }
44
45     private GsonUtil() {
46     }
47
48
49     public static String beanToJson(Object object) {
50         String gsonString = null;
51         if (gson != null) {
52             gsonString = gson.toJson(object);
53         }
54         return gsonString;
55     }
56
57     public static <T> T jsonToBean(String gsonString, Class<T> cls) {
58         T t = null;
59         if (gson != null) {
60             t = gson.fromJson(gsonString, cls);
61         }
62         return t;
63     }
64
65     public static <T> List<T> jsonToList(String gsonString, Class<T> cls) {
66         List<T> list = null;
67         if (gson != null) {
68             list = gson.fromJson(gsonString, TypeToken.getParameterized(List.class, cls).getType());
69         }
70         return list;
71     }
72
73     public static <T> List<Map<String, T>> jsonToListMaps(String gsonString, Class<T> cls) {
74         List<Map<String, T>> list = null;
75         if (gson != null) {
76             list = gson.fromJson(gsonString,
77                 TypeToken.getParameterized(List.class,
78                     TypeToken.getParameterized(Map.class, String.class, cls).getType()
79                 ).getType()
80             );
81         }
82         return list;
83     }
84
85     public static <T> Map<String, T> jsonToMap(String gsonString, Class<T> cls) {
86         Map<String, T> map = null;
87         if (gson != null) {
88             map = gson.fromJson(gsonString, TypeToken.getParameterized(Map.class, String.class, cls).getType());
89         }
90         return map;
91     }
92
93     public static String getAsString(JsonObject o, String field) {
94         String ret = null;
95
96         if (field == null) {
97             field = StringUtils.EMPTY;
98         }
99
100         if (o.has(field) && !o.get(field).isJsonNull()) {
101             ret = o.get(field).getAsString();
102         }
103         return ret;
104     }
105
106     public static long getAsLong(JsonObject o, String field) {
107         long ret = 0;
108
109         if (field == null) {
110             field = StringUtils.EMPTY;
111         }
112
113         if (o.has(field) && !o.get(field).isJsonNull()) {
114             ret = o.get(field).getAsLong();
115         }
116         return ret;
117     }
118
119     public static int getAsInt(JsonObject o, String field) {
120         int ret = 0;
121
122         if (field == null) {
123             field = StringUtils.EMPTY;
124         }
125
126         if (o.has(field) && !o.get(field).isJsonNull()) {
127             ret = o.get(field).getAsInt();
128         }
129         return ret;
130     }
131 }