Marker Interface: To implement serialization in Java or making an object serializable, you need to implement Serializable interface. Serializable is a marker interface, which means it is an interface without any fields and methods, for implementing some special behaviour. There are also other marker interfaces available in Java. Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. The byte stream created is platform independent.
Active2 months ago
I am able to serialize an object into a file and then restore it again as is shown in the next code snippet. I would like to serialize the object into a string and store into a database instead. Can anyone help me?
casperOne66.4k1414 gold badges161161 silver badges228228 bronze badges
Sergio del AmoSergio del Amo32.8k6363 gold badges142142 silver badges175175 bronze badges
13 Answers
Sergio:
You should use BLOB. It is pretty straighforward with JDBC.
The problem with the second code you posted is the encoding. You should additionally encode the bytes to make sure none of them fails.
If you still want to write it down into a String you can encode the bytes using java.util.Base64.
Still you should use CLOB as data type because you don't know how long the serialized data is going to be.
Here is a sample of how to use it.
Output:
NOTE: for Java 7 and earlier you can see the original answer here
Community♦
OscarRyzOscarRyz148k9999 gold badges350350 silver badges525525 bronze badges
How about writing the data to a ByteArrayOutputStream instead of a FileOutputStream?
Draw a custom stickman and watch him come to life in this interactive stickman adventure. Choose your Adventure. Designed by Hitcents.com. Draw a custom stickman and watch him come to life in this interactive stickman adventure. Draw a stickman game epic.
Otherwise, you could serialize the object using XMLEncoder, persist the XML, then deserialize via XMLDecoder.
Outlaw ProgrammerOutlaw Programmer8,38177 gold badges3939 silver badges6060 bronze badges
Thanks for great and quick replies. I will gives some up votes inmediately to acknowledge your help. I have coded the best solution in my opinion based on your answers.
Note i did not considered using JSON because is less efficient.
Note: I will considered your advice about not storing serialized object as strings in the database but byte[] instead.
Sergio del AmoSergio del Amo32.8k6363 gold badges142142 silver badges175175 bronze badges
XStream provides a simple utility for serializing/deserializing to/from XML, and it's very quick. Storing XML CLOBs rather than binary BLOBS is going to be less fragile, not to mention more readable.
skaffmanskaffman354k9191 gold badges748748 silver badges734734 bronze badges
KristianKristian3,63366 gold badges2424 silver badges2828 bronze badges
If you're storing an object as binary data in the database, then you really should use a
BLOB
datatype. The database is able to store it more efficiently, and you don't have to worry about encodings and the like. JDBC provides methods for creating and retrieving blobs in terms of streams. Use Java 6 if you can, it made some additions to the JDBC API that make dealing with blobs a whole lot easier.If you absolutely need to store the data as a String, I would recommend XStream for XML-based storage (much easier than
Daniel SpiewakDaniel SpiewakXMLEncoder
), but alternative object representations might be just as useful (e.g. JSON). Your approach depends on why you actually need to store the object in this way.48k1010 gold badges9999 silver badges119119 bronze badges
Java8 approach, converting Object from/to String, inspired by answer from OscarRyz. Ps3 save game files download. For de-/encoding, java.util.Base64 is required and used.
Markus SchulteMarkus Schulte1,70422 gold badges2828 silver badges4747 bronze badges
Take a look at the java.sql.PreparedStatement class, specifically the function
Then take a look at the java.sql.ResultSet class, specifically the function
Keep in mind that if you are serializing an object into a database, and then you change the object in your code in a new version, the deserialization process can easily fail because your object's signature changed. I once made this mistake with storing a custom Preferences serialized and then making a change to the Preferences definition. Suddenly I couldn't read any of the previously serialized information.
You might be better off writing clunky per property columns in a table and composing and decomposing the object in this manner instead, to avoid this issue with object versions and deserialization. Or writing the properties into a hashmap of some sort, like a java.util.Properties object, and then serializing the properties object which is extremely unlikely to change.
JoshJosh12.2k66 gold badges4040 silver badges5656 bronze badges
The serialised stream is just a sequence of bytes (octets). So the question is how to convert a sequence of bytes to a String, and back again. Further it needs to use a limited set of character codes if it is going to be stored in a database.
The obvious solution to the problem is to change the field to a binary LOB. If you want to stick with a characer LOB, then you'll need to encode in some scheme such as base64, hex or uu.
Tom Hawtin - tacklineTom Hawtin - tackline131k2929 gold badges190190 silver badges277277 bronze badges
You can use the build in classes sun.misc.Base64Decoder and sun.misc.Base64Encoder to convert the binary data of the serialize to a string. You das not need additional classes because it are build in.
Saboteur
How To Serialize Java Object
CiNNCiNN7,37266 gold badges3737 silver badges5252 bronze badges
priyanka_raopriyanka_rao
KristianKristian
Java How To Serialize An Object
3,63366 gold badges2424 silver badges2828 bronze badges