Restructure the serialization and deserialization logic