I am implementing a client server protocol using UDP sockets for a game in java. I am sending user input from the client to the server, where the server processes the input and updates a Level object containing players and other game objects. I am serializing the Level Object(which also contains transient properties to keep the size down) and sending it to the client. I want to merge the Level object received from the server with the one on the client since the one received from the server will have null values for its transient properties. Is there a good way to do this other than manually reconstructing a complete and up to date level object? Thanks!
Asked
Active
Viewed 65 times
1 Answers
1
Assuming your objects are "standard" java beans with getters and setters for their properties, Spring has a couple of utility methods in its BeanUtils class that could help. Both methods are named copyProperties
, and both are for copying properties from one object to another. One of the methods lets you specify a whitelist of properties and the other lets you specify a blacklist. With these methods, you could define an interface or a list of properties that either should or shouldn't be copied onto the Level received from the server and then have the copying done for you--something like:
interface LevelPropertiesToMerge {
void setPropertyOne(String something);
void setPropertyTwo(int somethingElse);
}
// serialization code on client
Level fromServer = ...;
Level fromClient = ...;
BeanUtils.copyProperties(fromClient, fromServer, LevelPropertiesToMerge.class);
or:
// serialization code on client
Level fromServer = ...;
Level fromClient = ...;
BeanUtils.copyProperties(fromClient, fromServer,
new String[] {"propertyOneToIgnore", "propertyTwoToIgnore"});

Ryan Stewart
- 126,015
- 21
- 180
- 199