using stack to keep track of current position during the serialization process