remove gson

This commit is contained in:
Timi
2026-04-08 11:25:45 +08:00
parent b6a58b7376
commit 8947269351
53 changed files with 492 additions and 844 deletions
@@ -1,9 +1,9 @@
package com.imyeyu.api.modules.common.service;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.imyeyu.api.modules.common.bean.SettingKey;
import com.imyeyu.api.modules.common.entity.Setting;
import com.imyeyu.java.bean.timi.TimiException;
@@ -60,15 +60,15 @@ public interface SettingService extends UpdatableService<Setting> {
*/
boolean not(SettingKey key);
JsonElement getAsJsonElement(SettingKey key);
JsonNode getAsJsonNode(SettingKey key);
JsonObject getAsJsonObject(SettingKey key);
ObjectNode getAsJsonObject(SettingKey key);
JsonArray getAsJsonArray(SettingKey key);
ArrayNode getAsArrayNode(SettingKey key);
<T> T fromJson(SettingKey key, Class<T> clazz);
<T> T fromJson(SettingKey key, TypeToken<T> typeToken);
<T> T fromJson(SettingKey key, TypeReference<T> typeReference);
<T> T fromYaml(SettingKey key, Class<T> clazz);
@@ -1,7 +1,7 @@
package com.imyeyu.api.modules.common.service.implement;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
import com.imyeyu.api.modules.common.bean.MediaAttach;
import com.imyeyu.api.modules.common.bean.Metadata;
@@ -55,7 +55,7 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
private final AttachmentMapper mapper;
private final Gson gson;
private final ObjectMapper jackson;
private final GridFSBucket gridFSBucket;
private final GridFsTemplate gridFsTemplate;
@@ -90,10 +90,10 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
attachment.setIsDestroyed(false);
if (attachment.getMimeType().startsWith("image")) {
BufferedImage image = ImageIO.read(gridFSBucket.openDownloadStream(gridFSFile.getObjectId()));
attachment.setMetadata(TimiJava.defaultIfNull(attachment.getMetadata(), new JsonObject()));
JsonObject metadata = attachment.getMetadata();
metadata.addProperty("width", image.getWidth());
metadata.addProperty("height", image.getHeight());
attachment.setMetadata(TimiJava.defaultIfNull(attachment.getMetadata(), jackson.createObjectNode()));
ObjectNode metadata = (ObjectNode) attachment.getMetadata();
metadata.put("width", image.getWidth());
metadata.put("height", image.getHeight());
}
mapper.insert(attachment);
} catch (Exception e) {
@@ -166,7 +166,7 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
thumbAttach.setBizType(attachment.getBizType());
thumbAttach.setBizId(attachment.getBizId());
thumbAttach.setAttachTypeValue(MediaAttach.Type.THUMB);
thumbAttach.setMetadata(gson.toJsonTree(thumbMetadata).getAsJsonObject());
thumbAttach.setMetadata(jackson.valueToTree(thumbMetadata));
thumbAttach.setInputStream(new ByteArrayInputStream(thumbStream.toByteArray()));
create(thumbAttach);
@@ -182,7 +182,7 @@ public class AttachmentServiceImplement extends AbstractEntityService<Attachment
public void deleteMedia(Long thumbId) throws TimiException {
Attachment attachment = get(thumbId);
delete(attachment.getId());
Metadata.ThumbImage thumbMetadata = gson.fromJson(attachment.getMetadata(), Metadata.ThumbImage.class);
Metadata.ThumbImage thumbMetadata = jackson.convertValue(attachment.getMetadata(), Metadata.ThumbImage.class);
delete(thumbMetadata.getSourceId());
}
@@ -1,11 +1,11 @@
package com.imyeyu.api.modules.common.service.implement;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.imyeyu.api.modules.common.bean.SettingKey;
import com.imyeyu.api.modules.common.entity.Setting;
import com.imyeyu.api.modules.common.mapper.SettingMapper;
@@ -39,7 +39,7 @@ public class SettingServiceImplement extends AbstractEntityService<Setting, Stri
private final SettingMapper mapper;
private final Redis<String, String> redisSetting;
private final Gson gson;
private final ObjectMapper jackson;
@Override
protected BaseMapper<Setting, String> mapper() {
@@ -59,7 +59,11 @@ public class SettingServiceImplement extends AbstractEntityService<Setting, Stri
String cacheValue = redisSetting.get(key.toString());
if (TimiJava.isNotEmpty(cacheValue)) {
return gson.fromJson(cacheValue, Setting.class);
try {
return jackson.readValue(cacheValue, Setting.class);
} catch (JsonProcessingException e) {
throw new TimiException(TimiCode.ERROR, "read setting cache error", e);
}
}
Setting setting = mapper.selectByKey(key);
if (TimiJava.isEmpty(setting.getValue())) {
@@ -72,7 +76,11 @@ public class SettingServiceImplement extends AbstractEntityService<Setting, Stri
settingTTL = Integer.parseInt(getByKey(SettingKey.TTL_SETTING).getValue());
}
if (0 < settingTTL) {
redisSetting.set(key.toString(), gson.toJson(setting), Time.M * settingTTL);
try {
redisSetting.set(key.toString(), jackson.writeValueAsString(setting), Time.M * settingTTL);
} catch (JsonProcessingException e) {
throw new TimiException(TimiCode.ERROR, "write setting cache error", e);
}
}
return setting;
}
@@ -117,28 +125,36 @@ public class SettingServiceImplement extends AbstractEntityService<Setting, Stri
}
@Override
public JsonElement getAsJsonElement(SettingKey key) {
return JsonParser.parseString(getAsString(key));
public JsonNode getAsJsonNode(SettingKey key) {
try {
return jackson.readTree(getAsString(key));
} catch (JsonProcessingException e) {
throw new TimiException(TimiCode.ERROR, "read setting json error", e);
}
}
@Override
public JsonObject getAsJsonObject(SettingKey key) {
return getAsJsonElement(key).getAsJsonObject();
public ObjectNode getAsJsonObject(SettingKey key) {
return (ObjectNode) getAsJsonNode(key);
}
@Override
public JsonArray getAsJsonArray(SettingKey key) {
return getAsJsonElement(key).getAsJsonArray();
public ArrayNode getAsArrayNode(SettingKey key) {
return (ArrayNode) getAsJsonNode(key);
}
@Override
public <T> T fromJson(SettingKey key, Class<T> clazz) {
return gson.fromJson(getAsJsonElement(key), clazz);
try {
return jackson.treeToValue(getAsJsonNode(key), clazz);
} catch (JsonProcessingException e) {
throw new TimiException(TimiCode.ERROR, "read setting json error", e);
}
}
@Override
public <T> T fromJson(SettingKey key, TypeToken<T> typeToken) {
return gson.fromJson(getAsJsonElement(key), typeToken);
public <T> T fromJson(SettingKey key, TypeReference<T> typeReference) {
return jackson.convertValue(getAsJsonNode(key), typeReference);
}
public <T> T fromYaml(SettingKey key, Class<T> clazz) {
@@ -1,6 +1,4 @@
package com.imyeyu.api.modules.common.service.implement;
import com.google.gson.Gson;
import com.imyeyu.api.modules.common.bean.SettingKey;
import com.imyeyu.api.modules.common.entity.Attachment;
import com.imyeyu.api.modules.common.service.AttachmentService;
@@ -31,8 +29,6 @@ import java.util.List;
@RequiredArgsConstructor
public class TempFileServiceImplement implements TempFileService {
private final Gson gson;
private final SettingService settingService;
private final AttachmentService attachmentService;