创建 JsonConverter.java
This commit is contained in:
22
src/main/java/com/kimgo/wepush/common/JsonConverter.java
Normal file
22
src/main/java/com/kimgo/wepush/common/JsonConverter.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user