0

I am using Gson to serialize my objects, but I ran into an issue which is that you can't read abstract objects after I have successfully saved them. I found this which pertains to my question, but it differs in a few keyways. Mainly, I am saving an object that contains a map where the maps value holds a reference to an abstract class. If this issue did not exist my code would look very clean here is a minimized version of how everything looks right now.

private void recoverData() {
        final File file = new File(this.getDataFolder(), "data.json");

        if(file.exists() && file.length() > 2) {
            try {
                final Gson gson = new GsonBuilder().setPrettyPrinting().create();
                final Type type = new TypeToken<Map<String, DataObj>>() {}.getType();
                DataObj.DATA = gson.fromJson(new FileReader(file), type);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
public class DataObj implements Comparable<DataObj> {
   public static transient Map<String, DataObj> DATA = new HashMap<>();

   // This also contains 20+ more fields (but those all serialize fine)
   public final Map<Upgrades, PlayerUpgrades> upgradesMap = new HashMap<>();
} 
public abstract class PlayerUpgrades implements Serializable {
    public int level;

    public abstract String upgradeName();
    public abstract Integer maxLevel();
}

I then have many "PlayerUpgrades" that extends PlayerUpgrades but I am not sure how to correctly get my upgradesMap to serialize. Does there happen to be a way I can denote something inside of PlayerUpgrades so I can manually create new instances of each class depending on the key from the upgradesMap? I bet I could try and manually serialize everything in my DataObj but since I have tons of fields, I feel like that would take a while and look clunky so how should I approach this?

  • This: _where the maps value holds a reference to an abstract class._ is literally impossible. A 'reference' points at an object, and objects cannot possibly be instances of abstract classes. – rzwitserloot Apr 26 '23 at 03:58
  • @tgdavies No because that again assumes I am not doing anything but saving objects that extend an abstract class. I need a solution that works in the form I specified above. Since the key difference is that the objects that extend an abstract class in my case are nested inside objects which are then all inside a map. If you could provide some example or solution to how I can accomplish this in my more complex version I would greatly appreciate it! – IgnoreExeption Apr 26 '23 at 20:47
  • @rzwitserloot Semantics it holds a reference to the memory address of the exact object I created which is not an abstract class. But it extends the abstract class and all objects that extend an abstract class are an ``instanceof`` of that abstract class. – IgnoreExeption Apr 26 '23 at 20:56
  • @IgnoreException you serialize objects, not classes. An object cannot be an instance of an abstract class. The correct question is: "How do I serialize objects - specifically in regards to the notion that this object is an instance of a class that doesn't extend `Object` but something else". The `abstract` part of this is completely irrelevant. – rzwitserloot Apr 26 '23 at 23:18
  • @rzwitserloot Its laughable you are arguing about "relevance" when none of what you are saying has any relevance to my question. Obviously, you serialize objects, but a class is just the template for the object. Stop going around to random questions and spamming a bunch of goofy comments. Either engage with question or go somewhere else. Also, the whole point of this question is to ask about serializing objects created from sub classes when the super class is abstract. So now "How do I serialize objects" is not the correct question. I am also not going to write a novel to convey my title... – IgnoreExeption Apr 27 '23 at 00:11

0 Answers0