com.luna.gson.GsonUtil Maven / Gradle / Ivy
package com.luna.gson;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.luna.gson.adapter.NumberTypeAdapter;
import com.luna.gson.exception.GsonException;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
* Gson工具类
* 优势:
* 数据量低于1万的时候速度有绝对优势
* API和注解支持较为完善,支持宽松解析
* 支持的数据源较广泛(字符串,对象,文件、流)
*
* @author czy1024
* 2020/5/27 16:53
*/
public class GsonUtil {
private static Gson gson;
static {
GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
gsonBuilder.disableHtmlEscaping();// 禁止将部分特殊字符转义为unicode编码
registerTypeAdapter(gsonBuilder);
gson = gsonBuilder.create();
}
private static void registerTypeAdapter(GsonBuilder gsonBuilder) {
gsonBuilder.registerTypeAdapter(short.class, new NumberTypeAdapter<>(short.class));
gsonBuilder.registerTypeAdapter(Short.class, new NumberTypeAdapter<>(Short.class));
gsonBuilder.registerTypeAdapter(int.class, new NumberTypeAdapter<>(int.class));
gsonBuilder.registerTypeAdapter(Integer.class, new NumberTypeAdapter<>(Integer.class));
gsonBuilder.registerTypeAdapter(long.class, new NumberTypeAdapter<>(long.class));
gsonBuilder.registerTypeAdapter(Long.class, new NumberTypeAdapter<>(Long.class));
gsonBuilder.registerTypeAdapter(float.class, new NumberTypeAdapter<>(float.class));
gsonBuilder.registerTypeAdapter(Float.class, new NumberTypeAdapter<>(Float.class));
gsonBuilder.registerTypeAdapter(double.class, new NumberTypeAdapter<>(double.class));
gsonBuilder.registerTypeAdapter(Double.class, new NumberTypeAdapter<>(Double.class));
gsonBuilder.registerTypeAdapter(BigDecimal.class, new NumberTypeAdapter<>(BigDecimal.class));
}
/**
* JSON反序列化
*/
public static V from(InputStream inputStream, Class type) {
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
return gson.fromJson(reader, type);
}
/**
* JSON反序列化
*/
public static V from(InputStream inputStream, TypeToken typeToken) {
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
return gson.fromJson(reader, typeToken.getType());
}
/**
* JSON反序列化(List)
*/
public static List fromList(InputStream inputStream, Class type) {
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return gson.fromJson(reader, typeToken.getType());
}
/**
* JSON反序列化
*/
public static V from(File file, Class type) {
try {
JsonReader reader = new JsonReader(new FileReader(file));
return gson.fromJson(reader, type);
} catch (FileNotFoundException e) {
throw new GsonException("gson from error, file path: {}, type: {}", file.getPath(), type, e);
}
}
/**
* JSON反序列化
*/
public static V from(File file, TypeToken typeToken) {
try {
JsonReader reader = new JsonReader(new FileReader(file));
return gson.fromJson(reader, typeToken.getType());
} catch (FileNotFoundException e) {
throw new GsonException("gson from error, file path: {}, type: {}", file.getPath(), typeToken.getType(), e);
}
}
/**
* JSON反序列化(List)
*/
public static List fromList(File file, Class type) {
try {
JsonReader reader = new JsonReader(new FileReader(file));
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return gson.fromJson(reader, typeToken.getType());
} catch (FileNotFoundException e) {
throw new GsonException("gson from error, file path: {}, type: {}", file.getPath(), type, e);
}
}
/**
* JSON反序列化
*/
public static V from(String json, Class type) {
return gson.fromJson(json, type);
}
/**
* JSON反序列化
*/
public static V from(String json, Type type) {
return gson.fromJson(json, type);
}
/**
* JSON反序列化
*/
public static V from(String json, TypeToken typeToken) {
return gson.fromJson(json, typeToken.getType());
}
/**
* JSON反序列化(List)
*/
public static List fromList(String json, Class type) {
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return gson.fromJson(json, typeToken.getType());
}
/**
* JSON反序列化(Map)
*/
public static Map fromMap(String json) {
return gson.fromJson(json, new TypeToken>() {}.getType());
}
/**
* 宽松JSON反序列化
*/
public static V fromLenient(InputStream inputStream, Class type) {
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
reader.setLenient(true);
return gson.fromJson(reader, type);
}
/**
* 宽松JSON反序列化(List)
*/
public static List fromListLenient(InputStream inputStream, Class type) {
JsonReader reader = new JsonReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
reader.setLenient(true);
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return gson.fromJson(reader, typeToken.getType());
}
/**
* 宽松JSON反序列化
*/
public static V fromLenient(File file, Class type) {
try {
JsonReader reader = new JsonReader(new FileReader(file));
reader.setLenient(true);
return gson.fromJson(reader, type);
} catch (FileNotFoundException e) {
throw new GsonException("gson lenient from error, file path: {}, type: {}", file.getPath(), type, e);
}
}
/**
* 宽松JSON反序列化(List)
*/
public static List fromListLenient(File file, Class type) {
try {
JsonReader reader = new JsonReader(new FileReader(file));
reader.setLenient(true);
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return gson.fromJson(reader, typeToken.getType());
} catch (FileNotFoundException e) {
throw new GsonException("gson lenient from error, file path: {}, type: {}", file.getPath(), type, e);
}
}
/**
* 宽松JSON反序列化
*/
public static V fromLenient(String json, Class type) {
if (StringUtils.isEmpty(json)) {
return null;
}
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
return gson.fromJson(reader, type);
}
/**
* 宽松JSON反序列化
*/
public static V fromLenient(String json, Type type) {
if (StringUtils.isEmpty(json)) {
return null;
}
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
return gson.fromJson(reader, type);
}
/**
* 宽松JSON反序列化
*/
public static V fromLenient(String json, TypeToken typeToken) {
if (StringUtils.isEmpty(json)) {
return null;
}
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
return gson.fromJson(reader, typeToken.getType());
}
/**
* 宽松JSON反序列化(List)
*/
public static List fromListLenient(String json, Class type) {
if (StringUtils.isEmpty(json)) {
return null;
}
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return gson.fromJson(reader, typeToken.getType());
}
/**
* 序列化为JSON
*/
public static String to(List list) {
return gson.toJson(list);
}
/**
* 序列化为JSON
*/
public static String to(V v) {
return gson.toJson(v);
}
/**
* 序列化为JSON文件
*/
public static void toFile(String path, List list) {
try (JsonWriter jsonWriter = new JsonWriter(new FileWriter(new File(path), true));) {
gson.toJson(list, new TypeToken>() {}.getType(), jsonWriter);
jsonWriter.flush();
} catch (Exception e) {
throw new GsonException("gson to file error, path: {}, list: {}", path, list, e);
}
}
/**
* 序列化为JSON文件
*/
public static void toFile(String path, V v) {
try (JsonWriter jsonWriter = new JsonWriter(new FileWriter(new File(path), true));) {
gson.toJson(v, v.getClass(), jsonWriter);
jsonWriter.flush();
} catch (Exception e) {
throw new GsonException("gson to file error, path: {}, obj: {}", path, v, e);
}
}
/**
* 从json串中获取某个字段
*
* @return String,默认为 null
*/
public static String getAsString(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
String propertyValue;
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return null;
}
try {
propertyValue = jsonByKey.getAsString();
} catch (Exception e) {
propertyValue = jsonByKey.toString();
}
return propertyValue;
}
/**
* 从json串中获取某个字段
*
* @return int,默认为 0
*/
public static int getAsInt(String json, String key) {
if (StringUtils.isEmpty(json)) {
return 0;
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return 0;
}
try {
return jsonByKey.getAsInt();
} catch (Exception e) {
throw new GsonException("gson get int error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return long,默认为 0
*/
public static long getAsLong(String json, String key) {
if (StringUtils.isEmpty(json)) {
return 0L;
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return 0L;
}
try {
return jsonByKey.getAsLong();
} catch (Exception e) {
throw new GsonException("gson get long error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return double,默认为 0.0
*/
public static double getAsDouble(String json, String key) {
if (StringUtils.isEmpty(json)) {
return 0.0;
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return 0.0;
}
try {
return jsonByKey.getAsDouble();
} catch (Exception e) {
throw new GsonException("gson get double error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return BigInteger,默认为 0.0
*/
public static BigInteger getAsBigInteger(String json, String key) {
if (StringUtils.isEmpty(json)) {
return new BigInteger(String.valueOf(0.00));
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return new BigInteger(String.valueOf(0.00));
}
try {
return jsonByKey.getAsBigInteger();
} catch (Exception e) {
throw new GsonException("gson get big integer error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return BigDecimal,默认为 0.0
*/
public static BigDecimal getAsBigDecimal(String json, String key) {
if (StringUtils.isEmpty(json)) {
return new BigDecimal("0.0");
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return new BigDecimal("0.0");
}
try {
return jsonByKey.getAsBigDecimal();
} catch (Exception e) {
throw new GsonException("gson get big decimal error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return boolean, 默认为 false
*/
public static boolean getAsBoolean(String json, String key) {
if (StringUtils.isEmpty(json)) {
return false;
}
JsonPrimitive jsonByKey = (JsonPrimitive)getAsJsonObject(json, key);
if (null == jsonByKey) {
return false;
}
try {
if (jsonByKey.isBoolean()) {
return jsonByKey.getAsBoolean();
} else {
if (jsonByKey.isString()) {
String string = jsonByKey.getAsString();
if ("1".equals(string)) {
return true;
} else {
return BooleanUtils.toBoolean(string);
}
} else {// number
return BooleanUtils.toBoolean(jsonByKey.getAsInt());
}
}
} catch (Exception e) {
throw new GsonException("gson get boolean error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return byte, 默认为 0
*/
public static byte getAsByte(String json, String key) {
if (StringUtils.isEmpty(json)) {
return 0;
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return 0;
}
try {
return jsonByKey.getAsByte();
} catch (Exception e) {
throw new GsonException("gson get byte error, json: {}, key: {}", json, key, e);
}
}
/**
* 从json串中获取某个字段
*
* @return object, 默认为 null
*/
public static V getAsObject(String json, String key, Class type) {
if (StringUtils.isEmpty(json)) {
return null;
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return null;
}
try {
return from(jsonByKey.getAsString(), type);
} catch (Exception e) {
throw new GsonException("gson get list error, json: {}, key: {}, type: {}", json, key, type, e);
}
}
/**
* 从json串中获取某个字段
*
* @return list, 默认为 null
*/
public static List getAsList(String json, String key, Class type) {
if (StringUtils.isEmpty(json)) {
return null;
}
JsonElement jsonByKey = getAsJsonObject(json, key);
if (null == jsonByKey) {
return null;
}
try {
JsonArray jsonArray = jsonByKey.getAsJsonArray();
TypeToken> typeToken = (TypeToken>)TypeToken.getParameterized(ArrayList.class, type);
return from(jsonArray.toString(), typeToken);
} catch (Exception e) {
throw new GsonException("gson get list error, json: {}, key: {}, type: {}", json, key, type, e);
}
}
/**
* 从json串中获取某个字段
*/
public static JsonElement getAsJsonObject(String json, String key) {
try {
JsonElement element = JsonParser.parseString(json);
JsonObject jsonObj = element.getAsJsonObject();
return jsonObj.get(key);
} catch (JsonSyntaxException e) {
throw new GsonException("gson get object from json error, json: {}, key: {}", json, key, e);
}
}
/**
* 向json中添加属性
*
* @return json
*/
public static String add(String json, String key, V value) {
JsonElement element = JsonParser.parseString(json);
JsonObject jsonObject = element.getAsJsonObject();
add(jsonObject, key, value);
return jsonObject.toString();
}
/**
* 向json中添加属性
*/
private static void add(JsonObject jsonObject, String key, V value) {
if (value instanceof String) {
jsonObject.addProperty(key, (String)value);
} else if (value instanceof Number) {
jsonObject.addProperty(key, (Number)value);
} else {
jsonObject.addProperty(key, to(value));
}
}
/**
* 除去json中的某个属性
*
* @return json
*/
public static String remove(String json, String key) {
JsonElement element = JsonParser.parseString(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(key);
return jsonObj.toString();
}
/**
* 修改json中的属性
*/
public static String update(String json, String key, V value) {
JsonElement element = JsonParser.parseString(json);
JsonObject jsonObject = element.getAsJsonObject();
jsonObject.remove(key);
add(jsonObject, key, value);
return jsonObject.toString();
}
/**
* 格式化Json(美化)
*
* @return json
*/
public static String format(String json) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = JsonParser.parseString(json);
return gson.toJson(jsonElement);
}
/**
* 判断字符串是否是json
*
* @return json
*/
public static boolean isJson(String json) {
try {
return JsonParser.parseString(json).isJsonObject();
} catch (Exception e) {
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy