【问题标题】:How to have flexjson TypeLocator throw exception on missing value?如何让 flexjson TypeLocator 对缺失值抛出异常?
【发布时间】:2014-11-01 00:09:00
【问题描述】:

我有一个像这样的 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 抛出异常?

【问题讨论】:

    标签: java flexjson


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 2013-06-13
      • 2011-06-28
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多