Call into toJSON method, if it exists.
Reference exports as this, allowing inheritance.

This doesn't work for the Scope object, but reimplementing it for custom
behavior probably costs fewer lines any way.