json: Refactor exceptions. Add Output class.