Skip to content

Instantly share code, notes, and snippets.

@nieldw
Created October 16, 2015 07:48
Show Gist options
  • Save nieldw/12ed4e7d29df6e70758b to your computer and use it in GitHub Desktop.
Save nieldw/12ed4e7d29df6e70758b to your computer and use it in GitHub Desktop.
Java toString() generator for IntelliJ IDEA that generates JSON using Gson
public java.lang.String toString() {
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(this);
}
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
@nieldw
Copy link
Author

nieldw commented Oct 16, 2015

This template is handy to add to your IntelliJ (can someone comment with instructions for Eclipse, Netbeans?) toString() templates.

  • Open the generate menu (alt + ins), choose toString()
  • Click on Settings

image

  • Go to the Templates tab

image

  • Add and name a new template
  • Paste the template in the text area

image

  • Apply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment