如何编组 class 的嵌套内容但不添加属性名称作为其键
How to marshall the nested content of a class but without adding the attribute name as its key
我有这个 class:
public class EnvironmentInformation implements Serializable {
private final String[] profiles;
//GETTERS/SETTERS
已将此 class 的一个对象添加到具有键 "environment" 的映射中。当我使用 Jackson 的对象映射器整理地图时,我得到:
{"environment":{"profiles":["dev"]}}
然而我想得到的是:
{"environment": ["dev"]}
有什么方法可以自定义编组过程以获得该结果吗?
注意我无法修改 EnvironmentInformation class 结构(不过我可以添加注释),
解决了实现 JsonSerializable 的问题:
public class EnvironmentInformation implements Serializable, JsonSerializable {
private final String[] profiles;
public EnvironmentInformation(String[] environment) {
this.profiles = environment;
}
public String[] getProfiles() {
return profiles;
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartArray();
for (String fooValue : getProfiles()) {
gen.writeString(fooValue);
}
gen.writeEndArray();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
}
我有这个 class:
public class EnvironmentInformation implements Serializable {
private final String[] profiles;
//GETTERS/SETTERS
已将此 class 的一个对象添加到具有键 "environment" 的映射中。当我使用 Jackson 的对象映射器整理地图时,我得到:
{"environment":{"profiles":["dev"]}}
然而我想得到的是:
{"environment": ["dev"]}
有什么方法可以自定义编组过程以获得该结果吗?
注意我无法修改 EnvironmentInformation class 结构(不过我可以添加注释),
解决了实现 JsonSerializable 的问题:
public class EnvironmentInformation implements Serializable, JsonSerializable {
private final String[] profiles;
public EnvironmentInformation(String[] environment) {
this.profiles = environment;
}
public String[] getProfiles() {
return profiles;
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartArray();
for (String fooValue : getProfiles()) {
gen.writeString(fooValue);
}
gen.writeEndArray();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
}