com.gitee.easyopen.util.json.JsonUtil Maven / Gradle / Ivy
package com.gitee.easyopen.util.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
* json工具类,兼容fastjson用法(非100%兼容,只封装了常用方法)
*/
public class JsonUtil {
private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* Gson实例线程安全,声明一个即可。文档中有说明:
* Gson instances are Thread-safe so you can reuse them freely across multiple threads
*/
private static Gson writer = new GsonBuilder()
.registerTypeAdapterFactory(LocalDateTimeTypeAdapter.FACTORY)
.registerTypeAdapterFactory(LocalDateTypeAdapter.FACTORY)
.setDateFormat(DATETIME_PATTERN)
.create();
/** 解析专用 */
private static Gson parser = new GsonBuilder()
.registerTypeAdapterFactory(LocalDateTimeTypeAdapter.FACTORY)
.registerTypeAdapterFactory(LocalDateTypeAdapter.FACTORY)
// 解决int变double问题
.registerTypeAdapterFactory(MapTypeAdapter.FACTORY)
.setDateFormat(DATETIME_PATTERN)
.create();
private static final Type TYPE_STRING_MAP = new TypeToken