When your program closes, everything in memory disappears. That's fine for a quick calculator β but if you built a game with saved levels or a diary app, you'd lose everything.
Persistence means saving your objects to a file so they survive after the program ends. Next time you run the app, you load the objects back into memory and pick up right where you left off.
Your language gives you tools to open, read, and write files. But you can't just dump a raw object into a file β the computer needs a format it understands, like text or bytes.
So you convert your object to text (writing) and back to an object (reading). That's the whole game: convert β save; load β convert back. Simple, but powerful.
Serialization means converting an object into a format that can be stored or sent β like JSON, XML, or binary bytes. Deserialization reverses it: reading the format and rebuilding the object.
Most modern apps use JSON because it's readable by humans and machines. Common libraries (like json in Python or Jackson in Java) handle the conversion automatically.
Two formats handle almost every persistence need:
| Feature | JSON | CSV |
|---|---|---|
| Best for | Nested objects, complex structures | Flat lists of records (like a table) |
| Readable? | Yes, with key-value pairs | Yes, as rows and columns |
| Handles nesting? | Yes β great for object graphs | No β best for flat data |
| File extension | .json | .csv |
Real apps have objects that reference other objects: a School contains Classrooms, which contain Students, which may reference a Parent. That's an object graph.
Saving it isn't just dumping one object β you're capturing the whole connected network. JSON handles this beautifully with nested objects and arrays. Watch out for cycles (A refers to B, B refers to A) β they can make naive serializers loop forever.
Instead of scattering fileβloading code everywhere, put it all in one class called a repository. A StudentRepository knows how to save and load students. Everyone else just calls repo.save(student) or repo.findAll().
This has two big wins: if you later switch from a file to a database, only the repository changes. And it's easy to swap in a fake repository for testing.
Files can break. They get truncated midβwrite, edited by hand, or saved in an old format. Your program should handle this gracefully, not crash.
Techniques: wrap file reads in try/catch, keep a backup file, validate the contents, and add a version number at the top of the file. If a file's version doesn't match what your program expects, refuse to load it (or migrate it).
First save: Create a Student class with name and grade. Save one student to a JSON file, then read it back. Print the loaded student's details to prove the data survived.
CSV roster: Create a list of five students. Save them to a CSV file, then load them back into a list. Confirm all five are present and identical.
Nested world: Build a Classroom class holding a list of Student objects. Serialize the whole classroom to JSON, then deserialize it. Check that the students inside survived.
Repository refactor: Move your save/load code into a StudentRepository class with save() and findAll() methods. Show that the rest of your code no longer touches files directly.
Version guard: Add a "version": 1 field at the top of your JSON file. Write a loader that refuses to load anything with an unknown version. Try loading a manually edited versionβ2 file β confirm it refuses.
Corruption drill: Manually truncate your JSON file (delete half the text). Try to load it. Wrap the load in try/catch and print a friendly error instead of crashing.
By the end of this module, you'll be able to: serialize objects to JSON and CSV, save and load nested object graphs, use the repository pattern to keep file code in one place, and handle corrupted or incompatible files without crashing.