如何让flexjson TypeLocator抛出缺失值的异常?

时间:2014-09-07 17:31:22

标签: java flexjson

我有一个像这样的JSONDeserializer:

    if (json != null)
    {
        response = new JSONDeserializer<EnvelopeBuilder>().use(null, EnvelopeBuilder.class)
        // Target
                .use("header.targetIdentifier", EnvelopeJsonCodec.locator)
                // Source
                .use("header.sourceIdentifier", EnvelopeJsonCodec.locator)
                // Do it
                .deserialize(json);

        EnvelopeJsonCodec.logger.debug(new StringBuffer("Builder: ").append(response).toString());
    }

它依赖于像这样的类型定位器:

private static final TypeLocator<String> locator = new TypeLocator<String>("msg-type")
// Request / Response
        .add("TARGETED_MESSAGE", ServiceIdentifier.class)
        // Publish / Subscribe
        .add("PUBLISH", PublishChannel.class);

但我意识到,由于客户端不一定在我的控制之下,它们可能会错误地输入msg-type,然后我会得到一个空对象。

我可以稍后测试null对象,但是对于那些落后于我的人来说,对象为null的原因并不明显是因为msg类型错误。

如果msg-type出错,怎么能让TypeLocator抛出异常?

1 个答案:

答案 0 :(得分:0)

最终,我通过更严格的架构控制了错误的条目。

相关问题