jsondebuggingintellij-idea

Get object as JSON in IntelliJ Idea from debugger


Is it possible to get the whole object from debugger as Json? There is an option View text but can I somehow View JSON?


Solution

  • 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:

    If you have Gson dependency in the classpath:

    if (null == this || this instanceof String)
      return this;
    
    new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);
    

    or if you have Jackson dependency in classpath:

    if (null == this || this instanceof String)
      return this;
    
    new com.fasterxml.jackson.databind.ObjectMapper().registerModule(new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule()) .disable(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).writerWithDefaultPrettyPrinter() .writeValueAsString(this);
    

    intellij settings

    Note: If you don't want to change the default behavior, 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.