Exercise: Round-Trip Custom Type Through JSON
Bartosz Zaczyński RP Team on June 11, 2026
Hi @Kathy Rogers, that’s fair feedback, and you’re right about the setup. Lesson 178 introduces the Person class to show why a custom type isn’t serializable, and then the worked example and this exercise switch over to the complex type. So I get why it feels like a bait and switch.
Here’s the reassuring part: the complex number was only a stand-in. The encode-then-decode pattern you practiced is type-agnostic, so the exact same steps serialize a Person. The sundae was there the whole time :)
Here’s your Person round-trip using the same approach from the Encoding and Decoding lessons:
import json
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def encode_person(obj):
if isinstance(obj, Person):
return {"__person__": True, "name": obj.name, "age": obj.age}
type_name = obj.__class__.__name__
raise TypeError(f"Object of type {type_name} is not JSON serializable")
def decode_person(dct):
if "__person__" in dct:
return Person(dct["name"], dct["age"])
return dct
Serialize a Person, then rebuild it:
>>> will = Person("Will", 29)
>>> json_str = json.dumps(will, default=encode_person)
>>> json_str
'{"__person__": true, "name": "Will", "age": 29}'
>>> restored = json.loads(json_str, object_hook=decode_person)
>>> restored.name, restored.age
('Will', 29)
>>> type(restored)
<class '__main__.Person'>
The only thing that changed from the complex example is what goes inside encode_person() and decode_person(). With complex you stored the real and imaginary parts and rebuilt with complex(...). With Person you store name and age and rebuild with Person(...). Same default= on the way out, same object_hook= on the way back. The "__person__": True flag plays the same role as the "__complex__" key from the lesson: it tells the decoder which dictionaries are really a Person.
You’re right that the course would land better if it closed the loop and serialized the Person it opened with, so I’ll pass that along to the rest of the team. Thanks for taking the time to write this up, and you now know exactly how to use JSON with a class.
Become a Member to join the conversation.

Kathy Rogers on June 9, 2026
We created a person class found it was not serializable so create a custom encode that encodes a complex type. We never learned how to encode the person class. I was promised an ice cream sundae and got a dish of ice cream. I don’t know how to use json with a class but I can a use json with complex type something I will never use again.