I have a complex set of data models that currently implement java.io.Serializable
, and I have successfully serialized and deserialized them with ObjectOutputStream
and ObjectInputStream
.
However, the result are binary files (as expected), and I was wondering if Java supports serialization and deserialization in the same manner to a non-binary format, such as XML.
I see that C# has this feature: XML vs Binary performance for Serialization/Deserialization.
Performance speed/ efficiency is not a consideration in this case.
Jackson annotations are useful in defining and controlling the process of serialization and deserialization across various formats such as XML, JSON, and YAML.
Google protocol buffers, popularly known as protobuf is an alternate and faster way to serialize Java objects. It's probably the best alternative to Java serialization and useful for both data storage and data transfer over the network.
To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.
The ObjectOutputStream class contains writeObject() method for serializing an Object. The ObjectInputStream class contains readObject() method for deserializing an object.
Further, I would suggest you to look at Simple and XStream frameworks. I found both good. You can choose to go with either one, or may be XmlEncoder as suggested by Jack.
Yes, Java has it and it's called XML Encoding. Check it out here!
The approach is quite similar to the normal serialization..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With