Refactorize encode visitor.