Jackson解析,@JsonIgnore、@JsonFormat 、@JsonIgnoreProperties 、自定义序列化器用法

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/82228213

@JsonIgnore、@JsonFormat 用法:

public class User {

    @JsonIgnore//默认是true,与@JsonIgnore(true)同义,序列化时忽略该属性
    private Integer id;

    @JsonIgnore(value = false)//序列化时不忽略该属性
    private String name;

    private Double grade;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")//日期序列化时转化为该格式
    private Date birthday;

    public User(Integer id, String name, Double grade, Date birthday) {
        this.id = id;
        this.name = name;
        this.grade = grade;
        this.birthday = birthday;
    }

   //get...set...
}
        User user1 = new User(101, "张三", 89.52135, new Date());
        ObjectMapper mapper = new ObjectMapper();
        //User类转JSON
        String json = mapper.writeValueAsString(user1);

输出:
{"name":"张三","grade":89.52135,"birthday":"2018-08-30 13:18:54"}

@JsonIgnoreProperties 用法:

@JsonIgnoreProperties({"id", "name"}) //序列化时忽略指定的属性,与 @JsonIgnore冲突时,以此处为准
public class User {

    @JsonIgnore//默认是true,与@JsonIgnore(true)同义,序列化时忽略该属性
    private Integer id;

    @JsonIgnore(value = false)//序列化时不忽略该属性
    private String name;

    private Double grade;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")//日期序列化时转化为该格式
    private Date birthday;

    public User(Integer id, String name, Double grade, Date birthday) {
        this.id = id;
        this.name = name;
        this.grade = grade;
        this.birthday = birthday;
    }

    //get...set...
}
        User user1 = new User(101, "张三", 89.52135, new Date());
        ObjectMapper mapper = new ObjectMapper();
        //User类转JSON
        String json = mapper.writeValueAsString(user1);

输出:

{"grade":89.52135,"birthday":"2018-08-30 13:22:48"}

自定义序列化器JsonSerializer:(比如:小数位保留两位)

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * @Desc 自定义序列化方法,也可自定义反序列化,
 **/
public class UserSerializer extends JsonSerializer<User> {
    DecimalFormat decimalFormat = new DecimalFormat("0.00");//保留两位小数

    @Override
    public void serialize(User value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeStartObject();
        gen.writeNumberField("id", value.getId());
        gen.writeStringField("name", value.getName());
        gen.writeNumberField("grade", Double.parseDouble(decimalFormat.format(value.getGrade())));
        gen.writeEndObject();

/*      //报错 Exception in thread "main" com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
        //gen.writeStartObject();
        gen.writeNumberField("grade", Double.parseDouble(decimalFormat.format(value.getGrade())));
        //gen.writeEndObject();
        //gen.writeStartObject();gen.writeEndObject();必须要写
        */
    }
}

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.Date;

/**
 * @Desc
 **/
@JsonSerialize(using = UserSerializer.class)
@JsonIgnoreProperties({"id", "name"}) //序列化时忽略指定的属性,与 @JsonIgnore冲突时,以此处为准
public class User {

    @JsonIgnore//默认是true,与@JsonIgnore(true)同义,序列化时忽略该属性
    private Integer id;

    @JsonIgnore(value = false)//序列化时不忽略该属性
    private String name;

    private Double grade;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")//日期序列化时转化为该格式
    private Date birthday;

    public User(Integer id, String name, Double grade, Date birthday) {
        this.id = id;
        this.name = name;
        this.grade = grade;
        this.birthday = birthday;
    }

    //get...set...
}
        User user1 = new User(101, "张三", 89.52135, new Date());
        ObjectMapper mapper = new ObjectMapper();
        //User类转JSON
        String json = mapper.writeValueAsString(user1);

输出:
{"id":101,"name":"张三","grade":89.52}

参考:

http://jackyrong.iteye.com/blog/2005323  jackson中自定义处理序列化和反序列化
https://blog.csdn.net/u012373815/article/details/52266609 json过滤某些属性之@jsonignore

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/82228213