Add serialization methods for wire protocol

repushed with shanes requests.