187. Object Serialization with pickle and json

1. Serialize and Deserialize with pickle

import pickle

data = {'name': 'Alice', 'age': 25, 'city': 'Wonderland'}
# Serialize
with open('data.pkl', 'wb') as f:
    pickle.dump(data, f)

# Deserialize
with open('data.pkl', 'rb') as f:
    loaded_data = pickle.load(f)

print(loaded_data)

2. Serialize Multiple Objects with pickle

import pickle

data1 = [1, 2, 3]
data2 = {'a': 10, 'b': 20}

with open('multi_data.pkl', 'wb') as f:
    pickle.dump(data1, f)
    pickle.dump(data2, f)

with open('multi_data.pkl', 'rb') as f:
    loaded_data1 = pickle.load(f)
    loaded_data2 = pickle.load(f)

print(loaded_data1, loaded_data2)

3. Serialize Custom Objects with pickle


4. Serialize and Deserialize with json


5. Serialize Python Objects to JSON String


6. Serialize Custom Objects with json Using default Argument


7. Deserialize Custom JSON with object_hook


8. Use pickle for Serializing Large Data


9. Handling Encoding and Decoding Issues in json


10. Serialize and Deserialize Nested Data Structures


Last updated