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

Support for default @JsonView for a class #507

Closed
markwoon opened this issue Jul 26, 2014 · 15 comments
Closed

Support for default @JsonView for a class #507

markwoon opened this issue Jul 26, 2014 · 15 comments
Milestone

Comments

@markwoon
Copy link
Contributor

I'd like to be able to specify a default view for all properties of a given class that doesn't already have a view defined on it. This would remove the need to annotate every single property.

Something along the lines of:

@JsonDefaultView(A.class)
public class Foo {
}
@markwoon markwoon changed the title Support a default @JsonView Support for default @JsonView for a class Jul 26, 2014
@cowtowncoder
Copy link
Member

And presumably this would be similar to @JsonView to allow more than one view, I am guessing.

@markwoon
Copy link
Contributor Author

That would be nice, but not requirement for my needs... :)

Do you think this is feasible?

@cowtowncoder
Copy link
Member

It wouldn't be more difficult, and more importantly, it would then be compatible with regular JsonView handling, as this would just add one more level of defaulting. And you can still use simple short-hand notation for single value.

And yes, I think feature itself is feasible, just needs to go in 2.5 since it adds a new annotation type.

@tadas-subonis
Copy link

Is this expected anytime soon? at the moment it is really tedious to go and label each of the properties with their respective JsonView.

@rgoldberg
Copy link
Contributor

rgoldberg commented Sep 29, 2016

Instead of adding a whole new @JsonDefaultView annotation, can you add TYPE to the @Target.value of @JsonView, so that it can be applied to types? Can you add an element named ancestors to @JsonView whose type is a new CombinationStyle enum with values OVERRIDE and UNION? You could also add a descendants element, also of type CombinationStyle. You'd have to spec out if descendants takes precedence over ancestors, or vice versa (I prefer the former). ancestors & descendants should each default to one of the CombinationStyle, whichever is more consistent with current behavior for the respective element.

@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 29, 2016

@tadas-subonis I don't think anyone is working on this at the moment, so it is not known if or when this would be implemented. I'll tag this for 2.9 to at least make it so it will be evaluated as simple implementation might not be too difficult.

@rgoldberg First part -- making @JsonView usable on Class, makes sense.
But I am not sure I follow the latter part (beyond general idea of allowing inheritance of some sort for views across type hierarchy). An example would be nice to show an example of what you are thinking.

@cowtowncoder cowtowncoder changed the title Support for default @JsonView for a class Support for default @JsonView for a class Oct 5, 2016
cowtowncoder added a commit that referenced this issue Oct 7, 2016
@cowtowncoder cowtowncoder added this to the 2.9.0 milestone Oct 11, 2016
@cowtowncoder
Copy link
Member

Implemented for 2.9.0 so that @JsonView can now be used on classes, and doing so will use said views as defaults, possibly overridden by per-property annotation as before.

@markwoon
Copy link
Contributor Author

I've been waiting so long for this! :)

Is 2.9 coming soon, or still months away?

@cowtowncoder
Copy link
Member

@markwoon was hoping to get rc1 out before end of the year, but that is not to be (leaving for vacation tomorrow). That said, should get rc1 out by end of january or so. So... somewhere in between the two.

@sdeleuze
Copy link

Interesting issue, FYI this kind of need is why we had to configure MapperFeature.DEFAULT_VIEW_INCLUSION to false in Spring by default, otherwise @JsonView is not usable easily for most non-trivial web application use cases I have seen.

@cowtowncoder Since MapperFeature.DEFAULT_VIEW_INCLUSION only concern non-annotated fields/properties, I guess we can expect class level @JsonView working as expected even with MapperFeature.DEFAULT_VIEW_INCLUSION set to false right?

@cowtowncoder
Copy link
Member

@sdeleuze Right, so default should only apply to non-annotated properties, but would then "override" setting of false in a sense. Or put another way: having setting as `false would NOT prevent class annotation from taking effect.

@yugander-krishan-singh
Copy link

yugander-krishan-singh commented Mar 27, 2019

@cowtowncoder I am a bit confused about class-level @JSONVIEW annotation.
After going throught he above discussions thread I tried the class-level @JSONVIEW annotations.
First, I have disabled DEFAULT_VIEW_INCLUSION in Object mapper. Now I have a class level JsonView annotation(with default view class) on my Entity class which has both annotated and non-annotated fields.

Now when I serialize my entity class by passing the default view class I am not getting the non-annotated fields of the class in the output.
But when I enable DEFAULT_VIEW_INCLUSION again the o/p is as expected.
It seems the class-level @JSONVIEW annotation has no effect on the o/p.

Pls have a look at this stackoverflow question which is not exactly same but similar.

@matiasah
Copy link

matiasah commented Oct 16, 2020

@cowtowncoder couldn't @JsonDefaultView be implemented so that you can make a class always use a particular view if no view is specified for serialization. It would be different than using @JsonView on class level.

@matiasah
Copy link

matiasah commented Oct 16, 2020

The way I'm currently dealing with "hiding all the views by default" is by doing the following.

objectMapper.setConfig(objectMapper.getSerializationConfig().withView(Object.class));

But it would be awesome to set this property per class

@cowtowncoder
Copy link
Member

@matiasah If you have specific new feature in mind, please file a new issue; I don't usually open old issues. I am also not quite sure why exactly a new annotation would be needed (or perhaps new setting), so you could outline expected semantics, above and beyond current use of @JsonView.

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

7 participants