Exclude fields in json Using Jackson and Json-View
Asked Answered
O

3

13

I am using json-view to create a dynamic json as per my need ,it is a great library ,I am using this library for a while now . Recently I am facing a problem with my one of the Use cases, let me place my code first

User class

public class User {

    private String name;
    private String emailId;
    private String mobileNo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

}

ScreenInfoPojo class

public class ScreenInfoPojo {

    private Long id;
    private String name;
    private ScreenInfoPojo parentScreen;
    private User createdBy;
    private User lastUpdatedBy;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ScreenInfoPojo getParentScreen() {
        return parentScreen;
    }

    public void setParentScreen(ScreenInfoPojo parentScreen) {
        this.parentScreen = parentScreen;
    }



    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public User getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(User lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    } 

Run code

public class TestMain {
    public static void main(String[] args) throws JsonProcessingException {
        User user=new User();
        user.setName("ABC");
        user.setEmailId("[email protected]");
        user.setMobileNo("123456789");

        ScreenInfoPojo screen1=new ScreenInfoPojo();
        screen1.setId(1l);
        screen1.setName("Screen1");
        screen1.setCreatedBy(user);
        screen1.setLastUpdatedBy(user);

        ScreenInfoPojo screen2=new ScreenInfoPojo();
        screen2.setId(2l);
        screen2.setName("Screen2");
        screen2.setParentScreen(Screen1);
        screen2.setCreatedBy(user);
        screen2.setLastUpdatedBy(user);

        ScreenInfoPojo screen3=new ScreenInfoPojo();
        screen3.setId(3l);
        screen3.setName("Screen3");
        screen3.setParentScreen(Screen2);
        screen3.setCreatedBy(user);
        screen3.setLastUpdatedBy(user);

        ScreenInfoPojo screen4=new ScreenInfoPojo();
        screen4.setId(4l);
        screen4.setName("Screen4");
        screen4.setParentScreen(Screen3);
        screen4.setCreatedBy(user);
        screen4.setLastUpdatedBy(user);

        List<ScreenInfoPojo> screens=new ArrayList<>();
        screens.add(screen1);
        screens.add(screen2);
        screens.add(screen3);
        screens.add(screen4);

        ObjectMapper mapper = new ObjectMapper().registerModule(new JsonViewModule());
        String json = mapper.writeValueAsString(JsonView.with(screens).onClass(ScreenInfoPojo.class, Match.match()
                .exclude("*")
                .include("id","name","createdBy.name","lastUpdatedBy.mobileNo","parentScreen.id")));
        System.out.println("json"+json);
    }    

Result

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

Expected Result

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

Problem

In my use case I have a class ScreenInfoPojo which refers to same class as parentScreen , I am trying to fetch specific field/fields of parent ( "parentScreen.id") instate I am getting all fields that I have defined on child/target Object ("id","name","createdBy.name","lastUpdatedBy.mobileNo","parentScreen.id") and parent response is again recursive ! One thing i observed that It is only happening in case of a class has its own reference , I placed User class reference as two different field createdBy and lastUpdatedBy and tried to fetch "name" and "mobileNo" respectively worked just fine.

Any suggestion to solve this problem will be really helpful !!!!

Thanks

Overissue answered 18/5, 2017 at 12:10 Comment(7)
Can you simply make another POJO with id property only and assign it to parentScreen property?Whiny
No i can not do thatOverissue
Why? From my perspective it's a distinct entity - kind of ScreenRefPojo.Whiny
My Application has multiple classes(pojo) ,and at runtime Match is applied,means as per requirement of client it will as for attributes(using ajax), so creating a different class will add an extra overhead.Overissue
I've seen that you opened an issue at json-view (and I agree that your code should behave as expected), but have you at least tried to exclude parentScreen.* and/or parentScreen.name, parentScreen.createdBy ... ?Inspan
Yes I Tried , but it doesn't make any difference same issue still persistOverissue
@Overissue Why are you passing your entire parent screen to the function? You are only trying to pass the ID of the previous, not the whole bleeding object. Because you are passing the entire object, your code would consistently keep producing nested fields of the previous parents!Brushwork
K
3

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);
Kele answered 23/5, 2017 at 15:52 Comment(1)
Thanks for your valuable answer , actually I had already done this ,I didn't wanted to modify library source code, but it seems that there is no other way accept this work around. thanks for spending your valuable time.Overissue
A
3

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..

Aubyn answered 23/5, 2017 at 16:27 Comment(1)
Yea and using this approach he just add one additional field with the parent id to still have the reference for minimal amount of code refactoring. seems pretty straightforward.Triceratops
B
2

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
      }
   }
]
Breastplate answered 21/5, 2017 at 15:6 Comment(1)
Thanks for answering , I can apply this solution if i have to deal with this issue only in one or two classes , but sadly i have many classes(Entity) facing this issue and this json attributes my change for different request for same api ,so i can not use custom serializer for a Entity class I have to make it dynamic .Overissue

© 2022 - 2024 — McMap. All rights reserved.