Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level Two Β· Lesson 6 of 8

Module Five

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
6
Module 5: Object Persistence – Files & Serialization for Young Coders
⭐ Module 5 · Level Two

Object Persistence:
Files & Serialization

πŸ’Ύ Make your objects remember things after the program closes

🎯 What you'll learn in this module

  • βœ” How to read and write files that hold objects
  • βœ” What serialization and deserialization mean
  • βœ” How to save objects as JSON and CSV
  • βœ” How to save and load entire object graphs
  • βœ” A first look at the Repository pattern
  • βœ” How to handle corrupted files and version mismatches
🧠
Why Do Objects Forget?
The Big Idea

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 memory is RAM (short‑term). A notebook is a file (long‑term). Persistence is writing things down so you don't forget.
πŸ“‚
Reading & Writing Files with Objects
Basics

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.

🧱 Object
β†’
πŸ“ Text / Bytes
β†’
πŸ’Ύ File
πŸ“¦ Sending a package: you put the object in a box (text), add a label, and ship it (file). On the other end, someone opens the box to get the object back.
πŸ”„
Serialization & Deserialization
Core Skill

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.

πŸ“Έ Serialization is "snapshotting" an object into a photo. Deserialization is rebuilding the object from that photo.
πŸ“Š
JSON & CSV: The Two Workhorses
Formats

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
🧩 JSON is a puzzle with pieces inside pieces. CSV is a simple spreadsheet β€” flat and clear.
πŸ•ΈοΈ
Saving Object Graphs
Advanced

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.

🌳 Like photographing a family tree: you capture each person and their connections to each other.
πŸ—„οΈ
Repository Pattern: One Door for Data
Design

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.

🏦 A bank teller is a repository for money: you don't break into the vault yourself. You go through the teller.
🚨
Handling Corruption & Version Mismatch
Robustness

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).

πŸ“Ό An old video game save: when the game updates, it warns you the save file is from an older version. Same idea β€” protect the player's progress.
⚠️ Common trap: Writing directly over the original file is dangerous β€” if the program crashes halfway through, you lose all your data. Instead, write to a temporary file first, then rename it over the original. Two seconds of extra work saves hours of regret.

πŸ› οΈ Hands-on activities (no boring stuff!)

1

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.

2

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.

3

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.

4

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.

5

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.

6

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.

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’