创建 JsonConverter.java
parent
f540a68ae9
commit
d0456431d6
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue