Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonView: support more than one view #84

Closed
renaudcerrato opened this issue Jun 10, 2016 · 4 comments
Closed

@JsonView: support more than one view #84

renaudcerrato opened this issue Jun 10, 2016 · 4 comments

Comments

@renaudcerrato
Copy link

Currently, @JsonView annotation on endpoint only use the first view passed to it.

@cowtowncoder
Copy link
Member

I am not sure I understand this: how would defining of multiple views work? Jackson requires a single view to be used for serialization, so endpoints can only similarly define one active view to use via annotation. So what would be your use case; what are you trying to achieve?

@renaudcerrato
Copy link
Author

renaudcerrato commented Jun 10, 2016

Mmmm... I see, that make sense. What I'm trying to achieve: my User model have an email field. I'd like to somewhat obfuscate that field on public view only:

{
    "firstname": "John",
    "lastname": "Doh",
    "email": "j**n***h@g**i**o*
}

The above could probably be easily achieved by using a custom serializer or writerFor() but since I'm in a jax-rs resource, I don't want to have to deal with my ObjectMapper directly. I was thinking about using views for that.

Any ideas?

@cowtowncoder
Copy link
Member

@renaudcerrato Ah yes. So dynamically changing the active view, based on user.

This is actually why a new (#33 included in 2.3.0...) feature was added to allow for configuring ObjectReader and/or ObjectWriter used. Classes you'll need are:

  • ObjectWriterInjector, ObjectWriterModifier

and test at json/src/test/java/com/fasterxml/jackson/jaxrs/json/dw/WriteModificationsTestBase.java might be useful. I wish there was better documentation, but maybe that'll get you started.

For what it's worth, changing view was the reason I added this functionality; definitely makes sense to be able to choose it, but seemed very difficult to be able to provide such changeability using pure Annotation based approach.

@renaudcerrato
Copy link
Author

Amazing @cowtowncoder ! Thanks for pointing me out to this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants