check for toJSON method before trying to call