72537

GWT - Serialize as superclass

Question:

I am trying to send a Game object from the server to the client in GWT. This Game object simply contains a list of Player:

public class Game implements Serializable { private List<Player> players; }

A Player is a simple Pojo that contains the player's state, like the name, remaining life, etc.

public class Player implements Serializable { private String name; private int numberOfGamesPlayed; private double hp, initialHp; }

However, the game can also contain a CpuPlayer, that is a Player with additional Artificial Intelligence logic

public class CpuPlayer extends Player { private PlayerIntelligence intelligence; // Not serializable }

Both the Game and Player objects are in my "shared" package, and should be used on server and client side.

However, the CpuPlayer contains many internal processing data, and is purely server-side. It is therefore not included in GWT serialization whitelist.

What I would like to do is to send the CpuPlayer as a Player to the client, i.e. mask all the subclass specificities during the serialization.

Is this approach sound? Is there a way to do that? I looked at custom serialization, but it didn't seem to work that way (I would need to serialize all the fields one by one).

Thanks for your help,<br /> Sébastien

Answer1:

The simplest solution I can think of would be to use a library like <a href="http://dozer.sourceforge.net/" rel="nofollow">Dozer</a> to copy everything you need from the CpuPlayer into the Player. This is as simple as calling

<pre class="lang-java prettyprint-override">Player player = new DozerBeanMapper().map(cpuPlayer, Player.class);

(the performance overhead for copying is probably negligible (sub-milliseconds)).

Another approach could be using RequestFactory instead of GWT-RPC, which is designed to transfer only a part of the server side object to the client (ideal for cases like yours, where you cannot send the entire internal data to the client).

Recommend

  • HP-UFT WPF TextBlock object capture
  • csrf-token POST 405 (Method Not Allowed) Laravel
  • Unit testing keypresses and terminal output in Python?
  • Rebol - HTTP response headers
  • Why dsofile.dll still need Office Installation?
  • use grep and awk to transfer data from .srt to .csv/xls
  • How to draw cross background via css which is responsive
  • HTMLcollection 0 list length, [n] returns undefined, converting to an array returns an empty array
  • Theme in user control
  • Is it expensive to create the Thread object or to actually start the thread?
  • How can I fill all space of a div 100% with 3 divs with the same size?
  • Python 3 Sorting a List of Tuples?
  • Different builds of turn-based Game Center game can’t see each other’s matches
  • opengl window freezing during move/resize
  • How to make R's read_csv2() recognise the text characters properly
  • Implementation of State Monad
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Passing 1 of 3 values to a Sub
  • How to test if a URL from an Eclipse bundle is a directory?
  • ActiveRecord query for a count of new users by day
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Use of this Javascript
  • Set the selected item in dropdownlist in MVC3
  • How to create a file in java without a extension
  • Linq Objects Group By & Sum
  • Spring Data JPA custom method causing PropertyReferenceException
  • Splitting given String into two variables - php
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal