Tencent/APIJSON · error · IllegalArgumentException

Cannot convert String value '" + value + "' to int: " + e.ge

Error message

Cannot convert String value '" + value + "' to int: " + e.getMessage()

What it means

Thrown by apijson.JSON.getInteger(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:385) when the value at the key is a String whose content cannot be parsed by Integer.parseInt. Only exact integer text is accepted — no leading/trailing whitespace, no decimal point, no hex, no thousands separators, no empty string.

Source

Thrown at APIJSONORM/src/main/java/apijson/JSON.java:385

	 * @param key The key
	 * @return The int value
	 * @throws IllegalArgumentException If value cannot be converted to int
	 */
	public static Integer getInteger(Map<String, Object> map, String key) throws IllegalArgumentException {
		Object value = map == null || key == null ? null : map.get(key);
		if (value == null) {
			return null;
		}

		if (value instanceof Number) {
			return ((Number) value).intValue();
		}

		if (value instanceof String) {
			try {
				return Integer.parseInt((String) value);
			} catch (NumberFormatException e) {
				throw new IllegalArgumentException("Cannot convert String value '" + value + "' to int: " + e.getMessage());
			}
		}

		throw new IllegalArgumentException("Cannot convert value of type " + value.getClass().getName() + " to int");
	}

	/**
	 * Get an int value from a Map
	 * @param map Source map
	 * @param key The key
	 * @return The int value
	 * @throws IllegalArgumentException If value cannot be converted to int
	 */
	public static int getIntValue(Map<String, Object> map, String key) throws IllegalArgumentException {
		Object value = map == null || key == null ? null : map.get(key);
		if (value == null) {
			return 0;
		}

View on GitHub (pinned to 5284052872)

Solutions

  1. Trim and validate the string yourself before the call: value.toString().trim(), then check it matches ^[+-]?\d+$ within int range.
  2. If the number may legitimately be decimal ("1.0"), parse as double/BigDecimal first and narrow: (int) Double.parseDouble(str) or BigDecimal.valueOf(...).intValue().
  3. Fix the producer to send a real JSON number instead of a formatted string — that takes the Number branch and never hits parseInt.
  4. For values that can exceed int (large IDs, timestamps in ms), switch to JSON.getLong / getLongValue.

Example fix

// before
Integer count = JSON.getInteger(row, "count"); // throws on "1,000" or "12.5"

// after
Object raw = row.get("count");
Integer count = raw instanceof Number ? ((Number) raw).intValue()
        : (raw instanceof String ? new java.math.BigDecimal(((String) raw).replace(",", "").trim()).intValue() : null);
Defensive patterns

Strategy: validation

Validate before calling

Object v = row.get("count");
if (v instanceof String && !((String) v).trim().matches("[+-]?\\d+")) {
    throw new IllegalArgumentException("'count' is not integer text: " + v);
}

Type guard

static boolean isIntLike(Object v) {
    if (v instanceof Number) return true;
    if (v instanceof String) { String s = ((String) v).trim(); return s.matches("[+-]?\\d+") && Math.abs(Long.parseLong(s)) <= Integer.MAX_VALUE; }
    return false;
}

Try / catch

try {
    Integer count = JSON.getInteger(row, "count");
} catch (IllegalArgumentException e) {
    log.warn("Bad integer at 'count': {}", e.getMessage());
    // reject row, apply default, or re-parse via BigDecimal per your policy
}

Prevention

When it happens

Trigger: Value is "1.0" or "12.5" (Integer.parseInt rejects decimal points); value is " 42 " with surrounding whitespace; value is "" or "null"; value contains a unit or separator like "1,000" or "12a"; value overflows int range such as "3000000000".

Common situations: JSON producers serializing numeric fields as strings with formatting (prices "12.99", locale-formatted counts); frontends forwarding form inputs verbatim without trimming; Excel/CSV-derived payloads with padding or non-breaking spaces; IDs growing past Integer.MAX_VALUE; a literal "null" string when the source system nulls out fields as text.

Related errors


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