Serialization question

So I figured out the hard way that Messages containing custom object fields can’t be sent through a connection. I have a GameState class for a turn-based strategy game that keeps track of various things, and it would be great if I could just send it through as one message. When I try, I get RuntimeException: Error serializing message.

What would I have to do to make this GameState object serializable? I tried looking through the jme3 serialization packages, but to be honest, it just looks like a maze. A little direction would be greatly appreciated.

Do you implement Serializable (jme3 one) on all your custom classes?

BTW, if we are at this topic, any particular reason why jme3 came up with it’s own serialization scheme instead of using Hessian?

That’s what I tried first, but I have no idea what to do with the inherited stuff that comes with it
[java] public Class serializer()
{
throw new UnsupportedOperationException(“Not supported yet.”);
}

public short id()
{
    throw new UnsupportedOperationException("Not supported yet.");
}

public Class annotationType()
{
    throw new UnsupportedOperationException("Not supported yet.");
}[/java]

I think it is annotation, so you would do it like

@Serializable
public class YourGameState {

rather than directly extending it.

SpiderMonkey supports serialization of simple types and collections.
Any class that is more complex needs to be annotated as serializable as abies said. But also, all it’s fields of complex types need to be annotated as serializable in their class file.

For a class Car with a name (String) an age (int) and an Engine (complex type), you need to annotate the Car and Engine classes.
Also, you need to register both (see monkeyZone NetworkUtil for an example). Use one class to register serializables and use that same class from the server and client, because the registering order needs to be the same for the server and the client.

NB: final and static fields are not serialized. I think you also need an empty, public constructor for each serializable class.

Works fine for me. I was very happy with SpiderMonkey.

@abies said: BTW, if we are at this topic, any particular reason why jme3 came up with it's own serialization scheme instead of using Hessian?

Just from a quick read-through, I think SM’s serializer is more compact in some cases… and it could be better. Someday I will fix it to be more flexible and even more compact using bit streams.

SM assumes that both ends are tightly coupled as far as format and so forgoes the sending of class file information and so on. It assumes both ends are speaking the same language.

Wow that’s really simple! I thought I would have to write a serializer myself for each class :stuck_out_tongue:

Thanks a bunch, I’ll try it out.

Hello, I found this topic by searching for an Example for serialization.

I searched for monkeyzone and Network util, but I believe they don’t exist anymore.
Can anyone give me a short example how to serialize or register an Object containing a string and an int? I searched a whole hour now and didn’t find anything that makes the field clearer…

My Serializer is using only the pre formatted Methods just now, but I don’t know what to write there…:

public class registerSerializer extends Serializer {
    public registerSerializer(){
       Serializer.registerClass(Message_Player.class);
    }

    @Override
    public void writeObject(ByteBuffer buffer, Object object) throws IOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Why do you want to create your own Serializer? Why isn’t the standard built-in support good enough? We can’t see your class so it’s hard to say.

There are plenty of examples of just registering an object that uses the standard built-in serializers. In all my games, not once have I had to write a custom one.

So if i want to register an object containing a string and an int i just have to write

serializer.register(objectClass.class) and everythings fine?

Of course.

Also, some things are faster to test than to open the forum and post about them. :wink:

1 Like