Get object as JSON in IntelliJ Idea from debugger

EDIT: as noted in the comments, this is not perfect, as for some variables you will get a "stackoverflow" response

As suggested by @Mr Han's answer, here's how you can do this:

Add a new way to view objects in Intellij debugger as json by

  • Going to File | Settings | Build, Execution, Deployment | Debugger | Data Views | Java Type Renderers
  • Click + to add new renderer
  • Call it JSON renderer
  • Supply java.lang.Object for Apply renderer to objects of type
  • Choose Use following expression: and supply an expression like so:
if (null == this || this instanceof String)
  return this;

new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);
  • Click OK
  • Now when you choose Copy Value on a variable, it will copy as json. enter image description here

Alternatively, as seen here, you can use the following piece of code in your debug watcher:

new ObjectMapper()
    .setSerializationInclusion(JsonInclude.Include.NON_NULL)
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString( myObject )

You could use the Show as ... plugin for IntelliJ.

A small plugin to display formatted data out of the debugger and console.

Uses IntelliJ's build-in formatting capabilities. No more need to copy values from debugger or console to a file to format them there. Following formats are supported: JSON, SQL, XML, Base64 encoded JSON, Base64 encoded text