创建 JsonConverter.java

This commit is contained in:
2023-12-14 12:44:03 +08:00
parent f540a68ae9
commit d0456431d6

View File

@@ -0,0 +1,22 @@
package com.kimgo.wepush.common;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonConverter<T> {
private final ObjectMapper objectMapper;
public JsonConverter () {
this.objectMapper = new ObjectMapper();
}
// 将对象序列化为 JSON 字符串
public String serialize(T object) throws JsonProcessingException {
return objectMapper.writeValueAsString(object);
}
// 将 JSON 字符串反序列化为对象
public T deserialize(String json, Class<T> clazz) throws JsonProcessingException {
return objectMapper.readValue(json, clazz);
}
}