Exclude fields in json Using Jackson and Json-View Exclude fields in json Using Jackson and Json-View json json

Exclude fields in json Using Jackson and Json-View


You can simply use jackson annotation @jsonignore on the field that you do not want in the json response.

I don't know whether you can or not use any annotations on your code . If so this is useless..


Yes. Include clause does not work for reference to the same class.

That you can do?

Compile from source according to github instruction build from source

Update function JsonViewSerializer.JsonWriter.fieldAllowed

find:

        if(match == null) {            match = this.currentMatch;        } else {            prefix = "";        }

and comment else clause

        if(match == null) {            match = this.currentMatch;        } else {            //prefix = "";        }

You will get expected result. But. I do not know how it will affect another filters.

To have more control you could add property to JsonView class.

For example:

in JsonView add:

private boolean ignorePathIfClassRegistered = true;     public boolean isIgnorePathIfClassRegistered() {            return ignorePathIfClassRegistered;        }        public JsonView1<T>  setIgnorePathIfClassRegistered(boolean ignorePathIfClassRegistered) {            this.ignorePathIfClassRegistered = ignorePathIfClassRegistered;            return this;        }

In JsonViewSerializer.JsonWriter.fieldAllowed function rewrite if clause to:

        if(match == null) {            match = this.currentMatch;        } else {            if (result.isIgnorePathIfClassRegistered())            prefix = "";        }

And you could use it in your example like:

JsonView<List<ScreenInfoPojo>> viwevedObject = JsonView        .with(screens)        .onClass(ScreenInfoPojo.class,                Match.match()                        .exclude("*")                        .include("id","name")                        .include("createdBy.name")                        .include("lastUpdatedBy.mobileNo")                        .include("parentScreen.id"))        .setIgnorePathIfClassRegistered(false);ObjectMapper mapper = new ObjectMapper().registerModule(new JsonViewModule());mapper.configure(SerializationFeature.INDENT_OUTPUT, true);String json = mapper.writeValueAsString(viwevedObject);


The most flexible way to serialize an object is to write a custom serializer.

If I understood your requirements correctly, the following serializer might work:

public class CustomScreenInfoSerializer extends JsonSerializer<ScreenInfoPojo> {    @Override    public void serialize(ScreenInfoPojo value, JsonGenerator gen, SerializerProvider serializers)            throws IOException, JsonProcessingException {        gen.writeStartObject();        gen.writeNumberField("id", value.getId());        gen.writeStringField("name", value.getName());        gen.writeFieldName("createdBy");        gen.writeStartObject();        gen.writeStringField("name", value.getCreatedBy().getName());        gen.writeEndObject();        gen.writeFieldName("lastUpdatedBy");        gen.writeStartObject();        gen.writeStringField("mobileNo", value.getLastUpdatedBy().getMobileNo());        gen.writeEndObject();        if (value.getParentScreen() == null) {            gen.writeNullField("parentScreen");        }        else {            gen.writeFieldName("parentScreen");            gen.writeStartObject();            gen.writeNumberField("id", value.getParentScreen().getId());            gen.writeEndObject();        }        gen.writeEndObject();    }}

Using

ObjectMapper mapper = new ObjectMapper();SimpleModule module = new SimpleModule();module.addSerializer(ScreenInfoPojo.class, new CustomScreenInfoSerializer());mapper.registerModule(module);String json = mapper.writeValueAsString(screens);System.out.println(json);

produces

[   {      "id": 1,      "name": "Screen1",      "createdBy": {         "name": "ABC"      },      "lastUpdatedBy": {         "mobileNo": "123456789"      },      "parentScreen": null   },   {      "id": 2,      "name": "Screen2",      "createdBy": {         "name": "ABC"      },      "lastUpdatedBy": {         "mobileNo": "123456789"      },      "parentScreen": {         "id": 1      }   },   {      "id": 3,      "name": "Screen3",      "createdBy": {         "name": "ABC"      },      "lastUpdatedBy": {         "mobileNo": "123456789"      },      "parentScreen": {         "id": 2      }   },   {      "id": 4,      "name": "Screen4",      "createdBy": {         "name": "ABC"      },      "lastUpdatedBy": {         "mobileNo": "123456789"      },      "parentScreen": {         "id": 3      }   }]