Serialization – JSON and Binary
Earlier in this chapter, we saw how to write binary data to a stream. We can call all the write
methods to write all sorts of types to a file. However, that can be pretty hard and also quite error-prone. You have to keep track of the format of the data. One simple mistake will make your files unreadable.
A better way would be to serialize your data in a format that the streams can understand. There are two ways to do that: JSON and Binary.
JSON is simple: most programming languages and platforms understand it. JSON has become the de facto standard for displaying a structure in text. In most places, JSON has replaced XML. JSON is smaller and more lightweight.
However, it can be even more lightweight. You can also serialize your data as a binary stream. That requires more coding but usually results in much smaller files and data streams. Again, that might be precisely what we, as system programmers, are looking for.