Jackson使用手册

引用jar:jackson-core,jackson-databind,jackson-annotationsjava

http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.9.9/jackson-annotations-2.9.9.jarsql

http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.9.9/jackson-core-2.9.9.jarjson

http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.9.9/jackson-databind-2.9.9.jarapp

一、jackson基本使用maven

1.一、建立Person对象this

public class Person {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }
}

1.二、Main方法调用spa

备注:对象转json须要属性拥有get方法(注解方法除外)code

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] arges) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Person person = new Person("jackson",20);
        System.out.println(mapper.writeValueAsString(person));
    }
}

输出结果为:orm

{"name":"jackson"}

二、注解使用xml

2.一、@JsonProperty注解使用

@JsonProperty注解相似于sql里字段的别名,用于序列化,使用注解字段属性,替代原字段属性

@JsonProperty("userName")
    private String name;

序列化结果为:在序列化的json串中,userName替代了name

{"userName":"jackson"}

2.二、@JsonIgnore注解使用

@JsonIgnore注解是在序列化时忽略该字段

  @JsonIgnore
    @JsonProperty("userName")
    private String name;
    @JsonProperty("userAge")
    private Integer age;

序列化结果为:{"userAge":20}

{"userAge":20}

2.三、@JsonIgnoreProperties注解使用

2.3.一、序列化@JsonIgnoreProperties与@JsonIgnore相似,用于类上,注解使用的是字段别名

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(value={"name","userAge"})
public class Person {
    @JsonIgnore
    @JsonProperty("userName")
    private String name;
    @JsonProperty("userAge")
    private Integer age;
    @JsonProperty("userHeight")
    private Integer height;

    public Person(String name, Integer age, Integer height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }
}
ObjectMapper mapper = new ObjectMapper();
Person person = new Person("jackson",20,175);
System.out.println(mapper.writeValueAsString(person));

运行结果为:{"userHeight":175}

{"userHeight":175}

2.3.二、@JsonIgnoreProperties(ignoreUnknown = true)用于忽略字段不匹配状况,至关于mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

备注:反序列化须要有无参构造器

ObjectMapper mapper = new ObjectMapper();
Person person = new Person("jackson",20,175);
System.out.println(mapper.writeValueAsString(person));
//mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
System.out.println(mapper.readValue("{\"sheight\":172}", Person.class).getHeight());

2.四、@JsonTypeName @JsonTypeInfo

@JsonTypeName(value = "user")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)

在序列化是增长一层

序列化结果为:{"user":{"height":175}}

{"user":{"height":175}}

2.五、@JsonRootName注解

  2.4组合在序列化上等于类上注解@JsonRootName("user") 和 mapper.enable(SerializationFeature.WRAP_ROOT_VALUE),反序列化无用;

2.六、@JsonFormat注解格式化日期格式

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss:SSS",timezone="GMT+8")
    private Date date;

 2.七、@JsonAnyGetter注解
@JsonAnyGetter注解主要用在序列化:
1.方法是非静态,没有参数的,方法名随意
2.方法返回值必须是Map类型
3.在一个实体类中仅仅用在一个方法上
4.序列化的时候json字段的key就是返回Map的key,value就是Map的value

    //@JsonAnyGetter
    public Map<String,Object> getMap(){
        return map;
    }

序列化结果为:{"height":175,"map":{"1":"1","key":"value"},"date":"2019-05-29 10:37:55:759"}

取消注释后序列化结果为:{"height":175,"date":"2019-05-29 10:39:14:685","1":"1","key":"value"}

 2.八、@JsonAnySetter注解
@JsonAnySetter注解主要做用于反序列化上:
1.用在非静态方法上,注解的方法必须有两个参数,第一个是json字段中的key,第二个是value,方法名随意
2.反序列化的时候将对应不上的字段所有放到Map里面

2.九、JavaType

list反序列化为LinkedHashMap若是要转换为原来对象类型

若是为Map类型 mapper.getTypeFactory().constructParametricType(Map.class,String.class,Student.class);
若是为List类型 personList  =  mapper.readValue(mapper.writeValueAsString(personList),mapper.getTypeFactory().constructParametricType(List.class,Person.class));// 第二个参数是Map的key,第三个参数是Map的value

2.十、TypeReference

TypeReference比javaType模式更加方便,代码也更加简洁

mapper.readValue(json, new TypeReference<List<Person>>(){}); 

三、序列化(SerializationFeature)与反序列化(DeserializationFeature)自定义规则

相关文章
相关标签/搜索