Will a list of superclass objects serialize the actual subclass type?

Discussion in 'Plugin Development' started by Dragonphase, Oct 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Dragonphase

    I have a list of custom objects which are declared as shown:

    Code:java
    1. private List<Effect> effects;


    Effect does not implement ConfigurationSerializable for specific reasons, but it's derived subclasses do. If I want to serialize the object that the list of effects is inside, will it also serialize the list of effects using the derived subclass' serialize method? Here is how I want to serialize my list of effects:

    Code:java
    1. @Override
    2. public Map<String, Object> serialize() {
    3. Map<String, Object> results = new HashMap<String, Object>();
    4.  
    5. results.put("effects", effects);
    6. return results;
    7. }


    And this is a subclass derived from Effect:

    Code:java
    1. public class SomeEffect extends Effect implements ConfigurationSerializable{
    2.  
    3. @Override
    4. public Map<String, Object> serialize() {
    5. Map<String, Object> results = new HashMap<String, Object>();
    6.  
    7. results.put("...", ...);
    8. return results;
    9. }
    10.  
    11. public SomeEffect deserialize(Map<String, Object> args){
    12. try{
    13. return new SomeEffect(...);
    14. }catch (Exception ex){
    15. throw new NullPointerException(ex.getLocalizedMessage());
    16. }
    17. }
    18. }


    Would I be able to do this, or would I need to change the data type declaration of effects to:

    Code:java
    1. private List<? extends Effect> effects;


    Will the serialize method correctly serialize every type of Effect within List<Effect> and correctly deserialize this using (List<Effect>) args.get("effects") ?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    epicfacecreeper

    I'm not completely clear what you're asking. If I am correct, you want to serialize a list of Effects. Now I have a question for you. Do all of these effects implement ConfigurationSerializable? If so, I would make Effect abstract and make THAT implement ConfigurationSerializable. If, for some reason, you need a direct instance of Effect, have Effect implement ConfigurationSerializable and give it a default serialize() method, which the subclasses can override to make a better serialize.

    Also, without Effect having a serialize method, serializing an Effect object without a cast will result in an error (Compile time probably).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  3. Offline

    1Rogue

    If Effect does not implement Serializable/ConfigurationSerializable then no, it cannot be serialized and must be marked transient.
     
  4. Offline

    Dragonphase

    Sorry for the late reply.

    Effect doesn't implement ConfigurationSerializable but its subclasses do.
     
  5. Offline

    1Rogue

    It seems that Effect just implements "Serializable", so you could simply write it to another file via "ObjectOutputStream"
     
Thread Status:
Not open for further replies.

Share This Page