Split object/array encoding into smaller functions
Fix object traits reference encoding
Tests for encoding objects and array by reference