Quickly Visualizing Complex Object Structures with PrettyPrinter

Alright, fellow Java devs and Vaadin aficionados, let’s talk about something we’ve all(!?) been guilty of at one point: representing DTOs by dumping pretty-printed JSON into our UIs. This is what I have done if I have had Jackson on the classpath:

add(new Pre(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(dto));

Sure, it’s somewhat “readable” (for developers), but let’s be real—it’s far from user-friendly. Our users, and even our fellow developers, deserve better than that. I have been working for Vaadin, a company focusing on (Java) web UIs, and still I have been guilty of this. Sometimes I have used it only as a stepping stone towards a more aesthetic UI, but I have a hobby project where this is the main presentation of the computation. And even Jackson sometimes fails with the above: one might need to handle (circular) references somehow, configure Jackson to support LocalDateTime’s etc.

Let’s be honest with ourselves: maybe we’re doing it wrong.

The data we’re working with can be presented so much more effectively using proper UI components like tables, grids, or other even with some more visual components. It’s more than just a matter of aesthetics—it’s about usability and clarity. That’s why I started building PrettyPrinter as part of the Viritin add-on. It’s a helper that prints data with one liner in a cleaner and friendlier format than JSON.

Check out the small demo here, and start giving your users (and yourself) the UI they deserve.

The first version is out, but I’m very willing to re-iterate with naming and API, I’m sure you have some ideas! The underlying DtoDisplay class already contains some API to also customize the presentation, if you for example wish to make certain properties of a certain type of objects somehow specially formatted. These “property printers” I plan to bring to the PrettyPrinter level (or whatever the final name shall be).

I also plan to prototype with a version of the API (or making this a separate library) that could output raw HTML or preformatted text. I think the whole Java ecosystem would need improvements for this area, even if they didn’t use Vaadin (Flow).

3 Likes

been wanting to do this many times myself before, glad you finally beat me to it ;-)

3 Likes

Now you just need to add custom renderers and editor support :smiley:

1 Like

You are reading my mind :nerd_face: Custom renderers (PropertyPrinter interface) are kind of already started if you check into the details of DtoDisplay. Planning to bring them to PrettyPrinter level somehow though.

And the next step is indeed autogenerated forms (in the spirit of Vaadin 3.0-6.0 forms), but with a modern approach. I’m probably the one to blaim why the autogeneration of forms was dropped, but I do think that approach has its use cases as well (RAD, simple admin UIs). Some things are better these days, for example Java’s reflection gives properties in the declaration order → sane order for fields without configuration.

1 Like

:-D Let’s relive the Model Driven Development woes from 20 years ago.

2 Likes