Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to use reflection when implementing toString()?

@Override  public String toString() {      return new Gson().toJson(this); } 

Am I breaking some good practice, "Joshua"-pattern thing, general design pattern or other convention by simply doing this as default behavior for my model objects?

toString() will anyhow only be used in debugging in the paradigm (Android) that we are currently using. That's also the reason why I like seeing the object in JSON since much ORM/json persistence will be happening through http->php/python->mysql and to the local SQLite.

like image 722
tortal Avatar asked May 13 '13 17:05

tortal


People also ask

Why should you override the toString () method?

When you create a custom class or struct, you should override the ToString method in order to provide information about your type to client code. For information about how to use format strings and other types of custom formatting with the ToString method, see Formatting Types.

How is toString implemented?

The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler. Else, the user implemented or overridden toString() method is called. Here are some of the advantages of using this method.

What is the purpose of the toString () method in object Why is it useful?

toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

Why is it wise to override the toString method in any class that you create from scratch?

You will not get what the object actually has in it. There will be no information about state or properties of an object. Therefore, it is recommended that every class you write must override toString() method so that you quickly get an overview of an object.


2 Answers

Yes. It's OK to use GSON/Jackson/Reflections library to implement toString() method.

There are few ways to implement toString method.

  1. Reflections (Apache library)

    @Override public String toString(){     return org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(this); } 
  2. JSON based implementation (GSON, Jackson libraries)

    // GSON library for JSON @Override public String toString(){     return new com.google.gson.Gson().toJson(this); }  // Jackson libabry for JSON/YAML @Override public String toString() {     try {         return new com.fasterxml.jackson.databind.ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);     } catch (com.fasterxml.jackson.core.JsonProcessingException e) {         e.printStackTrace();     }     return null; } 
  3. ToStringBuilder (available with apache-commons library)

    @Override public String toString() {     return new org.apache.commons.lang3.builder.ToStringBuilder(this).         append("field1", field1).         append("field2", field2).         toString(); } 
  4. Hard-core toString() implementation

    @Override public String toString() {     return new StringBuilder()         .append("field1:"+field1)         .append("field2:"+field2)         .toString(); } 
  5. Lombok annotations : Generates toString() at compile time

    import lombok.ToString;  @ToString public class ToStringExample {} 
like image 183
Amit Kaneria Avatar answered Sep 28 '22 13:09

Amit Kaneria


There's no harm in doing it this way. I would suggest you to create a static variable for your Gson instance and enable pretty printing:

static Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

This way the output from toString method will be formatted.

like image 27
Egor Neliuba Avatar answered Sep 28 '22 12:09

Egor Neliuba