Serializes an error into a JSON string.