gpt4 book ai didi

java - 具有变量 JsonProperty 的 Jackson 泛型(与泛型一起使用)

转载 作者:行者123 更新时间:2023-11-30 11:21:16 24 4
gpt4 key购买 nike

我有这样的类(class):

public class Data<U> {

@JsonProperty("difficulties")
private U[] data;

// ... geter setter constructor
}

而且我不想仅仅因为我只需要更改一行代码(在本例中为@JsonProperty("difficulties"))就创建了 10 个类似的类。属性值取决于类型。是否可以在一个类中编写?

最佳答案

基于 Jackson - Modify an attribute at runtime without annotation 的回复Michał Ziober 在这里我能够更改默认字段名称值通过覆盖 PropertyNamingStrategy:

这些是我收到的 JSON 示例(已简化):

{"status": "OK","error": null,"data": {
"difficulties": [{"value":"easy"},{"value":"medium"}]
}}

{"status": "ok", "error": null, "data": {
"countries": [{"code": "AT"},{"code": "BE"}]
}}

查看第二行的不同之处,其中数据对象包含困难或国家(或许多其他基于上下文的名称)。

基于JSON响应的响应类:

public class Response<T>{
private String status;
private String error;
private Data<T> data;
// Getters Setters Constructors
}

基于JSON响应的数据类:

public class Data<T> {
// property name, that will be changed
@JsonProperty(DataNamingStrategy.DATA_FIELD)
private T[] data;
// Getters Setters Constructors
}

这是命名策略,将默认值更改为运行时指定的值

public class DataNamingStrategy extends PropertyNamingStrategy{

// used by other classes (this will be default field name that should be changed)
public static final String DATA_FIELD = "variable:data";
private String fieldName;

public DataNamingStrategy(String fieldName) {
this.fieldName = fieldName;
}

// use this to change field name (format "variable":"value") not needed in my case
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field,
String defaultName) {
return (defaultName.equals(DATA_FIELD))?
fieldName :
super.nameForField(config, field, defaultName);
}

// use this to change setter method field name (JSON -> Object with format "variable":{})
@Override
public String nameForSetterMethod(MapperConfig<?> config,
AnnotatedMethod method, String defaultName) {
return (defaultName.equals(DATA_FIELD))?
fieldName :
super.nameForGetterMethod(config, method, defaultName);
}

// use this to change getter method field name (Object -> JSON with format "variable":{})
// should be same as nameForSetterMethod
@Override
public String nameForGetterMethod(MapperConfig<?> config,
AnnotatedMethod method, String defaultName) {
return nameForSetterMethod(config, method, defaultName);
}
}

用法应该是这样的:

ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new DataNamingStrategy(tableName));
JavaType type = mapper.getTypeFactory().constructParametricType(Response.class, dataClass);
Response<U> u = mapper.readValue(result, type);

其中 result 是作为字符串的 Json,tableName 是将在 JSON 中使用的字符串而不是默认值,dataClassU(例如 Difficulty.class)。

PropertyNamingStrategy 的更好用法应该是 Map 而不是一个 String。但我只需要更改一个特定值。

另请查看 PropertyNamingStrategy documentation或再次访问 Michał Ziober's answer

关于java - 具有变量 JsonProperty 的 Jackson 泛型(与泛型一起使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22321155/

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