jsonvert.x

Remove Map Node from vert.x JsonObject serialized String


Vert.x JsonObject Serialized String contains Map and empty filed

The structure of the Object I want to serialize is

public class MongoDatasourceResponse {

    private Mongoresponse response;
    private List<JsonObject> data;
    private int size;
    private Error error;

    //get, set method...
}

Vert.x JsonObject Serialized String contains "Map" and "empty" filed, But I do not want to have all these extra "map" and "empty" nodes. What I can I do to remove them?

{
    "code": -1,
    "data": [
        {
            "map": {
                "n": 0,
                "writeErrors": {
                    "list": [
                        {
                            "map": {
                                "index": 0,
                                "code": 11000,
                                "errmsg": "E11000 duplicate key error collection: test.users index: _id_ dup key: { : 5 }"
                            },
                            "empty": false
                        },
                        {
                            "map": {
                                "index": 1,
                                "code": 11000,
                                "errmsg": "E11000 duplicate key error collection: test.users index: _id_ dup key: { : 6 }"
                            },
                            "empty": false
                        },
                        {
                            "map": {
                                "index": 2,
                                "code": 11000,
                                "errmsg": "E11000 duplicate key error collection: test.users index: _id_ dup key: { : 3 }"
                            },
                            "empty": false
                        },
                        {
                            "map": {
                                "index": 3,
                                "code": 11000,
                                "errmsg": "E11000 duplicate key error collection: test.users index: _id_ dup key: { : 7 }"
                            },
                            "empty": false
                        }
                    ],
                    "empty": false
                },
                "ok": 1.0
            },
            "empty": false
        }
    ],
    "size": 0,
    "error": null
}

Solution

  • Vertx's JsonObject keeps some internal fields like "map" and if you use Jackson ObjectMapper to serialize, it will serialize with all those fields.

    To avoid this, you can use a custom Serializer like below.

    public class MyPojo{
        public String someField;
        //Vertx JsonObject
        @JsonSerialize(using = VertxJsonObjectSerilizer.class)
        public JsonObject someObject;
    }
    
    public class VertxJsonObjectSerilizer extends SerializerBase<JsonObject> {
    
    
        @Override
        public void serialize(JsonObject jsonObject, JsonGenerator jsonGenerator, SerializerProvider provider)
            throws IOException, JsonProcessingException {
            
             if(jsonObject == null) {
                 jsonGenerator.writeNull();
                 return;
             }
             jsonGenerator.writeRawValue(jsonObject.encode());
        }
    
    }