关于 fastjson2 支持 jackson注解这件事

前言

最近新项目采用新架构,JSON序列化选用了fastjson2。在使用时,有一个类的属性用了Jackson的注解 @JsonProperty 命名别名,结果竟然好使。

为了验证单独写代码测试了一下,确认是支持的。而后扒了下源码,发现 fastjson2 是默认支持了 jackson 以及 fastjson 的部分注解

探索过程

测试代码

import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {

    @JsonProperty("alias")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class DemoTest {

    public static void main(String[] args) {
        Person person = new Person();
        person.setName("zhangsan");

        System.out.println("fastjson result:" + com.alibaba.fastjson.JSON.toJSONString(person));
        System.out.println("fastjson2 result:" + com.alibaba.fastjson2.JSON.toJSONString(person));
    }
}

运行结果

fastjson2 result:{"alias":"zhangsan"}
fastjson2 result:{"name":"zhangsan"}

结果显示 fastjson1 不支持 @JsonProperty 这个注解,而 fastjson2 是支持这个注解的。

扒了一下源码,发现了这么一串代码:

boolean useJacksonAnnotation = JSONFactory.isUseJacksonAnnotation();
switch (annotationTypeName) {
    case "com.fasterxml.jackson.annotation.JsonIgnore":
        if (useJacksonAnnotation) {
            processJacksonJsonJsonIgnore(fieldInfo, annotation);
        }
        break;
    case "com.fasterxml.jackson.annotation.JsonAnyGetter":
        if (useJacksonAnnotation) {
            fieldInfo.features |= FieldInfo.UNWRAPPED_MASK;
        }
        break;
    case "com.fasterxml.jackson.annotation.JsonValue":
        if (useJacksonAnnotation) {
            fieldInfo.features |= FieldInfo.VALUE_MASK;
        }
        break;
    case "com.fasterxml.jackson.annotation.JsonRawValue":
        if (useJacksonAnnotation) {
            fieldInfo.features |= FieldInfo.RAW_VALUE_MASK;
        }
        break;
    case "com.alibaba.fastjson.annotation.JSONField":
        processJSONField1x(fieldInfo, annotation);
        break;
    case "com.fasterxml.jackson.annotation.JsonProperty":
        if (useJacksonAnnotation) {
            processJacksonJsonProperty(fieldInfo, annotation);
        }
        break;
    default:
        break;
}

fasjson2 对旧的 fastjson 和 Jackson 的注解都有支持,这个也许对新人是友好的。免得用错 Json 库的注解发现序列化不好用。

接着找了一下 useJacksonAnnotation 这个变量是怎么定义的。

它是在 JSONFactory 中定义了这个变量,并且默认情况下是开启的

{
    String property = System.getProperty("fastjson2.useJacksonAnnotation");
    if (property != null) {
        property = property.trim();
    }

    if (property == null || property.isEmpty()) {
        property = properties.getProperty("fastjson2.useJacksonAnnotation");
        if (property != null) {
            property = property.trim();
        }
    }

    useJacksonAnnotation = property == null || !property.equals("false");
}

想要关闭的话要在 JVM 启动参数里添加 -Dfastjson2.useJacksonAnnotation=false 或者调用 JSONFactory.setUseJacksonAnnotation(false);就可以关闭了。

结论

  • fastjson2 在序列化以及反序列化的时候对,fastjson1 和 jackson 的部分都是有支持的。
  • 不过对jackson注解的支持是可以关闭的,关闭方法:
    • 在 JVM 启动参数里添加 -Dfastjson2.useJacksonAnnotation=false
    • 或者调用 JSONFactory.setUseJacksonAnnotation(false);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pzzhao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值