javajsonjacksonvert.x

Jackson swallows empty JSON object upon parsing in Vertx


The code in Vert.x-Web "controller" of vertxVersion=4.5.11

var mapper = DatabindCodec.mapper();
mapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
mapper.setSerializationInclusion( Include.NON_EMPTY );

...

router.patch( "/my" ).consumes( JSON ).produces( JSON ).blockingHandler( this::validate );

...

void validate( RoutingContext rc ) {
  JsonObject json = rc.body().asJsonObject();
  log.info( "validate {} >> {}", rc.body().asString(), json );
}

upon receiving a JSON request of:

{
    "object": {
        "name": "aaa",
        "data": {
            "weNr": []
        }
    }
}

the "data" field is being ignored, producing the following log:

validate {"object":{"name":"aaa","data":{"weNr":[]}}} >> {"object":{"name":"aaa"}}}

If the weNr Array is filled, the parsed JSON looks as expected:

validate {"object":{"name":"aaa","data":{"weNr":[9410]}}} >> {"object":{"name":"aaa","data":{"weNr":[9410]}}

If the "data" has some other not-empty field, it's preserved but the empty "weNr" gets still swallowed:

{
    "object": {
        "name": "aaa",
        "data": {
            "weNr": [],
            "boo":"far"
        }
    }
}

produces:

validate {"object":{"name":"aaa","data":{"weNr":[],"boo":"far"}}} >> {"object":{"name":"aaa","data":{"boo":"far"}}}}

How can I configure DatabindCodec.mapper() or DatabindCodec.mapper().getFactory() or something else, to allow for empty JSON objects?


Solution

  • It turns out, that mapper.setSerializationInclusion( NON_EMPTY ) has an influence not only on serialization of POJOs, but also - against any logic - on JSON parsing.

    As soon I replaced the NON_EMPTY with NON_NULL, the parsing and deserialization started working as expected.

    Needless to say, that Javadoc does NOT highlight this...