Check for JSON encoding/decoding errors

Continuing #400 by @joshkadis.