Backport Message.to_json fix from 974f3619 to 2.8 branch

See 974f36197c5d51dc85950f026e578bfe4c844e62 for original fix. This is
a backport from 3.0.3 to 2.8.
Merge pull request #192 from localshred/bj/backport-message-json

Backport Message.to_json fix from 974f3619 to 2.8 branch