Add wrapper for JSON encoding exceptions because yay consistency

This should fix the underlying issue that was causing #965 to fail.

I'm not entirely happy with the method chosen, but can't think of another way that is cleaner :confounded: