refactor: abstract the serialization code to a separate file