encode and decode object