简体   繁体   中英

Serialization vs toString()

Since I'm writing/reading from files, I was wondering if there's any difference or there's any best practice between directly sending objects or using their representation as strings on files which in my case I personally find it easier to handle.

So when should I serialize instead of writing/reading objects as String?

There's typically not enough information in the string representation of an object to be used to recreate it.

Java serialization "just works", but does not give you a human-readable representation, if that's what you are looking for.

Another alternative is to read / write JSON representations of your objects. There are several JSON serialization / federalization libraries for Java that are popular, including GSon and Jackson.

The answer is in the javadoc for Object.toString() .

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

Note that it says:

  • concise,
  • informative, and
  • easy for a person to read.

But it does NOT say:

  • complete,
  • unambiguous, or
  • easy for a computer to read.

Serialization is about producing a linear (not necessarily textual) form that can be read by a computer and used to reconstruct the state of the original object.

So a typical serialization is not particularly human readable (eg JSON, XML, YAML) or completely unreadable (eg Java Object Serialization, ASN.1). But the flip-side is that the information needed to reconstruct an object should all be present, in an unambiguous form.

(There is a lot more that could be said about various kinds serialization, their properties and their utility. However, it is beyond the scope of your question.)


Does this preclude toString() from being used for serializing data?

No, it doesn't.

But if you take that approach, you need to code your toString() methods carefully to make sure that what they produce is complete and unambiguous. Then you need to write a corresponding method to parse the toString() output and create an new object from it.


... or using their representation as strings on files which in my case I personally find it easier to handle.

I think that as you write larger and more complicated programs, you will get to the stage where that kind of code is tedious and time consuming to write, test and maintain.

Serialization allows you to convert the state of an object into a stream of bytes, which can then be saved to a file on the local disk, sent over the.network to any other machine, or saved to the Database. Deserialization allows you to reverse the process, which means to reconvert the serialized byte stream into an object again. It's important to know that numbers or other types aren't as easy to write to files or treats as Strings. However, their initial states are not guaranteed to be maintained without being serialized.

Thus, it is convenient to use Strings, for simpler situations, which is not necessarily important to have a serialization, such as a college project. However, it is not recommended that this process be done, as there are other better solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM