flexible encode and decode