diff --git a/src/main/java/tabby/common/bean/converter/List2JsonStringConverter.java b/src/main/java/tabby/common/bean/converter/List2JsonStringConverter.java index e8b79aa..30e83af 100644 --- a/src/main/java/tabby/common/bean/converter/List2JsonStringConverter.java +++ b/src/main/java/tabby/common/bean/converter/List2JsonStringConverter.java @@ -1,10 +1,10 @@ package tabby.common.bean.converter; import com.google.gson.reflect.TypeToken; -import tabby.config.GlobalConfiguration; - import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; +import tabby.common.utils.JsonUtils; + import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -22,7 +22,7 @@ public String convertToDatabaseColumn(List attribute) { return ""; } - return GlobalConfiguration.GSON.toJson(attribute); + return JsonUtils.toJson(attribute); } @Override @@ -31,6 +31,6 @@ public List convertToEntityAttribute(String dbData) { return new ArrayList<>(); } Type objectType = new TypeToken>(){}.getType(); - return GlobalConfiguration.GSON.fromJson(dbData, objectType); + return JsonUtils.fromJson(dbData, objectType); } } diff --git a/src/main/java/tabby/common/bean/converter/ListInteger2JsonStringConverter.java b/src/main/java/tabby/common/bean/converter/ListInteger2JsonStringConverter.java index 9cf0a9d..1fa0c7e 100644 --- a/src/main/java/tabby/common/bean/converter/ListInteger2JsonStringConverter.java +++ b/src/main/java/tabby/common/bean/converter/ListInteger2JsonStringConverter.java @@ -1,10 +1,10 @@ package tabby.common.bean.converter; import com.google.gson.reflect.TypeToken; -import tabby.config.GlobalConfiguration; - import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; +import tabby.common.utils.JsonUtils; + import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -22,7 +22,7 @@ public String convertToDatabaseColumn(List attribute) { return ""; } - return GlobalConfiguration.GSON.toJson(attribute); + return JsonUtils.toJson(attribute); } @Override @@ -31,6 +31,6 @@ public List convertToEntityAttribute(String dbData) { return new ArrayList<>(); } Type objectType = new TypeToken>(){}.getType(); - return GlobalConfiguration.GSON.fromJson(dbData, objectType); + return JsonUtils.fromJson(dbData, objectType); } } diff --git a/src/main/java/tabby/common/bean/converter/ListSet2JsonStringConverter.java b/src/main/java/tabby/common/bean/converter/ListSet2JsonStringConverter.java index bb0a9c6..9ec189f 100644 --- a/src/main/java/tabby/common/bean/converter/ListSet2JsonStringConverter.java +++ b/src/main/java/tabby/common/bean/converter/ListSet2JsonStringConverter.java @@ -1,10 +1,10 @@ package tabby.common.bean.converter; import com.google.gson.reflect.TypeToken; -import tabby.config.GlobalConfiguration; - import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; +import tabby.common.utils.JsonUtils; + import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -23,7 +23,7 @@ public String convertToDatabaseColumn(List> attribute) { return ""; } - return GlobalConfiguration.GSON.toJson(attribute); + return JsonUtils.toJson(attribute); } @Override @@ -32,6 +32,6 @@ public List> convertToEntityAttribute(String dbData) { return new ArrayList<>(); } Type objectType = new TypeToken>>(){}.getType(); - return GlobalConfiguration.GSON.fromJson(dbData, objectType); + return JsonUtils.fromJson(dbData, objectType); } } diff --git a/src/main/java/tabby/common/bean/converter/Map2JsonStringConverter.java b/src/main/java/tabby/common/bean/converter/Map2JsonStringConverter.java index 056e3d8..c4745a2 100644 --- a/src/main/java/tabby/common/bean/converter/Map2JsonStringConverter.java +++ b/src/main/java/tabby/common/bean/converter/Map2JsonStringConverter.java @@ -1,10 +1,10 @@ package tabby.common.bean.converter; import com.google.gson.reflect.TypeToken; -import tabby.config.GlobalConfiguration; - import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; +import tabby.common.utils.JsonUtils; + import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; @@ -21,7 +21,7 @@ public String convertToDatabaseColumn(Map attribute) { if(attribute == null){ return "{}"; } - return GlobalConfiguration.GSON.toJson(attribute); + return JsonUtils.toJson(attribute); } @Override @@ -30,6 +30,6 @@ public Map convertToEntityAttribute(String dbData) { return new HashMap<>(); } Type objectType = new TypeToken>(){}.getType(); - return GlobalConfiguration.GSON.fromJson(dbData, objectType); + return JsonUtils.fromJson(dbData, objectType); } } diff --git a/src/main/java/tabby/common/bean/converter/Map2JsonStringForAnnotationsConverter.java b/src/main/java/tabby/common/bean/converter/Map2JsonStringForAnnotationsConverter.java index a0a9bd5..93f91cb 100644 --- a/src/main/java/tabby/common/bean/converter/Map2JsonStringForAnnotationsConverter.java +++ b/src/main/java/tabby/common/bean/converter/Map2JsonStringForAnnotationsConverter.java @@ -1,10 +1,10 @@ package tabby.common.bean.converter; import com.google.gson.reflect.TypeToken; -import tabby.config.GlobalConfiguration; - import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; +import tabby.common.utils.JsonUtils; + import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; @@ -23,7 +23,8 @@ public String convertToDatabaseColumn(Map>> attr if(attribute == null){ return "{}"; } - return GlobalConfiguration.GSON.toJson(attribute); + + return JsonUtils.toJsonWithReplace(attribute); } @Override @@ -32,6 +33,6 @@ public Map>> convertToEntityAttribute(String dbD return new HashMap<>(); } Type objectType = new TypeToken>>>(){}.getType(); - return new ConcurrentHashMap<>(GlobalConfiguration.GSON.fromJson(dbData, objectType)); + return new ConcurrentHashMap<>(JsonUtils.fromJson(dbData, objectType)); } } diff --git a/src/main/java/tabby/common/utils/JsonUtils.java b/src/main/java/tabby/common/utils/JsonUtils.java new file mode 100644 index 0000000..4f696d2 --- /dev/null +++ b/src/main/java/tabby/common/utils/JsonUtils.java @@ -0,0 +1,41 @@ +package tabby.common.utils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; + +import java.lang.reflect.Type; + +/** + * 为了解决 csv "" \" 同时存在时,可能导致的解析报错 + * @author wh1t3p1g + * @project tabby + * @since 2024/3/23 + */ +public class JsonUtils { + + public static Gson GSON = new GsonBuilder().disableHtmlEscaping().create(); + + public static String toJson(Object obj){ + return GSON.toJson(obj); + } + + public static T fromJson(String json, Type typeOfT) throws JsonSyntaxException { + return GSON.fromJson(json, typeOfT); + } + + /** + * 为了解决 csv "" \" 同时存在时,可能导致的解析报错 + * @param obj + * @return + */ + public static String toJsonWithReplace(Object obj){ + String data = GSON.toJson(obj); + return data.replace("\\", "\\\\"); + } + + public static T fromJsonWithReplace(String json, Type typeOfT) throws JsonSyntaxException { + json = json.replace("\\\\", "\\"); + return GSON.fromJson(json, typeOfT); + } +}