How to serialize correctly

Greetings monkeys.



I’m running into a few general issues about SpiderMonkey serialization. I’ve used some other Java networking APIs that are similar so my knowledge of one may be poisoning the other. :3



For example, I have a class that holds a Map of Strings. I’m using an EnumMap in the class to make it nice and easy to follow but when it comes to sending a copy of this class over the network I don’t need to send those Enums, just the strings. The order they’re in will be enough to know how to re-create the class. ( I couldn’t seem to send EnumMaps either, but that’s another issue and not really the root of my issues. )



So in Kryonet, I would just use the serialization interface to create the bytearray in the right order and read it out in the right order. I can’t see how to do the same kind of thing using the @Serializable annotation so I’m a bit stumped. The only way I can see to achieve this kind of thing is to convert my class into a message object that contains the data I need and send that. But this is an extra layer in the way. My biggest problem with coding in general is getting lost in my own messy code so in an attempt to keep things organized, clean and readable, I’d rather avoid this seemingly hackish approach.



So what’s the right way? And did I miss any documentation? The only thing I could find on Serialization was a tiny paragraph in the main SpiderMonkey tutorial page as well as old, depreciated pages that I assume are no longer relevant at all.



Thanks.

You can register your own Serializers to do sending/receiving however you want for specific classes.



The networking side only lets you send Messages because they have additional routing information. So everything you send through networking must be wrapped in a Message. This is not a problem with the serializer directly as it supports any object.



The enum class has to exist on both ends of the connection so there is no reason to send anything but the int value… which is what the default serializer does when sending enums.

Right, it makes more sense if I can make my own serializer but I have no idea how to go about this. The only thing I can find is this depreciated document.



Would you mind pointing me at the right document/classes so I can do this?



Thanks.

@ctwildgoose said:
Right, it makes more sense if I can make my own serializer but I have no idea how to go about this. The only thing I can find is this depreciated document.

Would you mind pointing me at the right document/classes so I can do this?

Thanks.


Probably best to just look at some of the existing serializers. Like the current enum serializer which I suspect already does what you want anyway.

Are these real Java enums? If so, your desired approach is the least efficient way of sending an enum that I can easily think of.

If not then take a look at some of the other serializers.

My desired approach is to not send any enums at all, to actually control what gets serialized in my class. After finding the included serializers in the com.jme3.network.serializing.serializers package, I think I can do what I’m trying to do. Thanks pspeed.

@ctwildgoose said:
My desired approach is to not send any enums at all, to actually control what gets serialized in my class. After finding the included serializers in the com.jme3.network.serializing.serializers package, I think I can do what I'm trying to do. Thanks pspeed.


Ok, I misunderstood your original post then. It sounded like your were sending strings or something. The current enum serialization only takes 6-8 bytes total, I think (including preamble).