Michu93
Michu93

Reputation: 5697

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?

Upvotes: 94

Views: 85996

Answers (11)

Brad Parks
Brad Parks

Reputation: 72011

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:

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);
  • Click OK
  • Now when you choose Copy Value on a variable, it will copy as JSON.

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.

Upvotes: 101

Umesh Raginda
Umesh Raginda

Reputation: 11

while you debug, navigate to "Evaluate Expression". inside that you can create a onject mapper instance.

new ObjectMapper().writeValueAsString(YOUROBJ)

after that you may get any object as a sting.

Upvotes: 1

felicityiz
felicityiz

Reputation: 9

worked for me: rightclick on the variable itself to select "Evaluate Expression"

In the popup with the evaluated expression you can right click the result and select "Copy JSON"

Upvotes: -1

Martin Patsov
Martin Patsov

Reputation: 386

In case someone is having hard time to make the renderers work for more complex objects - below you can find a combined JSON renderer from:

The following renderer helped me to identify multiple fields with the same name in the class hierarchy, so I could change that.

Initially I was having IllegalArgumentException for serialization of a nested object that I wasn't able to analyse.

If there is an issue during serialization, with this renderer you can find the stack trace from the exception that you need to fix in the console.

Good luck!


if (null == this)
    return "null";

if (this instanceof CharSequence
        || this instanceof Number
        || this instanceof Character
        || this instanceof Boolean
        || this instanceof Enum) {
    // Here you may add more sophisticated test which types you want to exclude from the JSON conversion.
    return this;
}
try {
    String json = new GsonBuilder().setPrettyPrinting().create().toJson(this);
    return json;
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 1

New Bee
New Bee

Reputation: 430

Use Intellij plugin Debug Variable Extractor More information - https://plugins.jetbrains.com/plugin/16362-debug-variable-extractor

Upvotes: 3

Luth Sanches
Luth Sanches

Reputation: 391

You can try this code fragment into the Evaluate Expression(Alt + F8) on IntelliJ :

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( myObject );

image IntelliJ

Upvotes: 28

Honza Zidek
Honza Zidek

Reputation: 20046

Follow the instructions of @BradParks, and use the following expression.

For me it did not work without fully-qualified class names. I also added some modifications to the ObjectMapper. For some reason which I don't understand, even if I have Apply renderers to object of type set to java.lang.Object, I needed to typecast this as (Object)this when used as a parameter of the writeValueAsString() method.

if (this == null 
|| this instanceof CharSequence 
|| this instanceof Number 
|| this instanceof Character 
|| this instanceof Boolean 
|| this instanceof Enum) {
// Here you may add more sophisticated test which types you want to exclude from the JSON conversion.
    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)
        .setVisibility(
                com.fasterxml.jackson.annotation.PropertyAccessor.FIELD, 
                JsonAutoDetect.Visibility.ANY)
        .setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
        .writerWithDefaultPrettyPrinter()         
        .writeValueAsString((Object)this);

Upvotes: 5

bluelurker
bluelurker

Reputation: 1444

If you have gson dependency in your project you can create a watch variable

new GsonBuilder().setPrettyPrinting().create().gson.toJson(myObject)

where myObject is your object.

Upvotes: 6

Mr Han
Mr Han

Reputation: 79

Just follow it : File | Settings | Build, Execution, Deployment | Debugger | Data Views | Java Type Renderers, click + to add new render , copy is OK :) u can choose another jar to format it

And now , Apply, join it ~

Upvotes: 6

Rlarroque
Rlarroque

Reputation: 2030

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 )

Upvotes: 28

glytching
glytching

Reputation: 47915

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

Upvotes: 7

Related Questions