• Jackson使用手册


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

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

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

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

    1、jackson基本使用

    1.1、创建Person对象

    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.2、Main方法调用

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

    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));
        }
    }

    输出结果为:

    {"name":"jackson"}

    2、注解使用

    2.1、@JsonProperty注解使用

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

    @JsonProperty("userName")
        private String name;

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

    {"userName":"jackson"}

    2.2、@JsonIgnore注解使用

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

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

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

    {"userAge":20}

    2.3、@JsonIgnoreProperties注解使用

    2.3.1、序列化@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.2、@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.4、@JsonTypeName @JsonTypeInfo

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

    在序列化是增加一层

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

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

    2.5、@JsonRootName注解

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

    2.6、@JsonFormat注解格式化日期格式

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

     2.7、@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.8、@JsonAnySetter注解
    @JsonAnySetter注解主要作用于反序列化上:
    1.用在非静态方法上,注解的方法必须有两个参数,第一个是json字段中的key,第二个是value,方法名随意
    2.反序列化的时候将对应不上的字段全部放到Map里面

    2.9、JavaType

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

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

    2.10、TypeReference

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

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

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

  • 相关阅读:
    JDK各版本新特性总结
    Java MyBatis3(3)分页插件PageHelper
    Kubernetes--K8S
    MySQL 查看表结构
    查看mysql库大小,表大小,索引大小
    Linux服务器安装Oracle服务端总结
    怎样玩转千万级别的数据
    Markdown 新手指南
    深入理解Java:注解(Annotation)基本概念
    深入理解Java:注解(Annotation)自定义注解入门
  • 原文地址:https://www.cnblogs.com/miaosj/p/10936451.html
Copyright © 2020-2023  润新知