Fix bug with PackWithReplacements when top-level object with complex objects.

PackWithReplacements failed with "Resources found during pickling",
because the original value becomes a Reference after replacemenet.