Serialize data before encoding