factor out some common text marshaling code