added JSON encode for error codes, fixed <state> field.