Skip to content

[BUG] 枚举类型变量反序列化后变成NULL #2820

@yaozhihang

Description

@yaozhihang

问题描述

枚举类型变量反序列化后变成NULL

环境信息

请填写以下信息:

  • OS信息: Windows 10
  • JDK信息: Openjdk 17
  • 版本信息:Fastjson 2:2.0.52

重现步骤

首先建立一个基于gradle的java项目,并且引入fastjson2依赖和另外一个开源库citygml4j

dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2:2.0.52'
    implementation 'org.citygml4j:citygml4j-core:3.2.1'
}

module-info.java 设置模块

module org.example {
    requires com.alibaba.fastjson2;
    requires org.citygml4j.core;

    exports org.example;
}

然后建立一个简单的类, 里面只有一个枚举类型的变量 version.

package org.example;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import org.citygml4j.core.model.CityGMLVersion;

public class FOO {
    @JSONField(serializeFeatures = JSONWriter.Feature.WriteEnumUsingToString)
    private CityGMLVersion version;

    public CityGMLVersion getVersion() {
        return version;
    }

    public FOO setVersion(CityGMLVersion version) {
        this.version = version;
        return this;
    }

    public FOO setVersion(String version) {
        this.version = CityGMLVersion.fromValue(version);
        return this;
    }
}

接下来进行测试

package org.example;

import com.alibaba.fastjson2.JSON;

public class Main {
    public static void main(String[] args) {
        FOO foo = JSON.parseObject("{\n" +
                "  \"version\": \"2.0\"\n" +
                "}", FOO.class);

        System.out.println(foo.getVersion());
    }
}

结果输出是 null。但是如果使用以前的版本 2:2.0.49, 结果则是正确的 "2.0". 附上以上测试代码项目包。

test.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions