lib: handle JSON parse errors

Don't throw an uncatchable exception when the input is malformed JSON.
Forward the exception as an 'error' event that the user can handle or
ignore as s/he sees fit.