Dumping a java object's properties

后端 未结 10 1561
-上瘾入骨i
-上瘾入骨i 2020-11-28 02:03

Is there a library that will recursively dump/print an objects properties? I\'m looking for something similar to the console.dir() function in Firebug.

I\'m aware o

相关标签:
10条回答
  • 2020-11-28 02:33

    I recommend you to use the GSON Lib fo Java.

    if You use Maven you can use this.

    Or you can download the Jar file from here.

    Here example how to use it:

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(obj);
    System.out.println(json);
    
    0 讨论(0)
  • 2020-11-28 02:35

    You could try XStream.

    XStream xstream = new XStream(new Sun14ReflectionProvider(
      new FieldDictionary(new ImmutableFieldKeySorter())),
      new DomDriver("utf-8"));
    System.out.println(xstream.toXML(new Outer()));
    

    prints out:

    <foo.ToString_-Outer>
      <intValue>5</intValue>
      <innerValue>
        <stringValue>foo</stringValue>
      </innerValue>
    </foo.ToString_-Outer>
    

    You could also output in JSON

    And be careful of circular references ;)

    0 讨论(0)
  • 2020-11-28 02:36

    Maybe you could use an XML binding framework like XStream, Digester or JAXB for that.

    0 讨论(0)
  • 2020-11-28 02:41

    You should use RecursiveToStringStyle:

    System.out.println(ReflectionToStringBuilder.toString(new Outer(), new RecursiveToStringStyle()));
    
    0 讨论(0)
提交回复
热议问题