Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. Best procedure to de-serialize a Java generic object?

What is the best procedure for storing and retrieving, using native Java serialization, generic objects like ArrayList<String>?

Edit: To clarify. When I serialize an object of type ArrayList<String> I'd like to de-serialize to the same type of object. However, I know of no way to cast back to this generic object without causing warnings.

like image 255
Jake Avatar asked Aug 24 '26 23:08

Jake


1 Answers

I think I understand the question, though it could be stated less vaguely. When you deserialize, you could cast to raw ArrayList and as long as that doesn't throw a ClassCastException you know you got that part right; but if you further cast to ArrayList<String> (or List<String>), you'll get a warning that this is not safe. The collection might contain Integers and this cast alone won't catch that.

The usual best course of action is just to match your serialization and deserialization code to each other carefully, and suppress the warnings that result.

like image 69
Kevin Bourrillion Avatar answered Aug 26 '26 14:08

Kevin Bourrillion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!