jackson中常用注解

 

  • 项目摘要: 需要安装lombok插件
  • 项目具体实施:
  1. pom引入:

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.5.3</version>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.12</version>

<scope>provided</scope>

</dependency>

<dependency>

      <groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13</version>

<scope>test</scope>

</dependency>

  1. 主类:

package com.jackson.demo;

/*************import**********/

@Data

@AllArgsConstructor

@NoArgsConstructor

public class CustomerInfo {

     private int id;

     /*

      * shap: 表示序列化后的一种类型 pattern: 表示日期的格式 timezone: 默认是GMT,中国需要GMT+8 locale:

      * 根据位置序列化的一种格式

      */

     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy年MM月dd日 HH:mm:ss", timezone = "GMT+8")

     private Date date;

     /*

      * 属性值为null的不参与序列化

      */

     @JsonInclude(Include.NON_NULL)

     @JsonProperty("customer_name")

     private String customerName;

     // 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称

     @JsonProperty("customer_id")

     private String customerId;

     /*

      * 此注解用于属性或者方法上(最好是属性上),用来完全忽略被注解的字段和方法对应的属性,即便这个字段或方法可以被自动检测到或者还有其

      * 他的注解,一般标记在属性或者方法上,返回的json数据即不包含该属性。

      */

     @JsonIgnore

     private String productId;

     @JsonProperty("source_address")

     private String sourceAddress;

     /*

      * 此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码

      */

     @JsonSerialize(using = OtherInfoSerialize.class)

     /*

      * @JsonDeserialize此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码

      */

     @JsonDeserialize(using = OtherInfoDeserialize.class)

     private String other;

}

 

class OtherInfoSerialize extends JsonSerializer<String> {

     @Override

     public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializers)

                throws IOException, JsonProcessingException {

           System.out.println(OtherInfoSerialize.class.getName());

           if (value == null || value.length() == 0) {

                jsonGenerator.writeString("没有信息");

           } else {

                jsonGenerator.writeString(value);

           }

     }

}

 

class OtherInfoDeserialize extends JsonDeserializer<String> {

 

     @Override

     public String deserialize(JsonParser jsonParser, DeserializationContext context)

                throws IOException, JsonProcessingException {

           System.out.println(OtherInfoDeserialize.class.getName());

           if (jsonParser == null || jsonParser.getText().isEmpty()) {

                return "没有信息";

           } else {

                return jsonParser.getText();

           }

     }

}

  1. 测试类

package Demo;

/*************import**********/

public class TestJackSon {

     @Test

     public void testJackSonDemo() throws JsonProcessingException {

           ObjectMapper mapper = new ObjectMapper();

           List<CustomerInfo> customerInfos=new ArrayList<CustomerInfo>() { 

                private static final long serialVersionUID = 1L;

           {

                add(new CustomerInfo(1,new Date(),"张三", UUID.randomUUID().toString(), "123", "陕西省",""));

                add(new CustomerInfo(2,new Date(),null, UUID.randomUUID().toString(), "123", "陕西省","哈哈"));

                add(new CustomerInfo(3,new Date(),"李四", UUID.randomUUID().toString(), "123", "陕西省",null));

           }};

           String json = mapper.writeValueAsString(customerInfos);

           System.out.println(json);

  }

}

  • 参考链接:

1. jackson使用教程: https://www.cnblogs.com/zjdxr-up/p/9737133.html

本内容由“丫丫”原创。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值