Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yaozhihang opened this issue Jul 19, 2024 · 7 comments
Closed

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

yaozhihang opened this issue Jul 19, 2024 · 7 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@yaozhihang
Copy link

问题描述

枚举类型变量反序列化后变成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

@yaozhihang yaozhihang added the bug Something isn't working label Jul 19, 2024
@wenshao wenshao added this to the 2.0.53 milestone Aug 10, 2024
@wenshao
Copy link
Member

wenshao commented Aug 10, 2024

这个你可以用Mixin的方式去解,比如:

public class Issue2820 {
    static class CityGMLVersionMixin {
        @JSONField(value = true)
        public String toValue() {
            return null;
        }
    }

    @Test
    public void test() {
        JSON.mixIn(CityGMLVersion.class, CityGMLVersionMixin.class);
        FOO foo = JSON.parseObject("{\n" +
                "  \"version\": \"2.0\"\n" +
                "}", FOO.class);

        System.out.println(foo.getVersion());
    }
    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;
        }
    }
}

wenshao added a commit that referenced this issue Aug 10, 2024
@clausnagel
Copy link

clausnagel commented Aug 13, 2024

@wenshao, can you please explain what the proposed MixIn class and its toValue method actually do in the background to solve the issue?

I suppose your proposal is rather a workaround than a solution. At least with version fastjson2 2.0.49, the code worked without having to use a MixIn class. I haven't been using the static JSON.mixIn method before. It looks like this method is some sort of global configuration for the JSON de-/serialization, right? My concern is that this configuration can simply be overriden by any external user. We are building a Java library that uses fastjson2 and I want this library to behave in a stable way without offering users the possibilty to change behaviour via static methods. And this could be accomplished using 2.0.49.

@yaozhihang
Copy link
Author

@wenshao 感谢回复,正如上面clausnagel提到的, 使用 JSON.mixIn 看起来只是一个临时替代方案。请问在接下来的版本中会修复这个bug吗?

@wenshao wenshao modified the milestones: 2.0.53, 2.0.54 Sep 16, 2024
@wenshao
Copy link
Member

wenshao commented Sep 16, 2024

JSON.toJSONString(obj, JSONWriter.Feature.WriteEnumUsingToString)

@yaozhihang 也可以只用这个方法,未来的版本应该也是这个行为,缺省是name()

@yaozhihang
Copy link
Author

不好意思,不是很理解。问题出现在反序列化的过程。您提上面到"缺省是name()" 是什么意思?

@clausnagel
Copy link

Agree with @yaozhihang. I guess, having a ReadEnumUsingToString feature for JSONReader would be a good solution. Strangely, it already worked with 2.0.49.

@wenshao wenshao added the fixed label Jan 1, 2025
@wenshao
Copy link
Member

wenshao commented Jan 12, 2025

https://github.com/alibaba/fastjson2/releases/tag/2.0.54
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants