将 pojo 序列化为嵌套 JSON 字典
Serializing a pojo as a nested JSON dictionary
鉴于简单 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 >> );
}
有什么建议吗?
您需要使用writeObjectFieldStart
方法写入字段并在同一类型上打开新的JSON Object
:
class SimplePojoJsonSerializer extends JsonSerializer<SimplePojo> {
@Override
public void serialize(SimplePojo value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeObjectFieldStart(value.getKey());
gen.writeStringField("value", value.getValue());
gen.writeNumberField("thing1", value.getThing1());
gen.writeBooleanField("thing2", value.isThing2());
gen.writeEndObject();
gen.writeEndObject();
}
}
您不需要自定义序列化程序。您可以利用 @JsonAnyGetter
注释生成包含所需输出属性的地图。
下面的代码采用上面的示例 pojo 并生成所需的 json 表示。
首先,您已使用 @JsonIgnore
注释所有 getter 方法,以便 jackson 在序列化期间忽略它们。将被调用的唯一方法是 @JsonAnyGetter
注释的方法。
public class SimplePojo {
private String key ;
private String value ;
private int thing1 ;
private boolean thing2;
// tell jackson to ignore all getter methods (and public attributes as well)
@JsonIgnore
public String getKey() {
return key;
}
// produce a map that contains the desired properties in desired hierarchy
@JsonAnyGetter
public Map<String, ?> getForJson() {
Map<String, Object> map = new HashMap<>();
Map<String, Object> attrMap = new HashMap<>();
attrMap.put("value", value);
attrMap.put("thing1", thing1); // will autobox into Integer
attrMap.put("thing2", thing2); // will autobox into Boolean
map.put(key, attrMap);
return map;
}
}
鉴于简单 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 >> );
}
有什么建议吗?
您需要使用writeObjectFieldStart
方法写入字段并在同一类型上打开新的JSON Object
:
class SimplePojoJsonSerializer extends JsonSerializer<SimplePojo> {
@Override
public void serialize(SimplePojo value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeObjectFieldStart(value.getKey());
gen.writeStringField("value", value.getValue());
gen.writeNumberField("thing1", value.getThing1());
gen.writeBooleanField("thing2", value.isThing2());
gen.writeEndObject();
gen.writeEndObject();
}
}
您不需要自定义序列化程序。您可以利用 @JsonAnyGetter
注释生成包含所需输出属性的地图。
下面的代码采用上面的示例 pojo 并生成所需的 json 表示。
首先,您已使用 @JsonIgnore
注释所有 getter 方法,以便 jackson 在序列化期间忽略它们。将被调用的唯一方法是 @JsonAnyGetter
注释的方法。
public class SimplePojo {
private String key ;
private String value ;
private int thing1 ;
private boolean thing2;
// tell jackson to ignore all getter methods (and public attributes as well)
@JsonIgnore
public String getKey() {
return key;
}
// produce a map that contains the desired properties in desired hierarchy
@JsonAnyGetter
public Map<String, ?> getForJson() {
Map<String, Object> map = new HashMap<>();
Map<String, Object> attrMap = new HashMap<>();
attrMap.put("value", value);
attrMap.put("thing1", thing1); // will autobox into Integer
attrMap.put("thing2", thing2); // will autobox into Boolean
map.put(key, attrMap);
return map;
}
}