基于Jackson中另一个字段值的条件字段要求?

THI*_*ELP 7 java json pojo jackson

考虑具有一个字符串和两个数组的JSON表示.例如,

{
    "type" : "A",
    "ListA" : []
    "ListB" : [3, 4, 5]
 }
Run Code Online (Sandbox Code Playgroud)

在上述情况下,type需要现场,但ListAListB有条件地基于所述值所需的反序列化type.换句话说,ListA如果只需要type有值AListB如果只要求type有一个值B.

目前,我在杰克逊和Java工作,我已经能够type通过创建POJO如下来实现制作字段:

public class Example {
    @JsonProperty(required = true)
    String type;

    // getter and setter auto-generated
Run Code Online (Sandbox Code Playgroud)

但我不能只是附加其他@JsonProperty(required = true)ListAListB因为它依赖的价值type.

我如何根据值的有条件要求ListAListB反序列化type

另外,我将要执行额外的检查,例如,是否任一ListAListB为空数组(size == 0)或没有.

cas*_*lin 7

您可以使用自定义反序列化器来实现它.

定义你的模型

你的Example课就像:

public class Example {

    private String type;
    private List<Integer> listA;
    private List<Integer> listB;

    // Getters and setters omitted    
}
Run Code Online (Sandbox Code Playgroud)

创建自定义反序列化程序

您的自定义反序列化器可能如下:

public class ExampleDeserializer extends StdDeserializer<Example> {

    private static final String TYPE_A = "A";
    private static final String TYPE_B = "B";

    public ExampleDeserializer() {
        super(Example.class);
    }

    @Override
    public Example deserialize(JsonParser p, DeserializationContext ctxt) 
                   throws IOException, JsonProcessingException {

        ObjectMapper mapper = (ObjectMapper) p.getCodec();  
        JsonNode tree = mapper.readTree(p);  

        Example example = new Example();

        JsonNode typeNode = tree.get("type");
        if (typeNode == null || typeNode.asText().isEmpty()) {
            throw ctxt.mappingException("\"type\" is required");
        }
        example.setType(typeNode.asText());

        switch (typeNode.asText()) {

        case TYPE_A:
            ArrayNode listANode = (ArrayNode) tree.get("ListA");
            if (listANode == null || listANode.size() == 0) {
                throw ctxt.mappingException(
                           "\"ListA\" is required when \"type\" is \"" + TYPE_A + "\"");
            }
            example.setListA(createList(listANode));
            break;

        case TYPE_B:
            ArrayNode listBNode = (ArrayNode) tree.get("ListB");
            if (listBNode == null || listBNode.size() == 0) {
                throw ctxt.mappingException(
                           "\"ListB\" is required when \"type\" is \"" + TYPE_B + "\"");
            }
            example.setListB(createList(listBNode));
            break;

        default:
            throw ctxt.mappingException(
                       "\"type\" must be \"" + TYPE_A + "\" or \"" + TYPE_B + "\"");
        }


        return example;
    }

    private List<Integer> createList(ArrayNode arrayNode) {
        List<Integer> list = new ArrayList<Integer>();
        for (JsonNode node : arrayNode) {
            list.add(node.asInt());
        }
        return list;
    }
}
Run Code Online (Sandbox Code Playgroud)

注册自定义反序列化程序

将上面定义的自定义反序列化程序注册到您的ObjectMapper:

SimpleModule module = new SimpleModule("ExampleDeserializer", 
        new Version(1, 0, 0, null, "com.example", "example-deserializer")); 

ExampleDeserializer exampleDeserializer = new ExampleDeserializer();
module.addDeserializer(Example.class, exampleDeserializer);

ObjectMapper mapper = new ObjectMapper()
                          .registerModule(module)
                          .enable(SerializationFeature.INDENT_OUTPUT);
Run Code Online (Sandbox Code Playgroud)

测试自定义反序列化器

使用自定义序列化程序:

String json = "{\"type\":\"A\",\"ListA\":[1,2,3]}";
Example example = mapper.readValue(json, Example.class);
Run Code Online (Sandbox Code Playgroud)