add safety check for toJSON