Serialization is the process of converting a structured object, such as a class with its attributes, into an interoperable, textual format such as JSON or XML.
Its primary purpose is to make data readable and exchangeable between different systems and programming languages.
Binary files, such as images, PDFs, or other files, are already a sequence of bytes (0s and 1s) and cannot be serialized in the traditional sense, as they do not have a key-value pair structure. To send them via APIs, they are handled using two approaches:
-
The first is the multipart/form-data method, which encapsulates the file’s binary data directly in an HTTP request, sending it unaltered; it is more common and efficient for sending large files, as it does not increase their size.
-
The second approach is Base64 encoding, which converts a file’s binary data into a string of textual characters (ASCII). This method is useful for embedding small files directly within a JSON or XML payload, making the data compatible with text-oriented formats. Its main disadvantage is that it increases the original file size by about 33%.
Tip
Binary decoding is very strict: during deserialization you must make sure that the decoding is configured to ignore all whitespace and newline characters.