Java tutorial
/* * Copyright (c) 2017 ?. All Rights Reserved. * Use of this source code is governed by a Shanghai Unovo Information Technology Co.,Ltd license * that can be found in the LICENSE file in the root of the web site. * * http://www.unovo.com.cn * * An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. * */ package com.unovo.frame.utils.gson; import com.unovo.frame.utils.gson.deserializer.DoubleJsonDeserializer; import com.unovo.frame.utils.gson.deserializer.FloatJsonDeserializer; import com.unovo.frame.utils.gson.deserializer.IntegerJsonDeserializer; import com.unovo.frame.utils.gson.deserializer.ListJsonDeserializer; import com.unovo.frame.utils.gson.deserializer.StringJsonDeserializer; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializer; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.List; import java.util.Map; /** * Created by Aeatho on 2016/12/29 16:51 * * Prject: unovo-guest-app * Description: * email: aeatho@gmail.com */ public class GsonHelper { private static Gson GSON_INSTANCE; public synchronized static Gson getGson() { if (GSON_INSTANCE == null) GSON_INSTANCE = createGson(); return GSON_INSTANCE; } private static Gson createGson() { GsonBuilder gsonBuilder = new GsonBuilder(); //gsonBuilder.setExclusionStrategies(new SpecificClassExclusionStrategy(null, Model.class)); gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss"); JsonDeserializer deserializer = new IntegerJsonDeserializer(); gsonBuilder.registerTypeAdapter(int.class, deserializer); gsonBuilder.registerTypeAdapter(Integer.class, deserializer); deserializer = new FloatJsonDeserializer(); gsonBuilder.registerTypeAdapter(float.class, deserializer); gsonBuilder.registerTypeAdapter(Float.class, deserializer); deserializer = new DoubleJsonDeserializer(); gsonBuilder.registerTypeAdapter(double.class, deserializer); gsonBuilder.registerTypeAdapter(Double.class, deserializer); deserializer = new StringJsonDeserializer(); gsonBuilder.registerTypeAdapter(String.class, deserializer); deserializer = new ListJsonDeserializer(); gsonBuilder.registerTypeAdapter(List.class, deserializer); return gsonBuilder.create(); } public static String createGsonString(Object object) { return getGson().toJson(object); } public static <T> T changeGsonToBean(String gsonString, Class<T> cls) { return getGson().fromJson(gsonString, cls); } public static <T> T changeGsonToBean(String gsonString, Type type) { return getGson().fromJson(gsonString, type); } public static <T> List<T> changeGsonToList(String gsonString, Class<T> cls) { return getGson().fromJson(gsonString, new TypeToken<List<T>>() { }.getType()); } public static <T> List<Map<String, T>> changeGsonToListMaps(String gsonString) { List<Map<String, T>> list = null; list = getGson().fromJson(gsonString, new TypeToken<List<Map<String, T>>>() { }.getType()); return list; } public static <T> Map<String, T> changeGsonToMaps(String gsonString) { Map<String, T> map = null; map = getGson().fromJson(gsonString, new TypeToken<Map<String, T>>() { }.getType()); return map; } }