Home » Java » Core Java » Don’t Parse, Use Parsing Objects

About Yegor Bugayenko

Yegor Bugayenko
Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.

Don’t Parse, Use Parsing Objects

The traditional way of integrating object-oriented back-end with an external system is through data transfer objects, which are serialized into JSON before going out and deserialized when coming back. This way is as much popular as it is wrong. The serialization part should be replaced by printers, which I explained earlier. Here is my take on deserialization, which should be done by—guess what—objects.

La science des rêves (2006) by Michel Gondry

Say there is a back-end entry point, which is supposed to register a new book in the library, arriving in JSON:

  "title": "Object Thinking",
  "isbn: "0735619654",
  "author: "David West"

Also, there is an object of class Library, which expects an object of type Book to be given to its method register():

class Library {
  public void register(Book book) {
    // Create a new record in the database

Say also, type Book has a simple method isbn():

interface Book {
  String isbn();

Now, here is the HTTP entry point (I’m using Takes and Cactoos), which is accepting a POST multipart/form-data request and registering the book in the library:

public class TkUpload implements Take {
  private final Library library;
  public Response act(Request req) {
    String body = new RqPrint(
      new RqMtSmart(new RqMtBase(req)).single("book")
    JsonObject json = Json.createReader(
      new InputStreamOf(body)
    Book book = new BookDTO();

What is wrong with this? Well, a few things.

First, it’s not reusable. If we were to need something similar in a different place, we would have to write this HTTP processing and JSON parsing again.

Second, error handling and validation are not reusable either. If we add it to the method above, we will have to copy it everywhere. Of course, the DTO may encapsulate it, but that’s not what DTOs are usually for.

Third, the code above is rather procedural and has a lot of temporal coupling.

A better design would be to hide this parsing inside a new class JsonBook:

class JsonBook implements Book {
  private final String json;
  JsonBook(String body) {
    this.json = body;
  public String isbn() {
    return Json.createReader(
      new InputStreamOf(body)

Then, the RESTful entry point will look like this:

public class TkUpload implements Take {
  private final Library library;
  public Response act(Request req) {
      new JsonBook(
        new RqPrint(
          new RqMtSmart(new RqMtBase(req)).single("book")

Isn’t that more elegant?

Here are some examples from my projects: RqUser from zerocracy/farm and RqUser from yegor256/jare.

As you can see from the examples above, sometimes we can’t use implements because some primitives in Java are not interfaces but final classes: String is a “perfect” example. That’s why I have to do this:

class RqUser implements Scalar<String> {
  public String value() {
    // Parsing happens here and returns String

But aside from that, these examples perfectly demonstrate the principle of “parsing objects” suggested above.

Published on Java Code Geeks with permission by Yegor Bugayenko, partner at our JCG program. See the original article here: Don’t Parse, Use Parsing Objects

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments