1

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

Community
  • 1
  • 1

2 Answers2

1

Ok, I finally solved this.

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

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

0

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.

xvronny
  • 84
  • 5
  • Thx for reply! I've already tried this, but it's not working for me. `@JsonView(Views.Internal.class) private String password;` and then: `ObjectMapper mapper = new ObjectMapper(); mapper.readerWithView(Views.Public.class); Landlord g = collection.findOneById(j.get("_id").asText()); return mapper.readerForUpdating(g).readValue(j.toString());` – Michal Zubkowicz Jul 11 '14 at 11:13