question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to change active JsonView on submodels

See original GitHub issue

I 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:open
  • Created 8 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisregniercommented, Nov 9, 2016

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.

class MyObj {
  @JsonView(view=Public.class, serialize=@JsonSerialize(using=MyPublicSerializer.class))
  @JsonView(view=Internal.class, serialize=@JsonSerialize(converter=FooToBar.class))
  public Foo foo;
}

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.

0reactions
cowtowncodercommented, Jul 17, 2018

I don’t have anything specific planned at this point.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found