Ability to change active JsonView on submodels
See original GitHub issueI have two very simplified model objects here…
class User {
@JsonView({ Views.Detail.class, Views.Summary.class })
String name;
@JsonView({ Views.Detail.class })
Set<Post> posts = new HashSet<>();
}
and
class Post {
@JsonView({ Views.Detail.class, Views.Summary.class })
Date publishDate;
@JsonView({ Views.Detail.class, Views.Summary.class })
String title;
@JsonView({ Views.Detail.class })
Set<Comment> comments = new HashSet<>();
}
I have a JAXRS endpoint annotated with @JsonView
, so serialization is being handled outside of my direct control. I would like to serialize User at the Detail view, but have its collections (in this case, posts) serialize using the Summary view.
I looked at making my own JsonSerializer, but looking at the SerializerProvider, I haven’t found a way to change the active view and I’d hate to have to make another view class. Any pointers? Or any other way to think about this?
(ps - where’s a good place to post questions like this? I don’t really want to clutter the Issues queue unless it requires new code. I posted on StackOverflow but saw nothing there)
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Jackson JSON Views - Baeldung
First – let's go through a simple example – serialize an object with @JsonView. Here is our view: public class Views { public...
Read more >Jackson's @JsonView annotation does not work
1 Answer 1 · all fields are included by default if you do not change this behavior (see BeanSerializerFactory.processViews ). To change default...
Read more >Designing a privacy aware infrastructure for an Inclusive ...
The resulting privacy policy specifications should therefore be able to deal with such changes in a consistent manner. 2.5.3 Access control.
Read more >devgeniem/dustpress - Packagist
A WordPress theme framework for writing template files with Dust.js templating engine and separate data models. Installation. We recommend that ...
Read more >Simple index - piwheels
... kfij odoo-addon-purchase-order-qty-change-no-recompute area-detector-handlers ... odoo14-addon-product-category-active django-scaffold-toolkit ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve just stumbled across a very similar problem, but I ruled out using views exactly because there’d be oddities with trying to change the view dynamically. Plus specifying a static tree of views from the top level really couples code together too much. What I was thinking might work better is to change JsonView to take a map of views to serializers. Then you can treat a property slightly different for each view.
with the ability to merge multiple views on the same property #38 I think this gives a powerful way to specify how you want a property to be treated different based on view. (I’m reusing the JsonSerialize annotation so you can specify all sorts of different ways to serialize the data. As well, I’m sure there’d be a deserialize property too.)
In my case I’m trying to serialize the inner property differently based on view, which I’d set on my JAX-RS endpoint. I believe this covers the intent of what’s being requested here; although this doesn’t quite give the ability to change the views dynamically, which I don’t think is the right direction.
I don’t have anything specific planned at this point.