Michal Zubkowicz
Michal Zubkowicz

Reputation: 93

Ignore field with annotation for JSON serialization but not for Mongo in MongoJack

Is there any possibility to ignore field for JSON serialization (for web display) but not for mongo (internal serialization) ?

I`ve tried so far all these methods but field was also ignored for Mongo, or not ignored in both in case of some variations

Jackson: how to prevent field serialization

Upvotes: 1

Views: 1412

Answers (2)

Michal Zubkowicz
Michal Zubkowicz

Reputation: 93

Ok, I finally solved this.

objectMapper.writerWithView(Views.Public.class).writeValueAsString(lo)); 

writeValueUsingView is from another version of Jackson, so it wasn't working

Upvotes: 1

xvronny
xvronny

Reputation: 84

Custom serialization for web/mongo can be solved by using @JsonView annotations, try along these lines:

class Views {
    static class OnAllViews {}
    static class OnlySomeViews extends OnAllViews {}
    ...
}

public class Thing {
    @JsonView(Views.OnAllViews.class) Integer id;
    @JsonView(Views.OnlySomeViews.class) String name;
}

and then you can call the appropriate level of serialization through writeValueUsingView method.

objectMapper.writeValueUsingView(out, beanInstance, ViewsPublic.class);

You can read more about it here.

Upvotes: 0

Related Questions