package com.ard.sample.annotation.util;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.ard.sample.annotation.model.dto.AnnotationPointsDTO;
|
import org.springframework.stereotype.Component;
|
|
import java.util.List;
|
|
@Component
|
public class JsonSerializeUtil {
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
// 序列化:List<AnnotationPointsDTO> → byte[]
|
public static byte[] serialize(List<AnnotationPointsDTO> points) throws JsonProcessingException {
|
if (points == null) {
|
return null;
|
}
|
String json = objectMapper.writeValueAsString(points);
|
return json.getBytes();
|
}
|
|
// 反序列化:byte[] → List<AnnotationPointsDTO>
|
public static List<AnnotationPointsDTO> deserialize(byte[] bytes) throws JsonProcessingException {
|
if (bytes == null || bytes.length == 0) {
|
return null;
|
}
|
String json = new String(bytes);
|
return objectMapper.readValue(json,
|
objectMapper.getTypeFactory().constructCollectionType(List.class, AnnotationPointsDTO.class));
|
}
|
}
|