Implement object persistence

This follows my proposal for #193.
Largely inspired by AKFish's work.