Junior
What is deserialization?
sobes.tech AI
Answer from AI
Deserialization is the process of converting a byte sequence (obtained, for example, from a file, network, or database) back into a data object that can be used in a program. It is the reverse operation of serialization.
Examples:
- Reading JSON from a file and converting it into a Python dictionary or list.
- Receiving data from an HTTP response and converting it into Python objects.
- Loading a saved object state from bytes.
In Python, standard libraries such as json for working with JSON or pickle for serializing/deserializing Python objects are often used for deserialization.
import json
json_string = '{"name": "Alice", "age": 30}'
# Deserialization: converting a JSON string into a Python dictionary
python_dict = json.loads(json_string)
print(python_dict)
import pickle
serialized_data = b'\x80\x04\x95\x0b\x00\x00\x00\x00\x00\x00\x00}\x94(X\x04\x00\x00\x00name\x94X\x05\x00\x00\x00Alice\x94X\x03\x00\x00\x00age\x94K\x1eub.'
# Deserialization: converting bytes into a Python object (dictionary)
python_object = pickle.loads(serialized_data)
print(python_object)