Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Handling deserialization of enums that no longer exist  (Read 694 times)
0 Members and 1 Guest are viewing this topic.
Offline SkyAphid
« Posted 2018-08-29 18:44:08 »

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:
1  
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.

it just werks
Offline KaiHH

JGO Kernel


Medals: 594



« Reply #1 - Posted 2018-08-29 18:50:21 »

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
Offline SkyAphid
« Reply #2 - Posted 2018-08-29 19:05:10 »

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.

it just werks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2018-08-29 19:08:40 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline KaiHH

JGO Kernel


Medals: 594



« Reply #4 - Posted 2018-08-29 19:11:33 »

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.
Offline SkyAphid
« Reply #5 - Posted 2018-08-29 19:15:36 »

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.

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.

it just werks
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2018-08-29 19:30:36 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline SkyAphid
« Reply #7 - Posted 2018-08-29 19:36:53 »

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.

it just werks
Offline nsigma
« Reply #8 - Posted 2018-08-29 19:50:35 »

Long story short: move away from Serializable ASAP. It's a backwards-compatibility trap.

And deprecated (or about to be)

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline SkyAphid
« Reply #9 - Posted 2018-08-29 20:22:03 »

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

it just werks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2018-08-29 20:41:53 »

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 Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline KaiHH

JGO Kernel


Medals: 594



« Reply #11 - Posted 2018-08-29 20:46:56 »

Jackson has extensive support for bidirectional/circular references: https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
But it requires annotations.
Offline CommanderKeith
« Reply #12 - Posted 2018-08-29 21:24:03 »

Could be worth checking out Kryo:
https://github.com/EsotericSoftware/kryo/blob/master/README.md
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.

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1303 views)
2018-06-10 19:43:48

EgonOlsen (1161 views)
2018-06-10 19:43:44

EgonOlsen (900 views)
2018-06-10 19:43:20

DesertCoockie (1301 views)
2018-05-13 18:23:11

nelsongames (1106 views)
2018-04-24 18:15:36

nelsongames (1337 views)
2018-04-24 18:14:32

ivj94 (2079 views)
2018-03-24 14:47:39

ivj94 (1231 views)
2018-03-24 14:46:31

ivj94 (2212 views)
2018-03-24 14:43:53

Solater (798 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!