java - Serializing an object which contains a list of other objects -
background: implementing game needs serialize model objects (events) able send them on network clients playing game.
problem: 1 of model classes eventshowcards
, subclasses event
(declared serializable).
public class eventshowcards extends event { private final player player; private final list<itemcard> cards; public eventshowcards(player player, list<itemcard> cards) { super(); this.player = player; this.cards = cards; } public player getplayer() { return player; } public list<itemcard> getcards() { return cards; } @override public void accept(visitor visitor) { visitor.visit(this); } }
however, when serialize object , send client, client receives object , list of itemcard
s empty. no exceptions thrown.
event shown server (before serialization):
event shown client (after serialization):
in order serialize/deserialize using standard objectoutputstream/objectinputstream
anyone has idea of happening here? thanks.
edit
as request posting code of itemcard (and card):
itemcard.java
public abstract class itemcard extends card { /** * default card ca played independently game state, subclasses have more complicated rules * must override method. */ public boolean canbeplayed(gamestateturn gamestate) { return true; } protected void emiteventcardplayed(player cardplayer, gamestate gamestate) { notifyobservers(new eventcardplayed(cardplayer, this)); } }
card.java
public abstract class card extends concreteobservable<event> implements cardbehavior, serializable { protected deck<? extends card> deck; // deck card belongs public card() { super(); } public void setdeck(deck<? extends card> deck) { this.deck = deck; } public void discard() { deck.discard(this); } @override public void preeffect(player cardplayer, gamestate gamestate) { // default cards have no preeffects } @override public void posteffect(player cardplayer, gamestate gamestate) { // default after card played discarded discard(); } @override public string tostring() { string classname = this.getclass().getsimplename(); return cardnamefromclassname(classname); } private string cardnamefromclassname(string classname) { stringbuilder sb = new stringbuilder(); (character c: classname.tochararray()) { if (character.isuppercase(c)) { sb.append(" ").append(character.tolowercase(c)); } else { sb.append(c); } } return "\"" + sb.substring(1) + "\""; } }
edit 2
and deck.java
public class deck<t extends card> extends concreteobservable<event> implements observer<event>, serializable {...}
edit 3
the list of item cards used initialize eventshowcards created list model object of application, passed as-is. creating arraylist (copying object) somehow solved problem.
the list can contain unlimited number of object types.
have eventshowcards
serialized (through event
class), need force itemcard
class serializable in order marshalled , unmarshalled when needed correctly.
edit
java.util.list
doesn't implement serializable, why list not serialized.
use other collections serializable arraylist
Comments
Post a Comment