remove gson
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.imyeyu.api.modules.common.controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imyeyu.api.bean.CaptchaFrom;
|
||||
import com.imyeyu.api.modules.common.bean.ImageType;
|
||||
import com.imyeyu.api.modules.common.bean.SettingKey;
|
||||
@@ -95,12 +95,28 @@ public class CommonController {
|
||||
private final AttachmentService attachmentService;
|
||||
private final ClipboardService clipboardService;
|
||||
|
||||
private final Gson gson;
|
||||
private final ObjectMapper jackson;
|
||||
private final Yaml yaml;
|
||||
private final GridFSBucket gridFSBucket;
|
||||
private final CaptchaManager captchaManager;
|
||||
private final ResourceHandler resourceHandler;
|
||||
|
||||
private String writeJson(Object value) {
|
||||
try {
|
||||
return jackson.writeValueAsString(value);
|
||||
} catch (IOException e) {
|
||||
throw new TimiException(TimiCode.ERROR, "write json error", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> readJsonMap(String value) {
|
||||
try {
|
||||
return jackson.readValue(value, new TypeReference<>() {});
|
||||
} catch (IOException e) {
|
||||
throw new TimiException(TimiCode.ERROR, "read json error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@AOPLog
|
||||
@RequestMapping("")
|
||||
public String root() {
|
||||
@@ -253,12 +269,12 @@ public class CommonController {
|
||||
case JSON -> {
|
||||
if (setting.getType() == Setting.Type.YAML) {
|
||||
Map<String, Object> obj = yaml.load(setting.getValue());
|
||||
result = gson.toJson(obj);
|
||||
result = writeJson(obj);
|
||||
}
|
||||
}
|
||||
case YAML -> {
|
||||
if (setting.getType() == Setting.Type.JSON) {
|
||||
Map<String, Object> obj = gson.fromJson(setting.getValue(), new TypeToken<Map<String, Object>>() {}.getType());
|
||||
Map<String, Object> obj = readJsonMap(setting.getValue());
|
||||
result = yaml.dump(obj);
|
||||
}
|
||||
}
|
||||
@@ -284,12 +300,12 @@ public class CommonController {
|
||||
case JSON -> {
|
||||
if (setting.getType() == Setting.Type.YAML) {
|
||||
Map<String, Object> obj = new Yaml().load(setting.getValue());
|
||||
setting.setValue(gson.toJson(obj));
|
||||
setting.setValue(writeJson(obj));
|
||||
}
|
||||
}
|
||||
case YAML -> {
|
||||
if (setting.getType() == Setting.Type.JSON) {
|
||||
Map<String, Object> obj = gson.fromJson(setting.getValue(), new TypeToken<Map<String, Object>>() {}.getType());
|
||||
Map<String, Object> obj = readJsonMap(setting.getValue());
|
||||
setting.setValue(new Yaml().dump(obj));
|
||||
}
|
||||
}
|
||||
@@ -483,7 +499,7 @@ public class CommonController {
|
||||
GridFSFile file = attachmentService.readByMongoId(mongoId);
|
||||
@Cleanup
|
||||
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(file.getObjectId());
|
||||
RequestRange range = TimiSpring.requestRange(attach.getSize());
|
||||
RequestRange range = TimiSpring.getRequestRange(attach.getSize());
|
||||
if (range == null) {
|
||||
// 完整文件
|
||||
resp.setContentLengthLong(attach.getSize());
|
||||
|
||||
@@ -26,7 +26,6 @@ import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import com.imyeyu.spring.annotation.RequestSingleParam;
|
||||
import com.imyeyu.spring.annotation.RequiredToken;
|
||||
import com.imyeyu.spring.bean.CaptchaData;
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
@@ -184,7 +183,7 @@ public class UserController implements TimiJava {
|
||||
@RequiredToken
|
||||
@RequestRateLimit
|
||||
@PostMapping("/cancel")
|
||||
public void cancel(@RequestSingleParam String password) {
|
||||
public void cancel(@RequestBody String password) {
|
||||
service.cancel(password);
|
||||
}
|
||||
|
||||
@@ -275,7 +274,7 @@ public class UserController implements TimiJava {
|
||||
@RequiredToken
|
||||
@RequestRateLimit
|
||||
@PostMapping("/comment/delete")
|
||||
public void deleteComment(@RequestSingleParam Long commentId) {
|
||||
public void deleteComment(@RequestBody Long commentId) {
|
||||
commentService.get(commentId);
|
||||
commentService.delete(commentId);
|
||||
}
|
||||
@@ -298,7 +297,7 @@ public class UserController implements TimiJava {
|
||||
@RequiredToken
|
||||
@RequestRateLimit
|
||||
@PostMapping("/comment/reply/delete")
|
||||
public void deleteCommentReply(@RequestSingleParam Long replyId) {
|
||||
public void deleteCommentReply(@RequestBody Long replyId) {
|
||||
CommentReply reply = commentReplyService.get(replyId);
|
||||
TimiException.requiredTrue(reply.getSenderId().equals(service.getLoginUser().getId()), "user.comment.reply.delete.not_owner");
|
||||
commentReplyService.delete(replyId);
|
||||
@@ -308,7 +307,7 @@ public class UserController implements TimiJava {
|
||||
@RequiredToken
|
||||
@RequestRateLimit
|
||||
@PostMapping("/comment/reply/ignore")
|
||||
public void ignoreCommentReply(@RequestSingleParam Long replyId) {
|
||||
public void ignoreCommentReply(@RequestBody Long replyId) {
|
||||
CommentReply reply = commentReplyService.get(replyId);
|
||||
TimiException.requiredTrue(reply.getReceiverId().equals(service.getLoginUser().getId()), "user.comment.reply.ignore.not_owner");
|
||||
reply.setIgnoredAt(Time.now());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.imyeyu.api.modules.common.entity;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.imyeyu.api.TimiServerAPI;
|
||||
import com.imyeyu.api.bean.MultilingualHandler;
|
||||
import com.imyeyu.api.modules.common.service.AttachmentService;
|
||||
@@ -81,7 +81,7 @@ public class Attachment extends Entity implements MultilingualHandler {
|
||||
|
||||
protected String mimeType;
|
||||
|
||||
protected JsonObject metadata;
|
||||
protected JsonNode metadata;
|
||||
|
||||
protected Long size;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
+9
-9
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
+34
-18
@@ -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) {
|
||||
|
||||
-4
@@ -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;
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.imyeyu.api.modules.common.task;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
|
||||
import com.imyeyu.api.modules.common.bean.SettingKey;
|
||||
import com.imyeyu.api.modules.common.entity.Multilingual;
|
||||
@@ -81,6 +80,7 @@ public class MultilingualTranslateTask {
|
||||
}
|
||||
}
|
||||
|
||||
private final ObjectMapper jackson;
|
||||
private final SettingService settingService;
|
||||
private final MultilingualService service;
|
||||
|
||||
@@ -147,18 +147,17 @@ public class MultilingualTranslateTask {
|
||||
.execute()
|
||||
.returnContent()
|
||||
.asString();
|
||||
JsonObject jo = JsonParser.parseString(response).getAsJsonObject();
|
||||
JsonNode jo = jackson.readTree(response);
|
||||
if (jo.has("error_code")) {
|
||||
System.err.println(jo);
|
||||
throw new TimiException(TimiCode.ERROR, jo.get("error_msg").getAsString());
|
||||
throw new TimiException(TimiCode.ERROR, jo.get("error_msg").asText());
|
||||
}
|
||||
JsonArray ja = jo.get("trans_result").getAsJsonArray();
|
||||
JsonNode ja = jo.get("trans_result");
|
||||
|
||||
JsonObject resultJO;
|
||||
Map<String, String> result = new HashMap<>();
|
||||
for (int i = 0; i < ja.size(); i++) {
|
||||
resultJO = ja.get(i).getAsJsonObject();
|
||||
result.put(resultJO.get("src").getAsString(), resultJO.get("dst").getAsString());
|
||||
JsonNode resultJO = ja.get(i);
|
||||
result.put(resultJO.get("src").asText(), resultJO.get("dst").asText());
|
||||
}
|
||||
wait(200);
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user