This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How do you serialize different fields to json depending on the circumstances?

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, how do you do this elegantly say using Jackson or maybe Gson? If you are writing a web based commenting system with 2 pages only:
1) Main page showing comments and the name of the user who posted each comment.
2) Member detail page showing all information such as join date, post count, last posted date, etc for any given user.

You naturally have a situation where you'd need to serve different amount of information for the same User object depending on which page you are serving.
If I use something like Spring Data's findAll() method (ie commentRepository.findAll()), I'll be able to get obviously all the comments (assuming we have a small system here) and the users (via predefined @OneToOne annotation).
If I then serialize to JSON using Jackson, I should see something like the following.


Then if I use the following Handlebars template, I'd be able to show just only the name of the person who posted the comment, and the rest of the user details such as id, totalPosts, lastPostedOn would never even hit the browser.


And then on the other page I can use something like the following to show all the details I want for each user.


So far so good. I can use the same User model and display different level of detail depending on what I specify in the template.

But this only works if you are using a template that gets rendered on the server such as handlebars.java. What if you now have a template that gets rendered on the client side such as handlebars.js?
If we are running the same handlebars template on the browser, the server will send all fields from the User model (ie all columns per user from the database) to the client before it gets picked out by the template on which one to use. The disadvantages are:
1) Poor performance because we are sending unnecessarily large amount of traffic to the browser.
2) Unable to hide details because if the User object contains confidential fields such as e-mail address, phone numbers, etc we don't want to send all these to the browser even if they won't be displayed at the end.

So the question is how can we do this properly? Has anyone come across this scenario? I'm hoping that I may find a way to have the Jackson 2 library to serialize only some of the fields to JSON in one case and all of the fields to JSON in another case depending on a flag which indicates which page we're serving but initial research doesn't seem like this is supported. There are custom serializers but this seems to be a bit complicated for a scenario that is supposed to be quite common. Do I have to write two User model classes now using two different native queries (one selecting some columns and another selecting more columns)? What would you do?
 
Mike Cheung
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only sensible way I can see is really resorting to using multiple Java classes to represent the same thing, and use the correct one depending on the scenario. For our example case, we'd have a User class that has all the fields and will be used for the user list page where we want to show all details. Then if we want to serve the comments page, we'd have to retrieve the details from database like how we normally would, and then convert it to a trimmed down class before serializing to JSON.

Normal Classes




Trimmed Down Classes


 
Mike Cheung
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay another way of doing it, if we are using the new Java 1.8 Function interface, can be like this. This is based on a description on how we can do this functionally from http://www.leveluplunch.com/java/tutorials/016-transform-object-class-into-another-type-java8/.

This class is the same as before, except we no longer need the constructor to do the conversion.


Again no custom constructor required here.


The conversion is now handled via this function which is implemented as an Anonymous Class. The logic for the conversion now takes place here.




And here is how we do the transform now.


Question is which one is better?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic