@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.
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.
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.
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.
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.
Yes. It's OK to use GSON/Jackson/Reflections library to implement toString() method.
There are few ways to implement toString method.
Reflections (Apache library)
@Override public String toString(){ return org.apache.commons.lang3.builder.ReflectionToStringBuilder.toString(this); }
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; }
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(); }
Hard-core toString() implementation
@Override public String toString() { return new StringBuilder() .append("field1:"+field1) .append("field2:"+field2) .toString(); }
Lombok annotations : Generates toString() at compile time
import lombok.ToString; @ToString public class ToStringExample {}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With