【发布时间】:2021-06-09 19:19:43
【问题描述】:
我有一个 JAVA POJO,它有很多字段。其中一个字段是Map<String, Object>,我使用Custom JsonSerializer,因为它可以有多种类型的Objects。我只想知道如何避免fieldname 的序列化只针对这个Map<String,Object> 字段。对于 POJO 中的所有其他字段,我希望拥有字段名称,但仅出于此目的,我想将其删除。
到目前为止,当使用 Jackson searlizer 时,我得到以下输出:
{
"isA" : "Human",
"name" : "Batman",
"age" : "2008",
"others" : {
"key1" : "value1",
"key2" : {
"key3" : "value3"
},
"key5" : {
"key4" : "One",
"key4" : "Two"
}
}
}
我想要得到以下输出:(我想要做的就是删除 Map<String,Object> 字段名称,但保留其子项。)
{
"isA" : "Human",
"name" : "Batman",
"age" : "2008",
"key1" : "value1",
"key2" : {
"key3" : "value3"
},
"key5" : {
"key4" : "One",
"key4" : "Two"
}
}
下面是我的Human.class POJO,被ObjectMapper使用:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
@ToString
class Human {
private String isA;
private String name;
private String age;
@JsonSerialize(using = MyCustomSearlize.class)
private Map<String, Object> others = new HashMap<>();
}
以下是我的Custom searlizer,MAP 在搜索过程中使用它:
class MyCustomSearlize extends JsonSerializer<Map<String, Object>> {
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
recusiveSerializer(value, gen, serializers);
gen.writeEndObject();
}
public void recusiveSerializer(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
for (Map.Entry<String, Object> extension : value.entrySet()) {
if (extension.getValue() instanceof Map) {
//If instance is MAP then call the recursive method
gen.writeFieldName(extension.getKey());
gen.writeStartObject();
recusiveSerializer((Map) extension.getValue(), gen, serializers);
gen.writeEndObject();
} else if (extension.getValue() instanceof String) {
//If instance is String directly add it to the JSON
gen.writeStringField(extension.getKey(), (String) extension.getValue());
} else if (extension.getValue() instanceof ArrayList) {
//If instance if ArrayList then loop over it and add it to the JSON after calling recursive method
for (Object dupItems : (ArrayList<Object>) extension.getValue()) {
if (dupItems instanceof Map) {
gen.writeFieldName(extension.getKey());
gen.writeStartObject();
recusiveSerializer((Map) dupItems, gen, serializers);
gen.writeEndObject();
} else {
gen.writeStringField(extension.getKey(), (String) dupItems);
}
}
}
}
}
}
下面是我的Main类:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
Human person = new Human();
person.setName("Batman");
person.setAge("2008");
Map<String, Object> others = new HashMap<>();
others.put("key1", "value1");
Map<String, Object> complex = new HashMap<>();
complex.put("key3", "value3");
others.put("key2", complex);
Map<String, Object> complex2 = new HashMap<>();
List<String> dup = new ArrayList<>();
dup.add("One");
dup.add("Two");
complex2.put("key4", dup);
others.put("key5", complex2);
person.setOthers(others);
final ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
objectMapper.registerModule(simpleModule);
final String jsonEvent = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);
System.out.println(jsonEvent);
}
}
以下我尝试过的事情:
- 我尝试在
Map上添加@JsonValue,但这会删除我的所有其他值(姓名、年龄、isA 等) - 我尝试了
@JsonAnyGetter,它适用于Map和String,但不适用于我想要的ArrayList。作为我要求的一部分,我在我的应用程序中以不同的方式处理ArrayList位。
有没有办法让它与@JsonSerialize 和@JsonAnyGetter 一起使用,因为我无法同时使用它们。
有人可以帮忙解决这个问题吗?请指导我找到适当的文档或解决方法,非常感谢。
【问题讨论】:
标签: java json jackson jackson-databind jackson2