gpt4 book ai didi

java - 如何使用 Jackson 反序列化枚举?

转载 作者:行者123 更新时间:2023-12-05 02:52:49 26 4
gpt4 key购买 nike

用户发帖 {"lang": ["en_US", "en_UK"]}到 REST API。我想填充一个具有此属性的对象 List<EnmLanguage> lang; .除了 JSON 数组外,其他所有东西都有效。

我的 EnmLanguage.java 是:

@JsonSerialize(
using = BaseEnumSerializer.class
)
@JsonDeserialize(
using = BaseEnumDeserializer.class
)
public enum EnmLanguage implements BaseEnum, Serializable {
en_US {
public String getCode() {
return "en_US";
}

public String getText(EnmLanguage s) {
return s == EnmLanguage.en_US ? "English" : "İngilizce";
}
},

我的 BaseEnum.java 是:

public interface BaseEnum {
Object getCode();

String getText(EnmLanguage lang);

default String getText() {
return this.getText(EnmLanguage.tr_TR);
}

default Map<String, Object> getMap(EnmLanguage lang) {
return new HashMap<String, Object>() {
{
this.put("name", BaseEnum.this.getText(lang));
this.put("id", BaseEnum.this.getCode());
}
};
}

default Map<String, Object> getMap() {
return this.getMap(EnmLanguage.tr_TR);
}

static <T extends Enum<T> & BaseEnum> T fromCode(Class<T> parent, Object code) {
Map<Object, T> lookup = new HashMap();
Enum[] var3 = (Enum[])parent.getEnumConstants();
int var4 = var3.length;

for(int var5 = 0; var5 < var4; ++var5) {
T d = var3[var5];
lookup.put(((BaseEnum)d).getCode().toString(), d);
}

return (Enum)lookup.get(code.toString());
}
}

我的 BaseEnumDeserialiazer 是:

public class BaseEnumDeserializer<T extends Enum<T> & BaseEnum> extends StdDeserializer<T> {
public BaseEnumDeserializer() {
this((Class)null);
}

public BaseEnumDeserializer(Class<T> vc) {
super(vc);
}

public T deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException {
Field field = this.findField(jsonparser.getCurrentName(), jsonparser.getCurrentValue().getClass());
Class<T> javaType = field.getType();
return BaseEnum.fromCode(javaType, jsonparser.getText());
}

public Field findField(String name, Class<?> c) {
while(c != null) {
Field[] var3 = c.getDeclaredFields();
int var4 = var3.length;

for(int var5 = 0; var5 < var4; ++var5) {
Field field = var3[var5];
if (!Modifier.isStatic(field.getModifiers()) && field.getName().equals(name)) {
return field;
}
}

c = c.getSuperclass();
}

return null;
}
}

但最后它没有转换 "lang": ["en_US", "en_UK"]进入ListEnmLanguage .我认为它不知道到哪个 BaseEnum兑换。我应该如何让它知道它应该将其转换为 List<EnmLanguage>

最佳答案

对于您的特定情况,在基本类型中添加静态 @JsonCreator 方法可能就足够了。

interface Lang {
@JsonCreator
static Lang valueOf(String value) {
return Languages.valueOf(value);
}
}

@JsonFormat(shape = JsonFormat.Shape.STRING)
enum Languages implements Lang {
en_US,
en_GB
}

然后你可以像解析其他类一样解析它

class LangTest {
private final ObjectMapper mapper = new ObjectMapper()
.setAnnotationIntrospector(new JacksonAnnotationIntrospector());

@Test
void readsJson() throws IOException {
final Lang[] tree = this.mapper
.readerFor(Lang[].class)
.readValue("[\"en_US\", \"en_GB\"]");
Assertions.assertArrayEquals(
tree,
new Lang[]{
Languages.en_US,
Languages.en_GB
}
);
}
}

关于java - 如何使用 Jackson 反序列化枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62491071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com