Custom serialization and deserialization