从调试器获取IntelliJ IDEA中的JSON对象

jqjz2hbq  于 2022-10-07  发布在  IntelliJ IDEA
关注(0)|答案(8)|浏览(519)

有可能以Json的身份从调试器获取整个对象吗?有一个选项View text,但我能以某种方式View JSON吗?

d8tt03nd

d8tt03nd1#

编辑:正如评论中所指出的,这并不完美,因为对于某些变量,您将得到“stackoverflow”响应

正如@han先生的回答所暗示的,你可以这样做:

在IntelliJ调试器中以json by的身份查看对象的新方式

  • 转至File | Settings | Build, Execution, Deployment | Debugger | Data Views | Java Type Renderers
  • 点击+添加新的渲染器
  • 称其为JSON renderer
  • Apply renderer to objects of type提供java.lang.Object
  • 选择Use following expression:并提供如下表达式:
if (null == this || this instanceof String)
  return this;

new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);
  • 点击OK
  • 现在,当您在变量上选择Copy Value时,它将复制为json。

b4wnujal

b4wnujal2#

或者,如here所示,您可以在调试观察器中使用以下代码:

new ObjectMapper()
    .setSerializationInclusion(JsonInclude.Include.NON_NULL)
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString( myObject )
iih3973s

iih3973s3#

您可以使用用于IntelliJ的Show as ...插件。
一个小插件,用于显示调试器和控制台中的格式化数据。

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

bpzcxfmw

bpzcxfmw4#

您可以在IntelliJ上将此代码片段尝试到EVALUE表达式(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 );

image IntelliJ

dw1jzc5e

dw1jzc5e5#

如果您的项目中有gson依赖项,则可以创建监视变量

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

其中myObject是您的对象。

hgb9j2n6

hgb9j2n66#

只需按照以下步骤操作:文件|设置|构建、执行、部署|调试器|数据视图|Java类型呈现器,点击+添加新的呈现器,copy is OK :) u can choose another jar to format it

现在,申请,加入吧~

3qpi33ja

3qpi33ja7#

遵循instructions of @BradParks,并使用以下表达式。

对我来说,如果没有完全限定的类名,它就不起作用。我还对ObjectMapper进行了一些修改。出于某些我不理解的原因,即使我将Apply renderers to object of type设置为java.lang.Object,当用作writeValueAsString()方法的参数时,我也需要将this类型转换为(Object)this

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

相关问题