Improve json_handler decorator

It will now sanely respond with a 400 error if the input is invalid, and
the wrapped function can indicate by raising a certain exception that a
status code other than 200 or 500 should be returned.