package com.hdl.linkpm.sdk.utils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; /** * Created by Tong on 2021/11/10. */ public class HDLGsonUtils { private static Gson gson; public static T fromJson(String json, Type type) { Gson gson = getGson(); return gson.fromJson(json, type); } public static String toJson(Object object) { return getGson().toJson(object); } public static Gson getGson() { if (gson == null) { gson = new GsonBuilder() .disableHtmlEscaping() .registerTypeAdapter(String.class, new StringAdapter()) .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter()) .registerTypeAdapter(Double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(Long.class, new LongDefault0Adapter()) .create(); } return gson; } private static class StringAdapter implements JsonSerializer, JsonDeserializer { @Override public String deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json instanceof JsonPrimitive) { return json.getAsString(); } else { return json.toString(); } } @Override public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } private static class IntegerDefault0Adapter implements JsonSerializer, JsonDeserializer { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) { return 0; } } catch (Exception ignore) { } try { return json.getAsInt(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } private static class DoubleDefault0Adapter implements JsonSerializer, JsonDeserializer { @Override public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) { return 0.00; } } catch (Exception ignore) { } try { return json.getAsDouble(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } private static class LongDefault0Adapter implements JsonSerializer, JsonDeserializer { @Override public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) { return 0L; } } catch (Exception ignore) { } try { return json.getAsLong(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); } } /** * * @param o * @param * @return */ public static JsonArray toJsonArray(Object o) { try { return getGson().fromJson(HDLGsonUtils.toJson(o), new TypeToken() { }.getType()); } catch (Exception e) { return null; } } /** * copyProperties * @param o * @param type * @param * @return */ public static T copyProperties(Object o, Type type) { Gson gson = getGson(); return gson.fromJson(gson.toJson(o), type); } }