Serialization
Serialization is the process of translating a complex object into a format, typically a string of characters, that can be deserialized back into a copy of the original object. This is beneficial as it allows the software to save the state of an arbitrary object to a destination that can be completely unaware of its details. Any object can be saved to a simple string variable, database field, external file system, third-party application in the cloud, or even printed out on a sheet of paper.
Common formats for serialization include XML and JSON, which have the advantage of being human-readable. However, that is not a requirement, and in some cases, serialization uses other formats such as binary which aren’t human-readable. Regardless of the format, the purpose of serialization is that at a later point in time, the serialized data can be read or processed as input and be deserialized back into a copy of the original object.
When programming languages implement serialization, they often require special declarations in defined classes to make their associated objects serializable and often require special libraries to implement this functionality.
Example
Here is a simple example of serialization in Python:
import jsondef SerializeDict(input_dict):print("Serializing input to JSON")output_str = json.dumps(input_dict)print(output_str)test_dict = {"make": "Chevy","model": "Silverado","msrp" : 15000,"options": ["4-wheel-drive","Towing Package","Heated Seats"],"year": 2019}SerializeDict(test_dict)
The output looks like this:
Serializing input to JSON{"make": "Chevy", "model": "Silverado", "msrp": 15000, "options": ["4-wheel-drive", "Towing Package", "Heated Seats"], "year": 2019}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn more on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly16 hours