2 * Copyright 2018 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.common.utils;
18 import com.google.gson.Gson;
19 import com.google.gson.reflect.TypeToken;
20 import java.util.List;
23 public class GsonUtil {
24 private static Gson gson = null;
33 public static String beanToJson(Object object) {
34 String gsonString = null;
36 gsonString = gson.toJson(object);
41 public static <T> T jsonToBean(String gsonString, Class<T> cls) {
44 t = gson.fromJson(gsonString, cls);
49 public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
52 list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
58 public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
59 List<Map<String, T>> list = null;
61 list = gson.fromJson(gsonString,
62 new TypeToken<List<Map<String, T>>>() {
68 public static <T> Map<String, T> GsonToMaps(String gsonString) {
69 Map<String, T> map = null;
71 map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {