Junior — Middle
What is the purpose of the data serialization mechanism in programming?
sobes.tech AI
Answer from AI
Serialization is the process of converting a data structure or object into a sequence of bytes or a string for storage or transmission.
Why serialization is needed:
- Saving the state of an object to a file or database.
- Transmitting data over a network between applications.
- Caching objects.
In Python, the pickle module or JSON formats are often used for serialization, especially when data needs to be human-readable and compatible with other languages.
Example of serialization in JSON:
import json
data = {'name': 'Ivan', 'age': 30}
serialized = json.dumps(data) # converts to JSON string
print(serialized)
# For restoring
restored = json.loads(serialized)
print(restored['name'])