'Save state of object in IntelliJ debug?

Does anyone know whether it is possible to save the state of an object while debugging?

I would like to compare the state of an object in two different execution cycles.

Bonus Question: Anyone with experience in writing IntelliJ plugins? Does the IntelliJ SDK allow to access debug values in debug mode? Read them out of IntelliJ cache and write them to disk?



Solution 1:[1]

To expand on Josep's answer, your best bet is to import Google's Gson library into your project and run:

Gson gson = new Gson();
gson.toJson(yourObject);

Then copy the value from the result.

Here's an example on IntelliJ IDEA:

example

Solution 2:[2]

My best workaround to save an object state is to use the Evaluate tool when I have the object in the desired state and using Gson library convert it to a JSON, then at the test setup I copy the JSON as a String and convert it to a Java object again. May be a bit crude but for really large and complex objects it can save you a lot of time.

Solution 3:[3]

In IntelliJ 2016, you have an option to "View Text" when you right click on a variable in "Variables" window while debugging

Solution 4:[4]

If you are OK with saving each and every object (you cannot select which objects are saved) then there is a plugin for this: https://plugins.jetbrains.com/plugin/14974-breakpoint-exporter-importer-with-variable-information

Solution 5:[5]

Add below sample code and it will show the data in json form:

ObjectMapper mapper = new ObjectMapper();
  try {
    String sample = mapper.writeValueAsString(Your object);
    System.out.println(sample);
  } catch (JsonProcessingException e) {
    e.printStackTrace();
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 cb4
Solution 3 Anton Skovorodko
Solution 4 Csa77
Solution 5