Handling deserialization of enums that no longer exist

Hey guys. I asked this question on StackOverflow and haven’t had much luck on there and thought I’d try here.

I have a file I’m trying to de-serialize that’s giving this error:

java.io.InvalidObjectException: enum constant ENUM_NAME does not exist in class

Normally for most classes you can just use readObject() and catch/fix the error before it happens. I.E. initialize a variable that didn’t exist in the old file or etc. How do you handle this with enums? I’d like to just basically return a null enum or check what the non-existent enum was and replace it with the correct one. I read online that java doesn’t handle enums like other classes when it comes to deserialization, so readObject() won’t work here, right?

Thanks in advance.

Always research whether the same question has been asked before, which in your case was 100% likely: https://stackoverflow.com/questions/8743668/handling-deserialization-of-enum-values-that-no-longer-exist

I did find that in my initial search. That post ends with him saying all he could do was make a hacky solution because there wasn’t a good one at the time, hence me asking it again on here to see if there was a better solution now. Additionally, I figured since it had been asked six years ago (around the Java6/7 era iirc) there may be better workarounds in newer java versions. So the post isn’t really that current or helpful.

If you’re in a bind… recreate the old classes, load them in with a seperate classloader, deserialize the bytes, and provide the result in some way to your application (it can inspect the object-tree with reflection, creating a new object-tree).

Long story short: move away from Serializable ASAP. It’s a backwards-compatibility trap. Use JSON, XML, or whatever floats your boat - something human readable.

The current/recent version was Java 8 when he wrote the answer in 2014.
Usually, when something changes that is relevant to an already asked question, people post follow-up answers or edit an earlier answer to reflect that.

Yeah, we’re looking into at the moment. The problem is that stuff like JSON where you manually handle your data structures can turn into pure misery as the data structure gets larger. Simple files like the ones used in JDialogue aren’t too bad, but when you have a world full of NPCs with individual personalities and etc, you really are enticed by the simplicity of serialization. That said, your solution could work for the short term since we need to get an old file working pretty quick. Thanks for the feedback. If I find a good solution I’ll post it here.

EDIT:
Apparently using Externalizable instead of Serializable can solve the problem without manually reading the bytecode. I’m gonna try it out.

We have libraries for json/xml handling: jaxb, jackson, gson - it serializes back and forth to/from java objects just as easily as I/O with Serializables.

I hadn’t worked with those other two, but I tried GSON awhile back and it’s easy to get circular reference errors with it. I tried using it for JDialogue originally for the dialogue files and without special configuration, it’s easy to break it. Arguably that’s worse than just manually handling it since you’ll have to learn the ins and outs of someone else’s systems (which in itself isn’t bad, but can be when the documentation isn’t complete for every potential situation). The other two may be better about that though. I’ll certainly look into them.

And deprecated (or about to be)

I think we’re just going to make a basic custom serializer.

If you have circular-references, then it’s indeed a poor fit for tree-based datastructures like json and xml.

You can create ‘tables’ with ‘foreign keys’, and GSON could help you there, but… you’ll have to do the heavy lifting yourself.

However, whatever you write as a custom format (if you go that route), make it text-based (and gzip it, if needed), none of that binary rubbish :slight_smile:

Jackson has extensive support for bidirectional/circular references: https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
But it requires annotations.

Could be worth checking out Kryo:


Our very own Nate is the author. It’s a well used and tested serialisation method for games. I haven’t used it myself, but it looks highly customisable and may suit your use case.