Jackson 映射中标准属性和动态属性的混合
Mix of standard and dynamic properties in Jackson mapping
我们正在使用提供 json 的 REST 服务,该服务将包含一些标准属性以及一些动态属性。
例如:
{
id: 123,
name: "some name",
custom_name: "Some value",
some_other_custom_name: "Some other value",
}
理想情况下,我希望 class 设计如下:
public class MyObject{
@JsonProperty int id;
@JsonProperty String name;
private Map<String, String> customVals;
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getCustomVal(String key){
return customVals.get(key);
}
}
有什么方法可以说服 Jackson 将自定义值推送到地图中(或实现等效功能)?
现在,我只是将整个对象反序列化为一个 Map,并将其包装在我的业务对象中,但它远不如反序列化处理它时那样优雅。
您可以使用 Jackson @JsonAnySetter and @JsonAnyGetter annotations。
这是一个完整的例子:
public class JacksonAnyGetter {
static final String JSON = "{"
+ " \"id\": 123,"
+ " \"name\": \"some name\","
+ " \"custom_name\": \"Some value\","
+ " \"some_other_custom_name\": \"Some other value\""
+ "}";
static class Bean {
public int id;
public String name;
private Map<String, Object> properties = new HashMap<>();
@JsonAnySetter
public void add(String key, String value) {
properties.put(key, value);
}
@JsonAnyGetter
public Map<String, Object> getProperties() {
return properties;
}
@Override
public String toString() {
return "Bean{" +
"id=" + id +
", name='" + name + '\'' +
", properties=" + properties +
'}';
}
}
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final Bean bean = mapper.readValue(JSON, Bean.class);
System.out.println(bean);
final String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
System.out.println(json);
}
}
输出:
Bean{id=123, name='some name', properties={custom_name=Some value, some_other_custom_name=Some other value}}
{
"id" : 123,
"name" : "some name",
"custom_name" : "Some value",
"some_other_custom_name" : "Some other value"
}
我们正在使用提供 json 的 REST 服务,该服务将包含一些标准属性以及一些动态属性。
例如:
{
id: 123,
name: "some name",
custom_name: "Some value",
some_other_custom_name: "Some other value",
}
理想情况下,我希望 class 设计如下:
public class MyObject{
@JsonProperty int id;
@JsonProperty String name;
private Map<String, String> customVals;
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getCustomVal(String key){
return customVals.get(key);
}
}
有什么方法可以说服 Jackson 将自定义值推送到地图中(或实现等效功能)?
现在,我只是将整个对象反序列化为一个 Map,并将其包装在我的业务对象中,但它远不如反序列化处理它时那样优雅。
您可以使用 Jackson @JsonAnySetter and @JsonAnyGetter annotations。
这是一个完整的例子:
public class JacksonAnyGetter {
static final String JSON = "{"
+ " \"id\": 123,"
+ " \"name\": \"some name\","
+ " \"custom_name\": \"Some value\","
+ " \"some_other_custom_name\": \"Some other value\""
+ "}";
static class Bean {
public int id;
public String name;
private Map<String, Object> properties = new HashMap<>();
@JsonAnySetter
public void add(String key, String value) {
properties.put(key, value);
}
@JsonAnyGetter
public Map<String, Object> getProperties() {
return properties;
}
@Override
public String toString() {
return "Bean{" +
"id=" + id +
", name='" + name + '\'' +
", properties=" + properties +
'}';
}
}
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final Bean bean = mapper.readValue(JSON, Bean.class);
System.out.println(bean);
final String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
System.out.println(json);
}
}
输出:
Bean{id=123, name='some name', properties={custom_name=Some value, some_other_custom_name=Some other value}}
{
"id" : 123,
"name" : "some name",
"custom_name" : "Some value",
"some_other_custom_name" : "Some other value"
}