Jackson 库中的 ObjectMapper

定义实体类用于序列化的对象

import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {
    @JsonProperty("name")
    private String fullName;
    private int age;

    public Person() {
    }

    public Person(String fullName, int age) {
        this.fullName = fullName;
        this.age = age;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

使用单元测试进行测试

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JacksonSerializationDeserializationTest {
    private ObjectMapper objectMapper;

    @BeforeEach
    public void setUp() {
        // 初始化ObjectMapper,并设置一些配置选项
        objectMapper = new ObjectMapper();

        // 忽略未知属性,使序列化/反序列化更灵活
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 设置日期格式,例如:yyyy-MM-dd
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        objectMapper.setDateFormat(dateFormat);

        // 启用日期时间戳格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }


    @Test
    public void testSerializeAndDeserializePerson() throws Exception {
        // 创建一个Person对象
        Person originalPerson = new Person("IKUN", 25);

        // 序列化Person对象为JSON字符串
        String json = objectMapper.writeValueAsString(originalPerson);

        // 反序列化JSON字符串为Person对象
        Person deserializedPerson = objectMapper.readValue(json, Person.class);

        // 断言原始对象和反序列化后的对象是否相等
        assertEquals(originalPerson.getFullName(), deserializedPerson.getFullName());
        assertEquals(originalPerson.getAge(), deserializedPerson.getAge());
    }

    @Test
    public void testSerializeAndDeserializeList() throws Exception {
        // 创建一个List集合
        List<String> stringList = Arrays.asList("Apple", "Banana", "Cherry");
        String json = objectMapper.writeValueAsString(stringList);

        // 反序列化JSON字符串为List集合
        List<String> deserializedList = objectMapper.readValue(json, List.class);

        // 断言原始集合和反序列化后的集合是否相等
        assertEquals(stringList, deserializedList);
    }

    @Test
    public void testSerializeAndDeserializeMap() throws Exception {
        // 创建一个Map
        Map<String, Integer> numberMap = new HashMap<>();
        numberMap.put("One", 1);
        numberMap.put("Two", 2);
        String json = objectMapper.writeValueAsString(numberMap);

        // 反序列化JSON字符串为Map
        Map<String, Integer> deserializedMap = objectMapper.readValue(json, Map.class);

        // 断言原始Map和反序列化后的Map是否相等
        assertEquals(numberMap, deserializedMap);
    }
}


你可能感兴趣的:(java)