remove gson
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
package com.imyeyu.api.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.api.TimiServerAPI;
|
||||
import com.imyeyu.api.bean.MultilingualHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @version 2023-10-26 10:16
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class GsonSerializerAdapter implements JsonSerializer<Object> {
|
||||
|
||||
private final Gson gson;
|
||||
private final RedisMultilingual redisMultilingual;
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Object value, Type typeOfSrc, JsonSerializationContext context) {
|
||||
if (value instanceof MultilingualHandler _value) {
|
||||
fillMultilingual(_value);
|
||||
}
|
||||
return gson.toJsonTree(value);
|
||||
}
|
||||
|
||||
private <K extends MultilingualHandler> void fillMultilingual(K value) {
|
||||
try {
|
||||
List<Field> fields = Ref.listFields(value.getClass());
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
Field field = fields.get(i);
|
||||
MultilingualHandler.MultilingualField multiField = field.getAnnotation(MultilingualHandler.MultilingualField.class);
|
||||
if (multiField != null) {
|
||||
String multiLangId = Ref.getFieldValue(value, field, String.class);
|
||||
if (TimiJava.isNotEmpty(multiLangId)) {
|
||||
Long langId = Long.parseLong(multiLangId);
|
||||
if (redisMultilingual.map(TimiServerAPI.getUserLanguage()) instanceof RedisLanguage rl) {
|
||||
// TODO 支持插值参数
|
||||
Ref.setFieldValue(value, field, rl.text(langId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.imyeyu.api.util;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.imyeyu.api.TimiServerAPI;
|
||||
import com.imyeyu.api.modules.common.bean.SettingKey;
|
||||
import com.imyeyu.api.modules.common.entity.Multilingual;
|
||||
@@ -88,22 +88,22 @@ public class InitApplication implements ApplicationRunner {
|
||||
}
|
||||
|
||||
private void initFileType() {
|
||||
JsonObject items = settingService.getAsJsonObject(SettingKey.SYSTEM_FILE_TYPE);
|
||||
ObjectNode items = settingService.getAsJsonObject(SettingKey.SYSTEM_FILE_TYPE);
|
||||
|
||||
String[] extensions;
|
||||
JsonArray extensionsArray;
|
||||
JsonObject itemObject;
|
||||
ArrayNode extensionsArray;
|
||||
JsonNode itemObject;
|
||||
List<String> extensionsList;
|
||||
for (Map.Entry<String, JsonElement> item : items.entrySet()) {
|
||||
for (Map.Entry<String, JsonNode> item : (Iterable<Map.Entry<String, JsonNode>>) items::fields) {
|
||||
ServerFile.FileType fileType = Ref.toType(ServerFile.FileType.class, item.getKey());
|
||||
itemObject = item.getValue().getAsJsonObject();
|
||||
itemObject = item.getValue();
|
||||
extensionsList = new ArrayList<>();
|
||||
extensionsArray = itemObject.get("extensions").getAsJsonArray();
|
||||
for (int i = 0; i < extensionsArray.size(); i++) {
|
||||
if (extensionsArray.get(i).isJsonObject()) {
|
||||
extensionsList.add(extensionsArray.get(i).getAsJsonObject().get("value").getAsString());
|
||||
extensionsArray = (ArrayNode) itemObject.get("extensions");
|
||||
for (JsonNode extensionNode : extensionsArray) {
|
||||
if (extensionNode.isObject()) {
|
||||
extensionsList.add(extensionNode.path("value").asText());
|
||||
} else {
|
||||
extensionsList.add(extensionsArray.get(i).getAsString());
|
||||
extensionsList.add(extensionNode.asText());
|
||||
}
|
||||
}
|
||||
extensions = new String[extensionsList.size()];
|
||||
|
||||
Reference in New Issue
Block a user