Get object as JSON in IntelliJ Idea from debugger

Mic*_*u93 6 debugging json intellij-idea

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

Bra*_*rks 29

编辑:如评论中所述,这并不完美,对于某些变量,您将获得“stackoverflow”响应

正如@韩先生的回答所建议的那样,您可以这样做:

添加一种将 Intellij 调试器中的对象查看为 json 的新方法

  • 即将 File | Settings | Build, Execution, Deployment | Debugger | Data Views | Java Type Renderers
  • 单击+以添加新渲染器
  • 称它为 JSON renderer
  • 供应java.lang.ObjectApply renderer to objects of type
  • 选择Use following expression:并提供这样的表达式:
if (null == this || this instanceof String)
  return this;

new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);
Run Code Online (Sandbox Code Playgroud)
  • 点击 OK
  • 现在,当您选择Copy Value一个变量时,它将复制为 json。 在此处输入图片说明

  • 我得到 Method throws 'java.lang.ClassNotFoundException' 异常。 (7认同)
  • 有时对于某些对象,我会遇到 StackOverflow 或 OutOfMemory 异常。否则就像魅力一样。谢谢。 (2认同)

Lut*_*hes 19

您可以在 IntelliJ 上的评估表达式(Alt + F8)中尝试此代码片段:

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 );
Run Code Online (Sandbox Code Playgroud)

图像智能


Rla*_*que 10

另外,由于看到这里,你可以使用下面的代码在你的调试守望者:

new ObjectMapper()
    .setSerializationInclusion(JsonInclude.Include.NON_NULL)
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString( myObject )
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下我们如何添加父元素 (2认同)

gly*_*ing 8

您可以为 IntelliJ使用Show as ...插件。

一个小插件,用于在调试器和控制台之外显示格式化数据。

使用 IntelliJ 的内置格式化功能。不再需要将值从调试器或控制台复制到文件中以在那里格式化它们。支持以下格式:JSON、SQL、XML、Base64 编码的 JSON、Base64 编码的文本

  • 不幸的是,正如插件的评论部分指出的那样,在调试时它似乎无法正常工作。我自己尝试过但没有成功。 (5认同)

Mr *_*Han 6

只需按照它:文件| 设置 | 构建、执行、部署 | 调试器| 数据视图 | Java类型渲染器,点击+添加新渲染, 复制就可以了:)你可以选择另一个jar来格式化它

现在,申请,加入吧~


blu*_*ker 6

如果gson您的项目有依赖性,您可以创建一个监视变量

new GsonBuilder().setPrettyPrinting().create().gson.toJson(myObject)
Run Code Online (Sandbox Code Playgroud)

你的对象在哪里myObject


Hon*_*dek 5

按照@BradParks 的说明进行操作,并使用以下表达式。

对我来说,如果没有完全限定的类名,它就无法工作。我还对ObjectMapper. 由于某种我不明白的原因,即使我已Apply renderers to object of type设置为java.lang.Object,我也需要this(Object)this用作方法的参数时那样进行类型转换writeValueAsString()

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);
Run Code Online (Sandbox Code Playgroud)