Tencent/APIJSON · error · UnsupportedEncodingException

JSON格式不合法! !

Error message

JSON格式不合法!{}! {}

What it means

parseRequest(String) wraps JSON.parseObject plus a null check; any parse failure or null result is rethrown as UnsupportedEncodingException with message 'JSON格式不合法!<cause>! <original request>'. It is the entry gate: the raw request body must be a valid JSON object string before any APIJSON parsing begins.

Solutions

  1. Print the request string included in the message and run it through any JSON validator to find the syntax error
  2. Use a real JSON serializer client-side instead of string concatenation/template literals
  3. Ensure the endpoint receives a raw JSON object body ({} outermost), not an array or query-string
  4. Check for encoding damage: URL-decode twice only if your transport requires it

Example fix

// before
String body = "{'name': 'a',}");
// after
String body = "{\"name\": \"a\"}";
Defensive patterns

Strategy: validation

Validate before calling

Object parsed = new JSONParser().parse(body);
if (!(parsed instanceof JSONObject)) throw new IOException("body is not a JSON object");

Type guard

boolean isValidJsonObjectString(String s) {
  try { return s != null && s.trim().startsWith("{") && new JSONParser().parse(s) instanceof JSONObject; }
  catch (Exception e) { return false; }
}

Try / catch

catch (UnsupportedEncodingException e) { if (e.getMessage().contains("JSON格式不合法")) { logPayload(body); return clientError("invalid json body"); } throw e; }

Prevention

When it happens

Trigger: Sending a malformed body: truncated JSON, single quotes, trailing commas, a JSON array instead of object, form-encoded or XML content, or an empty body. Also double-URL-encoding that corrupts the payload before decode.

Common situations: HTTP client sends the JSON with wrong Content-Type handling or manual string concatenation; proxy/gateway mangles the body; client library serializes undefined to invalid tokens; version change switched from form params to raw body without updating the client.

Related errors


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

Appendix: source

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

		if (verifier == null) {
			verifier = createVerifier().setVisitor(getVisitor());
		}
		verifier.setParser(this);
		return verifier;
	}

	/**解析请求JSONObject
	 * @param request => URLDecoder.decode(request, UTF_8);
	 * @return
	 * @throws Exception
	 */
	public static <M extends Map<String, Object>> M parseRequest(String request) throws Exception {
		try {
			M req = JSON.parseObject(request);
			Objects.requireNonNull(req);
			return req;
		} catch (Throwable e) {
			throw new UnsupportedEncodingException("JSON格式不合法!" + e.getMessage() + "! " + request);
		}
	}

	/**解析请求json并获取对应结果
	 * @param request
	 * @return
	 */
	@Override
	public String parse(String request) {
		return JSON.toJSONString(parseResponse(request));
	}
	/**解析请求json并获取对应结果
	 * @param request
	 * @return
	 */
	@NotNull
	@Override
	public String parse(M request) {

View on GitHub (pinned to 5284052872)