GsonUtils
package com.lagou.edu.stream;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import lombok.SneakyThrows;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class GSON {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
private static final JsonSerializer<LocalDateTime> dateTimeSerializer = (obj, type, ctx) -> new JsonPrimitive(dateTimeFormatter.format(obj));
private static final JsonSerializer<LocalDate> dateSerializer = (obj, type, ctx) -> new JsonPrimitive(dateFormatter.format(obj));
private static final JsonSerializer<LocalTime> timeSerializer = (obj, type, ctx) -> new JsonPrimitive(timeFormatter.format(obj));
private static final JsonDeserializer<LocalDateTime> dateTimeDeserializer = (json, type, ctx) -> LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), dateTimeFormatter);
private static final JsonDeserializer<LocalDate> dateDeserializer = (json, type, ctx) -> LocalDate.parse(json.getAsJsonPrimitive().getAsString(), dateFormatter);
private static final JsonDeserializer<LocalTime> timeDeserializer = (json, type, ctx) -> LocalTime.parse(json.getAsJsonPrimitive().getAsString(), timeFormatter);
private static final Gson INSTANCE;
static {
GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
builder.enableComplexMapKeySerialization();
// builder.excludeFieldsWithoutExposeAnnotation();
builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
builder.registerTypeAdapter(LocalDateTime.class, dateTimeSerializer);
builder.registerTypeAdapter(LocalDate.class, dateSerializer);
builder.registerTypeAdapter(LocalTime.class, timeSerializer);
builder.registerTypeAdapter(LocalDateTime.class, dateTimeDeserializer);
builder.registerTypeAdapter(LocalDate.class, dateDeserializer);
builder.registerTypeAdapter(LocalTime.class, timeDeserializer);
INSTANCE = builder.create();
}
public static Type makeJavaType(Type rawType, Type... typeArguments) {
return TypeToken.getParameterized(rawType, typeArguments).getType();
}
public static String toString(Object value) {
if (Objects.isNull(value)) {
return null;
}
if (value instanceof String) {
return (String) value;
}
return toJsonStr(value);
}
public static String toJsonStr(Object value) {
return INSTANCE.toJson(value);
}
public static String toPrettyString(Object value) {
return INSTANCE.newBuilder().setPrettyPrinting().create().toJson(value);
}
public static JsonElement fromJavaObject(Object value) {
JsonElement result = null;
if (Objects.nonNull(value) && (value instanceof String)) {
result = parseObject((String) value);
} else {
result = INSTANCE.toJsonTree(value);
}
return result;
}
@SneakyThrows
public static JsonElement parseObject(String content) {
return JsonParser.parseString(content);
}
public static JsonElement getJsonElement(JsonObject node, String name) {
return node.get(name);
}
public static JsonElement getJsonElement(JsonArray node, int index) {
return node.get(index);
}
@SneakyThrows
public static <T> T toObject(JsonElement node, Class<T> clazz) {
return INSTANCE.fromJson(node, clazz);
}
@SneakyThrows
public static <T> T toObject(JsonElement node, Type type) {
return INSTANCE.fromJson(node, type);
}
public static <T> T toObject(JsonElement node, TypeToken<?> typeToken) {
return toObject(node, typeToken.getType());
}
public static <E> List<E> toList(JsonElement node, Class<E> clazz) {
return toObject(node, makeJavaType(List.class, clazz));
}
public static List<Object> toList(JsonElement node) {
return toObject(node, new TypeToken<List<Object>>() {
}.getType());
}
public static <V> Map<String, V> toMap(JsonElement node, Class<V> clazz) {
return toObject(node, makeJavaType(Map.class, String.class, clazz));
}
public static Map<String, Object> toMap(JsonElement node) {
return toObject(node, new TypeToken<Map<String, Object>>() {
}.getType());
}
@SneakyThrows
public static <T> T toObject(String content, Class<T> clazz) {
return INSTANCE.fromJson(content, clazz);
}
@SneakyThrows
public static <T> T toObject(String content, Type type) {
return INSTANCE.fromJson(content, type);
}
public static <T> T toObject(String content, TypeToken<?> typeToken) {
return toObject(content, typeToken.getType());
}
public static <E> List<E> toList(String content, Class<E> clazz) {
return toObject(content, makeJavaType(List.class, clazz));
}
public static List<Object> toList(String content) {
return toObject(content, new TypeToken<List<Object>>() {
}.getType());
}
public static <V> Map<String, V> toMap(String content, Class<V> clazz) {
return toObject(content, makeJavaType(Map.class, String.class, clazz));
}
public static Map<String, Object> toMap(String content) {
return toObject(content, new TypeToken<Map<String, Object>>() {
}.getType());
}
}