枚举上忽略了 JSON 绑定 @JsonbTypeDeserializer 注释? [英] JSON Binding @JsonbTypeDeserializer annotation ignored on enums?
问题描述
我正在将 JAXB 应用程序转换为 JSON-B 并且我在我的一个测试中尝试使用自定义 JsonbDeserializer
反序列化 Java 枚举时遇到问题.
I'm converting a JAXB application to JSON-B and I've run into an issue while trying to deserialize a Java enum using a custom JsonbDeserializer
inside one of my tests.
我需要反序列化的原始 JSON 包含引用枚举常量的 int
s.因此,我的自定义 JsonbDeserializer
需要采用 int
并返回具有匹配 ordinal
的枚举常量.它看起来像这样:
The original JSON I need to deserialize contains int
s referencing the enum's constants. Therefore my custom JsonbDeserializer
needs to take the int
and return the enum constant with the matching ordinal
. It looks like this:
@JsonbTypeDeserializer(Region.RegionDeserializer.class)
public enum Region implements BaseEnum {
REGION_A,
REGION_B;
static final class RegionDeserializer implements JsonbDeserializer<Region> {
// deserialize() method returns REGION_A for 0 and REGION_B for 1.
}
}
然后我像这样运行它:
try (var jsonb = JsonbBuilder.create()) {
var result = jsonb.fromJson(text, Region.class);
} catch (final Exception ex) {
fail(ex);
}
不幸的是,这是我得到的:
Unfortunately, here's what I get back:
java.lang.IllegalArgumentException: No enum constant Region.1
at java.base/java.lang.Enum.valueOf(Enum.java:266)
at org.eclipse.yasson.internal.serializer.EnumTypeDeserializer.deserialize(EnumTypeDeserializer.java:40)
如您所见,未使用 RegionDeserializer
.相反,使用默认的枚举解串器.查看 JSON-B 文档,我发现我应该手动注册反序列化器:
As you can see, RegionDeserializer
is not used. Instead, the default enum deserializer is used. Looking into the JSON-B docs, I see I should register the deserializer manually:
JsonbConfig config = new JsonbConfig()
.withDeserializer(RegionDeserializer.class);
Jsonb jsonb = JsonbBuilder.create(config);
...
当我这样做时,代码实际上可以工作.但这是我的问题 - 我该怎么做才能让 JsonbTypeDeserializer
注释自动注册?考虑到我有很多需要自定义反序列化器的枚举,手动注册它们确实无法扩展.
And when I do that, the code in fact works. But here's my question - what can I do to have the JsonbTypeDeserializer
annotation registered automatically? Considering I have a lot of enums I need custom deserializers for, registering them manually really doesn't scale.
EDIT 1: 我尝试改用 @JsonbCreator
注释的静态方法,结果是一样的.仍然使用默认的枚举解串器.
EDIT 1: I have tried to use @JsonbCreator
-annotated static method instead, and the result was the same. The default enum deserializer was still used.
推荐答案
JSON-B 规范提到了两种注册自定义解串器的方式:
The JSON-B specification mentions both ways of registering the custom deserializer:
注册JsonbSerializer/JsonbDeserializer有两种方式:
There are two ways how to register JsonbSerializer/JsonbDeserializer:
- 使用
JsonbConfig::withSerializers
/JsonbConfig::withDeserializers
方法; - 使用
JsonbSerializer
/JsonbDeserializer
注释对类型进行注释.
- Using
JsonbConfig::withSerializers
/JsonbConfig::withDeserializers
method; - Annotating a type with
JsonbSerializer
/JsonbDeserializer
annotation.
注释不起作用的事实是一个错误.我可以在 Yasson 1.0.6 上重现这个,但不能在 Yasson 2.0.0-M1 上重现.也许更新到最新版本可以解决您的问题?
The fact that the annotation does not work is a bug. I could reproduce this on Yasson 1.0.6, but not on Yasson 2.0.0-M1. Perhaps updating to the latest version solves your problem?
这篇关于枚举上忽略了 JSON 绑定 @JsonbTypeDeserializer 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!