This is still pretty magical because we're enumerating over all of the currently defined variables, which is not a guaranteed list. In addition if there's a private variable that's added for internal state tracking, this will glob it up. (For instance, this just bit us over in [GH-1868](https://github.com/puppetlabs/puppet/pull/1868/files#L0L233).

We already have the [`#to_pson`](https://github.com/rarruda/puppet/blob/1bd1da1ad5f758811395eab7bbe4039907f05688/lib/puppet/util/log.rb#L275) method implemented, that could be decomposed into a `#to_hash` method that your code could invoke, and have `#to_pson` call that method as well.