Skip to content

Commit

Permalink
fix csv load error
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1t3p1g committed Jun 13, 2024
1 parent b3fa31f commit 6d90e5b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,7 +22,7 @@ public String convertToDatabaseColumn(List<String> attribute) {
return "";
}

return GlobalConfiguration.GSON.toJson(attribute);
return JsonUtils.toJson(attribute);
}

@Override
Expand All @@ -31,6 +31,6 @@ public List<String> convertToEntityAttribute(String dbData) {
return new ArrayList<>();
}
Type objectType = new TypeToken<List<String>>(){}.getType();
return GlobalConfiguration.GSON.fromJson(dbData, objectType);
return JsonUtils.fromJson(dbData, objectType);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,7 +22,7 @@ public String convertToDatabaseColumn(List<Integer> attribute) {
return "";
}

return GlobalConfiguration.GSON.toJson(attribute);
return JsonUtils.toJson(attribute);
}

@Override
Expand All @@ -31,6 +31,6 @@ public List<Integer> convertToEntityAttribute(String dbData) {
return new ArrayList<>();
}
Type objectType = new TypeToken<List<Integer>>(){}.getType();
return GlobalConfiguration.GSON.fromJson(dbData, objectType);
return JsonUtils.fromJson(dbData, objectType);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +23,7 @@ public String convertToDatabaseColumn(List<Set<String>> attribute) {
return "";
}

return GlobalConfiguration.GSON.toJson(attribute);
return JsonUtils.toJson(attribute);
}

@Override
Expand All @@ -32,6 +32,6 @@ public List<Set<String>> convertToEntityAttribute(String dbData) {
return new ArrayList<>();
}
Type objectType = new TypeToken<List<Set<String>>>(){}.getType();
return GlobalConfiguration.GSON.fromJson(dbData, objectType);
return JsonUtils.fromJson(dbData, objectType);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +21,7 @@ public String convertToDatabaseColumn(Map<String, String> attribute) {
if(attribute == null){
return "{}";
}
return GlobalConfiguration.GSON.toJson(attribute);
return JsonUtils.toJson(attribute);
}

@Override
Expand All @@ -30,6 +30,6 @@ public Map<String, String> convertToEntityAttribute(String dbData) {
return new HashMap<>();
}
Type objectType = new TypeToken<Map<String, String>>(){}.getType();
return GlobalConfiguration.GSON.fromJson(dbData, objectType);
return JsonUtils.fromJson(dbData, objectType);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +23,8 @@ public String convertToDatabaseColumn(Map<String, Map<String, Set<String>>> attr
if(attribute == null){
return "{}";
}
return GlobalConfiguration.GSON.toJson(attribute);

return JsonUtils.toJsonWithReplace(attribute);
}

@Override
Expand All @@ -32,6 +33,6 @@ public Map<String, Map<String, Set<String>>> convertToEntityAttribute(String dbD
return new HashMap<>();
}
Type objectType = new TypeToken<Map<String, Map<String, Set<String>>>>(){}.getType();
return new ConcurrentHashMap<>(GlobalConfiguration.GSON.fromJson(dbData, objectType));
return new ConcurrentHashMap<>(JsonUtils.fromJson(dbData, objectType));
}
}
41 changes: 41 additions & 0 deletions src/main/java/tabby/common/utils/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -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> 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> T fromJsonWithReplace(String json, Type typeOfT) throws JsonSyntaxException {
json = json.replace("\\\\", "\\");
return GSON.fromJson(json, typeOfT);
}
}

0 comments on commit 6d90e5b

Please sign in to comment.