0

I've came across a problem that I have 2 objects as the fields of each other. I'll give a simplistic example:

public class Dog{
boolean tired;
Walk walk;

public boolean setTired(boolean tired){
    this.tired = tired;
}
}

public class Walk implements Runnable{
int seconds;
Dog dog;

public void run(){
  //After conditions are met
  dog.setTired(true);
}

public int getSeconds(){
   return seconds;
}

}

I'm saving Dog instances to a HashSet and by that I can get the Walk instances.

Walk is essentialy a class which helps Dog but it's in a different class because i need it to return stuff so I don't want it to be an anonymous Runnable.

Here comes the problem - I want to serialize the Dog HashSet and save it into a file, but I can't do that, I get these long repeating errors until I get a StackOverFlow error.

I searched it online and saw that because I have Walk as a field in Dog and Dog as a field in Walk, it goes into an infinite loop from what I gathered.

I thought about looping through the HashSet inside the Runnable and check if the Dog's Walk field is equal to this Walk class, but, I have a condition which would need to run the loop every time the Runnable executes, so I'm guessing it would reduce the performance a lot for such a simple task.

Does anyone have an alternative solution or has an advice on what I should do in order to serialize the class?

  • It looks like you don't use java serialization, but some other library. It will help if you tell us which library you use. – talex Sep 24 '20 at 09:19
  • Oh sorry, didn't know it was important. I use Gson which is a library that serializes objects to Json files. – JasonRl Sep 24 '20 at 09:23
  • Normally just saying "serialization" means the built-in Java serialization, which has specific capabilities for handling cyclic object graphs. Gson and Jackson don't support that by default; you'll have to do something like tell your serializer to use an ID in place of an inline object somewhere. – chrylis -cautiouslyoptimistic- Sep 24 '20 at 09:35
  • 1
    Does this answer your question? [gson.toJson() throws StackOverflowError](https://stackoverflow.com/questions/10209959/gson-tojson-throws-stackoverflowerror) – talex Sep 24 '20 at 09:59
  • @talex , not really, I've re-coded a bit of the stuff to try my method of looping through the set in order to find the right "Walk", so that means there are no overlapping fields BUT - I still get a StackOverFlow Error when I try to serialize, Both of the classes have 1 identical field, but the field is not the classes so it shouldn't cause a StackOverFlow. I searched more and couldn't find anything that fits my problem 1:1. – JasonRl Sep 24 '20 at 11:05
  • What do you expect as result? – talex Sep 25 '20 at 06:08
  • @talex For it to be serialized into a json, I actually have no idea what's causing it, this is what my code looks like if I'd edit the example: https://pastebin.com/dyWZgUgJ Essentially, I have a connector variable between the 2 (in this case id), so I'm just checking if the IDs are the same instead of the whole Walk classes, there's nothing that should cause a StackOverFlow Error here, or any error from what I can see. – JasonRl Sep 25 '20 at 06:46
  • Can you add example of json you want to generate to your question? – talex Sep 25 '20 at 06:47
  • @talex I actually don't care how the Json would look like, it could be just gibberish but as long as I can save it into a file and deserialize it and get the object back, I'm fine with it, I just know that Json is a good file for storing stuff so that's the first thing I looked into, and then I found Gson. – JasonRl Sep 25 '20 at 08:04

1 Answers1

0

Mark Dog dog as transient. Restore it manually after deserialization.

talex
  • 17,973
  • 3
  • 29
  • 66
  • This is not the solution unfortunately... as I said, I made Dog not be a field anymore and it still causes errors with Gson. – JasonRl Sep 26 '20 at 14:21
  • I just tried and it works for me. Can you post minimal complete example of this errors? – talex Sep 26 '20 at 17:47
  • ` java.lang.StackOverflowError: null at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383) ` Basically this. The 2nd line repeats lots of times with different code line number. I'm not using Dog as a field anymore as i said, I removed it and now using something else to link between the two so this shouldn't even happen. – JasonRl Sep 26 '20 at 20:21