Encode a key/value pair for database storage.