Jackson全面解析--注解全讲解一(@JacksonAnno

2020-11-24  本文已影响0人  牧羊人刘俏

Jackson作为Spring的默认的序列化框架,不管是在性能上还是扩展性,使用的方便程度上面都得到了广泛的认可,本系列会由浅入深的介绍整个Jackson的方方面面,从使用方法,到源码解析,到自定义的扩展,以及与spring框架的的集成一一道来。
本系列文章已Jackson的2.9.8版本为基础讲解,首先我们对改版本里面的所有的注解一一实战下,先学会怎么用,后面再讲源码。
在Jackson-annotations包里面,我们可以看到Jackson提供的所有注解,如下


image.png

我们使用从上往下的方式介绍这些注解的作用

1 @JacksonAnnotation

/**
 * Meta-annotation (annotations used on other annotations)
 * used for marking all annotations that are
 * part of Jackson package. Can be used for recognizing all
 * Jackson annotations generically, and in future also for
 * passing other generic annotation configuration.
 */
@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface JacksonAnnotation
{
    // for now, a pure tag annotation, no parameters
}

从注释信息可以看出,此注解是其他所有jackson注解的元注解,打上了此注解的注解表明是jackson注解的一部分。

2 @JacksonAnnotationsInside

/**
 * Meta-annotation (annotations used on other annotations)
 * used for indicating that instead of using target annotation
 * (annotation annotated with this annotation),
 * Jackson should use meta-annotations it has.
 * This can be useful in creating "combo-annotations" by having
 * a container annotation, which needs to be annotated with this
 * annotation as well as all annotations it 'contains'.
 * 
 * @since 2.0
 */
@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JacksonAnnotationsInside
{

}

可以看到,JacksonAnnotation注解也打到了这个元注解上面,此注解也是一个元注解,一般用于将其他的注解一起打包成"组合"注解,虽然说jackson提供了很多的非常实用的注解给我们来用,但是产品的需求是无限的,很多时候,我们需要定义自己的注解,来满足我们的需求。
如下,我们定义了一个@CombineJacksonAnnotation注解,可以打在class上面,实现的功能是,1 只输出非null的属性,2按照 name age sex 的顺序序列化属性,然后将此注解打在Person类上面,代码如下

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"name","age","sex"})
@JacksonAnnotationsInside
public @interface CombineJacksonAnnotation {

    @Data
    @AllArgsConstructor(staticName = "of")
    @CombineJacksonAnnotation
    class Person{

        int age;
        String name;
        String sex;

    }
}

ut如下

public static ObjectMapper om = new ObjectMapper();

    static {

        om.enable(SerializationFeature.INDENT_OUTPUT);


    }


    @Test
    public void JacksonAnnotationsInsideTest() throws Exception{

        System.out.println(om.writeValueAsString(CombineJacksonAnnotation.Person.of(12,"tom",null)));


    }
输出如下
{
  "name" : "tom",
  "age" : 12
}

当然JacksonAnnotationsInside注解的还有其他的能力,随着我们使用的深入,会介绍更加高级的用法。

3@JacksonInject

这个注解用于反序列化的时候,如果打在属性上面,表明此字段的反序列化时候,不使用json串里面的属性,而是使用自定义的注入逻辑,使用方式如下

 class Person{

        int age;
        String name;
        @JacksonInject(value = "sex")
        String sex;

    }

  static {

        om.enable(SerializationFeature.INDENT_OUTPUT);
        InjectableValues.Std iv = new InjectableValues.Std();
        iv.addValue("sex","female");
        om.setInjectableValues(iv);


    }


    @Test
    public void JacksonInjectTest() throws Exception{

        CombineJacksonAnnotation.Person person  = om.readValue("{\n" +
                "  \"name\" : \"tom\",\n" +
                "  \"age\" : 12\n" +
                "}",CombineJacksonAnnotation.Person.class);


        System.out.println(om.writeValueAsString(person));

        
    }

输出如下
{
  "name" : "tom",
  "age" : 12,
  "sex" : "female"
}

此注解很特殊,个人感觉使用的频率不高,可以在很特殊的情况下使用。

4 @JsonAlias

此注解的作用很大,用于反序列的时候,指定json别名,特别是在对接不同的api的时候,有的接入方的字段是sex,有的是xingbie,有的是gender但是又表示的同一个意思的时候,非常有用。
使用方法如下

class Person{

        int age;
        String name;
        @JsonAlias({"xingbie","gender","sex"})
        String sex;

    }
测试代码如下

 @Test
    public void JsonAliasTest() throws Exception{

        CombineJacksonAnnotation.Person person1  = om.readValue("{\n" +
                "  \"name\" : \"tom\",\n" +
                "  \"age\" : 12,\n" +
                "  \"sex\" : \"female\"\n" +
                "}",CombineJacksonAnnotation.Person.class);


        CombineJacksonAnnotation.Person person2  = om.readValue("{\n" +
                "  \"name\" : \"tom\",\n" +
                "  \"age\" : 12,\n" +
                "  \"xingbie\" : \"female\"\n" +
                "}",CombineJacksonAnnotation.Person.class);

        CombineJacksonAnnotation.Person person3  = om.readValue("{\n" +
                "  \"name\" : \"tom\",\n" +
                "  \"age\" : 12,\n" +
                "  \"gender\" : \"female\"\n" +
                "}",CombineJacksonAnnotation.Person.class);

        System.out.println(om.writeValueAsString(person1));
        System.out.println(om.writeValueAsString(person2));
        System.out.println(om.writeValueAsString(person3));
    }

ut结果如下
{
  "name" : "tom",
  "age" : 12,
  "sex" : "female"
}
{
  "name" : "tom",
  "age" : 12,
  "sex" : "female"
}
{
  "name" : "tom",
  "age" : 12,
  "sex" : "female"
}

Process finished with exit code 0
上一篇 下一篇

猜你喜欢

热点阅读