创建 JsonConverter.java

pull/1/head
wangsiyuan 2023-12-14 12:44:03 +08:00
parent f540a68ae9
commit d0456431d6
1 changed files with 22 additions and 0 deletions

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);
}
}