从另一个JsonDeserializer调用自定义JsonDeserializer

时间:2015-07-13 22:12:34

标签: java spring jackson

我试图从另一个调用自定义JsonDeserializer,但我无法弄清楚如何。

我在我的实体中使用@JsonDeserialize注释:

@JsonDeserialize(using = BobDeserializer.class)
class Bob {

    private Integer number;

    ...

}


@JsonDeserialize(using = AliceDeserializer.class)
class Alice {

    private Bob bob;

    ...

}

我的反序列化是:

class BobDeserializer extends StdDeserializer<Bob> {

    public Bob deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode node = parser.getCodec().readTree(parser);

        Integer number = node.get("number").asInt();

        Bob bob = new Bob();
        bob.setNumber(number);

        return bob;
    }

}


class AliceDeserializer extends StdDeserializer<Alice> {

    public Alice deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode node = parser.getCodec().readTree(parser);

        Bob bob = ???????; // How to get Bob calling BobDeserializer to deserialize node.get("bob")?

        Alice alice = new Alice();
        alice.setBob(bob);

        return alice;
    }

}

AliceDeserializer接收如下的JSON输入:

{
    "bob": {
        "number": 2
    }
}

我需要帮助从AliceDeserializer调用BobDeserializer。 我想为此使用自动机制,而不是使用@Autowired注入反序列化器。

谢谢

1 个答案:

答案 0 :(得分:2)

通常,在编写自定义反序列化器时,您需要确保可以使用ObjectMapper进行序列化,而不是实际解析每个字段。执行此操作后,ObjectMapper将自动调用对象的反序列化程序(如果存在)。

代码:

@JsonDeserialize(using = BobDeserializer.class)
class Bob {

    private Integer number;

    public Bob() {
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }
}

@JsonDeserialize(using = AliceDeserializer.class)
public class Alice {
    private Bob bob;

    public Alice() {
    }

    public Bob getBob() {
        return bob;
    }

    public void setBob(Bob bob) {
        this.bob = bob;
    }
}

class BobDeserializer extends StdDeserializer<Bob> {

    protected BobDeserializer() {
        super(Bob.class);
    }

    public Bob deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode node = parser.getCodec().readTree(parser);

        Integer number = node.get("number").asInt();

        Bob bob = new Bob();
        bob.setNumber(number);

        return bob;
    }
}

class AliceDeserializer extends StdDeserializer<Alice> {

    protected AliceDeserializer() {
        super(Alice.class);
    }

    public Alice deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode node = parser.getCodec().readTree(parser);

        ObjectMapper mapper = new ObjectMapper();
        Bob bob = mapper.readValue(node.get("bob").toString(), Bob.class);

        Alice alice = new Alice();
        alice.setBob(bob);

        return alice;
    }
}

反序列化测试代码:

Alice alice = new Alice();
Bob bob = new Bob();
bob.setNumber(3);
alice.setBob(bob);

ObjectMapper mapper = new ObjectMapper();

Alice alice1 = mapper.readValue(mapper.writeValueAsString(alice), Alice.class);