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

Note: If you don’t want to change default behaviour, create a “default” renderer also with “use default renderer” settings, and put it first in the list, it will use that as default and you can switch to JSON on demand by right click on debugged variable -> use renderer: JSON Renderer.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)