Search code examples
javaflexjson

How to have flexjson TypeLocator throw exception on missing value?


I have a JSONDeserializer like so:

    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());
    }

and it relies on a type locator like this one:

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

But I realize that since the clients are not necessarily under my control, they may typo the msg-type and I'll get a null object subsequently.

I can test for the null object later but for those coming behind me it will not be obvious that the reason the object is null is because the msg-type was wrong.

How can I have the TypeLocator throw an exception if the msg-type comes up wrong?


Solution

  • Ultimately, I controlled the errant entries through a stricter schema.