Tencent/APIJSON · error · UnsupportedEncodingException

JSON格式不合法!

Error message

JSON格式不合法!

What it means

In parseResponse(String), JSON.parseObject returning null (e.g. input is "null", empty after trim, or a bare scalar) triggers UnsupportedEncodingException('JSON格式不合法!'). Unlike parseRequest it is caught internally and converted into an error result object rather than propagating.

Source

Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractParser.java:487

	@Override
	public String parse(M request) {
		return JSON.toJSONString(parseResponse(request));
	}

	/**解析请求json并获取对应结果
	 * @param request 先parseRequest中URLDecoder.decode(request, UTF_8);再parseResponse(getCorrectRequest(...))
	 * @return parseResponse(requestObject);
	 */
	@NotNull
	@Override
	public M parseResponse(String request) {
		Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
				+ requestMethod + "/parseResponse  request = \n" + request + "\n\n");

		try {
			requestObject = JSON.parseObject(request);
			if (requestObject == null) {
				throw new UnsupportedEncodingException("JSON格式不合法!");
			}
		} catch (Exception e) {
			return newErrorResult(e, isRoot);
		}

		return parseResponse(requestObject);
	}

	private int queryDepth;
	private long executedSQLDuration;

	/**解析请求json并获取对应结果
	 * @param request
	 * @return requestObject
	 */
	@NotNull
	@Override
	public M parseResponse(M request) {

View on GitHub (pinned to 5284052872)

Solutions

  1. Check the input string is non-empty and starts with '{' before calling parseResponse
  2. Fix the upstream producer that emitted null/empty where a JSON object was promised
  3. Inspect the returned error result's msg — it reflects this exact parse failure point
  4. Add a client-side guard: if body == null || body.trim().isEmpty() or equals "null", raise a transport error instead

Example fix

// before
parser.parseResponse(readBodySilently(httpRequest)); // may be "" or "null"
// after
String body = readBody(httpRequest);
if (body == null || body.trim().isEmpty() || "null".equals(body.trim())) throw new IOException("empty response");
parser.parseResponse(body);
Defensive patterns

Strategy: type-guard

Validate before calling

String b = body == null ? null : body.trim();
if (b == null || b.isEmpty() || "null".equals(b)) return errorResult("empty/non-object response");

Type guard

boolean isNonEmptyJsonObject(String s) {
  String t = s == null ? "" : s.trim();
  return t.startsWith("{") && t.endsWith("}");
}

Prevention

When it happens

Trigger: Calling parseResponse with the literal string "null", an empty string, whitespace, or a top-level non-object token like "123"/"\"text\"" — parseObject succeeds but yields null for an object expectation.

Common situations: Upstream service or queue returns an empty body on error and the client forwards it directly; string concatenation defaults produce "null" from a Java null; misconfigured mock server returning plain text; retry logic passing a previously-consumed stream rendered as null.

Related errors


AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14). Data as JSON: /api/errors/8439e12bfee23e31. Report an issue: GitHub.