【发布时间】:2019-07-22 21:25:08
【问题描述】:
鉴于简单的POJO:
public class SimplePojo {
private String key ;
private String value ;
private int thing1 ;
private boolean thing2;
public String getKey() {
return key;
}
...
}
我在序列化成这样的东西时没有问题(使用Jackson):
{
"key": "theKey",
"value": "theValue",
"thing1": 123,
"thing2": true
}
但真正让我高兴的是,如果我可以这样序列化该对象:
{
"theKey" {
"value": "theValue",
"thing1": 123,
"thing2": true
}
}
我想我需要一个自定义序列化程序,但我面临的挑战是插入一个新字典,例如:
@Override
public void serialize(SimplePojo value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeNumberField(value.getKey(), << Here be a new object with the remaining three properties >> );
}
有什么建议吗?
【问题讨论】:
标签: java json serialization jackson json-serialization