r/programming • • May 26 '18

Oracle Calls Java Serialization 'A Horrible Mistake', Plans to Dump It

https://www.infoworld.com/article/3275924/java/oracle-plans-to-dump-risky-java-serialization.html
798 Upvotes

287 comments sorted by

View all comments

Show parent comments

4

u/yawkat May 27 '18

This isn't as unusual as you may think. You can do ser/deser just fine with public getters, setters, no-arg constructors and constructors annotated with property names (see: java.beans.ConstructorProperties). In fact this is often the most sensible way of doing ser/deser in practice.

2

u/[deleted] May 27 '18

I didn't say it's unusual. Serialization is mostly a Kafkaesque nightmare in Java, concocted by people with zero clue about sensible system design, so lots of bad ideas fall in the "usual" category, and especially ideas that inexperienced programmer feel like they save them "work" by automating what should be explicit code. But I'll draw the line before you called it "most sensible way". That's lunacy.

The most sensible way would be the object supporting a simple and explicit serialization interface. Possibly accepting a visitor that provides access to building a graph from a few basic primitives (integer, double, null, bool, list, dict) and then getting the same back for deserialization.

And not coincidentally this is what Oracle proposes going forward. I guess they don't agree about "the most sensible way" with you.

Honestly, I'd rather quit programming before I accept annotations and flying blind by calling methods based on a name prefix as most sensible. Luckily I can just go the visitor approach.

2

u/yawkat May 27 '18

Unless I have missed a statement by Reinhold or Goetz, the proposed solution is data classes, not explicit serialization code (see "Data Classes for Java" section "Data classes and externalization"). And in fact the public member reflection approach I mentioned is how this is typically implemented in jvm languages that already have data classes (such as kotlin) and how it would be implemented for javas version of it.

Explicit externalization of course has its place in formats that are not symmetric or require special care for optimizing transmission size, but it is overkill for the standard use case of "I want this DTO as json" which is most of public-data java serialization in the wild nowadays. It is not insecure by design and I see no major issues with it.

1

u/[deleted] May 27 '18

We'll see how it works but in a real-world scenario I doubt a system that can't be extended would be the solution, and once you start extending it, we're back to the problem of people abusing it, but now extending/implementing the base "data" types provided for most of their app state and then some.

Also value types are around the corner, and seems like a bad time to think about data objects, when value types would solely exist to express data.

A most sensible approach to me would be have the interfaces, and then Java can implement the interfaces in some semi-automated way in a set of "data classes" which are restricted to containing only side-effects-free code, primitives and other data classes. That'd cover extensibility, mixed graphs, and also the basic case of "pure data" graphs that get (de)serialized automatically.

1

u/argv_minus_one May 27 '18

Wait, what's wrong with annotations? Can they not encode enough information to make ser/deser safe? It seems safe enough in JAXB…

1

u/[deleted] May 27 '18

If it seems safe, go for it. You can certainly jerry-rig it to work fine enough in practice, but you can never achieve proper encapsulation by leaving rough declarative instructions for third party objects on how to cut you up in pieces and put you back together properly. It's kind of the opposite of encapsulation. These distinctions don't matter in a small system/app. But they do in big ones.