zhaowenyu
2026-04-01 0a0174e3b784f7f9f7c3abfb419c23ee26709d0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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));
    }
}