fastxml Jackson annotation使用小记

Posted 清风小筑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastxml Jackson annotation使用小记相关的知识,希望对你有一定的参考价值。

dependencies:

compile("com.fasterxml.jackson.core:jackson-annotations")
compile("com.fasterxml.jackson.core:jackson-core")
compile("com.fasterxml.jackson.core:jackson-databind")

examples:

public class JacksonSerializerTest {

    @Test
    public void testJsonIgnoreProperties() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        PersonDTO dto = new PersonDTO();
        dto.setAge(18);
        dto.setName("Jack");
        dto.setId(1);

        String jsonStr = mapper.writeValueAsString(dto);
        PersonDTO dto1 = mapper.readValue(jsonStr, PersonDTO.class);

        assertTrue(dto1.getName() == null);

        String jsonStr1 = "{\"id\":1, \"name\":\"Jack\"}";
        PersonDTO dto2 = mapper.readValue(jsonStr1, PersonDTO.class);
        assertTrue(dto2.getName() == null);
    }

    @Test
    public void testJsonIgnore() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        PersonDTO dto = new PersonDTO();
        dto.setId(2);
        dto.setMale(true);
        String jsonStr = mapper.writeValueAsString(dto);
        PersonDTO dto1 = mapper.readValue(jsonStr, PersonDTO.class);

        assertTrue(dto1.getMale() == false);
    }

    @Test
    public void testCustomSerializer() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        PersonDTO dto = new PersonDTO();
        dto.setId(3);
        dto.setWeight(Double.valueOf(65.371));

        String jsonStr = mapper.writeValueAsString(dto);
        PersonDTO dto1 = mapper.readValue(jsonStr, PersonDTO.class);

        assertTrue(dto1.getWeight().equals(Double.valueOf(65.4)));
    }

    @Test
    public void testJsonNaming() throws IOException {
        String nickName = "JK.";
        ObjectMapper mapper = new ObjectMapper();
        PersonDTO dto = new PersonDTO();
        dto.setId(4);
        dto.setNickName(nickName);

        String jsonStr = mapper.writeValueAsString(dto);
        PersonDTO dto1 = mapper.readValue(jsonStr, PersonDTO.class);

        assertTrue(dto1.getNickName().equals(nickName));
    }

    @JsonIgnoreProperties(value = {"name", "age"})
    @Data
    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
    public static class PersonDTO {
        private int id;
        private String name;
        private int age;
        @JsonIgnore
        private boolean male;
        @JsonSerialize(using = CustomDoubleSerialize.class)
        private Double weight;

        private String nickName;

        public boolean getMale() {
            return this.male;
        }
    }

    public static class CustomDoubleSerialize extends JsonSerializer<Double> {

        private DecimalFormat df = new DecimalFormat("##.0");

        @Override

        public void serialize(Double value, JsonGenerator jgen,SerializerProvider provider) throws IOException,JsonProcessingException {

            jgen.writeString(df.format(value));

        }

    }

}

 

以上是关于fastxml Jackson annotation使用小记的主要内容,如果未能解决你的问题,请参考以下文章

Apache Beam 的 BigQueryIO (Java):无法将 TIMESTAMP 字段写入 BigQuery——fastxml.jackson 异常“类型不支持”

如何在没有 SparkSQL 的情况下使用 fastxml 解析 Spark 中的 JSON?

eclipse tomcat启动项目报错:Invalid byte tag in constant pool: 19 jackson-core-2.10.1.jar/] for annotatio

在fastxml的ObjectMapper中使用SimpleDateFormat是不是安全[重复]

Spring Data JPA:在两个方向上保持多对可能的关系

Jackson怎么读 英语jackson怎么读