-
Notifications
You must be signed in to change notification settings - Fork 527
[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
Comments
这个你可以用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, can you please explain what the proposed MixIn class and its 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 |
@wenshao 感谢回复,正如上面clausnagel提到的, 使用 |
JSON.toJSONString(obj, JSONWriter.Feature.WriteEnumUsingToString) @yaozhihang 也可以只用这个方法,未来的版本应该也是这个行为,缺省是name() |
不好意思,不是很理解。问题出现在反序列化的过程。您提上面到"缺省是name()" 是什么意思? |
Agree with @yaozhihang. I guess, having a |
问题描述
枚举类型变量反序列化后变成NULL
环境信息
请填写以下信息:
重现步骤
首先建立一个基于gradle的java项目,并且引入fastjson2依赖和另外一个开源库citygml4j
在
module-info.java
设置模块然后建立一个简单的类, 里面只有一个枚举类型的变量
version
.接下来进行测试
结果输出是 null。但是如果使用以前的版本 2:2.0.49, 结果则是正确的 "2.0". 附上以上测试代码项目包。
test.zip
The text was updated successfully, but these errors were encountered: