Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 515
Michu93 Avatar asked Dec 14 '18 08:12

Michu93


People also ask

How do I copy an object in debug mode IntelliJ?

This action is bound to Numpad * key ( * ) by default and opens the whole object tree. Then you select all the elements of opened object tree with shift and copy them to clipboard. You can also use ctrl/cmd-A to select all variables and then copy them.

Where is JSON file in IntelliJ?

Search for a key - press CTRL + ALT + SHIFT + F (Windows) or Cmd + Shift + Option + F (MacOS) Right click on the selected text and pick the option: Find Key in JSON.


Video Answer


3 Answers

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
like image 67
Brad Parks Avatar answered Oct 19 '22 00:10

Brad Parks


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 )
like image 45
Rlarroque Avatar answered Oct 19 '22 01:10

Rlarroque


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

like image 7
glytching Avatar answered Oct 19 '22 00:10

glytching