Tencent/APIJSON · error · IllegalArgumentException

Cannot convert String value '" + value + "' to long: " + e.g

Error message

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

What it means

Thrown by apijson.JSON.getLongValue(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:469) when the value is a String that Long.parseLong cannot parse. Unlike the getLong overload, the message correctly says "to long". Accepted strings are exact integer text within long range — no whitespace, decimals, separators, or empty string.

Source

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

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

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

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

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

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

View on GitHub (pinned to 5284052872)

Solutions

  1. Trim and normalize the string (strip separators; BigDecimal-parse decimal text) before relying on getLongValue.
  2. Send bare JSON numbers from the producer so the Number branch is taken.
  3. Treat blank strings as missing and skip the call — getLongValue returns 0 for null values.
  4. For strings that may exceed long range, keep the value as String; do not force it into a long.

Example fix

// before
long ts = JSON.getLongValue(payload, "timestamp"); // throws on "1710000000000.0"

// after
Object raw = payload.get("timestamp");
long ts = raw instanceof Number ? ((Number) raw).longValue()
        : (raw instanceof String && !((String) raw).isBlank() ? new java.math.BigDecimal(((String) raw).trim()).longValue() : 0);
Defensive patterns

Strategy: validation

Validate before calling

Object v = payload.get("timestamp");
if (v instanceof String) {
    String s = ((String) v).trim();
    if (!s.matches("[+-]?\\d+")) throw new IllegalArgumentException("'timestamp' not integer text: " + s);
    if (new java.math.BigInteger(s).bitLength() > 63) throw new IllegalArgumentException("'timestamp' exceeds long: " + s);
}

Type guard

static boolean isParsableLong(Object v) {
    if (v instanceof Number) return true;
    if (v instanceof String) { String s = ((String) v).trim(); return s.matches("[+-]?\\d+") && new java.math.BigInteger(s).bitLength() <= 63; }
    return false;
}

Try / catch

try {
    long ts = JSON.getLongValue(payload, "timestamp");
} catch (IllegalArgumentException e) {
    log.warn("Bad long at 'timestamp': {}", e.getMessage());
    // default to 0 or reject the payload
}

Prevention

When it happens

Trigger: Value is "1710000000000.0", " 1710000000000 ", "", "1,710,000,000,000", or a >19-digit string that overflows long.

Common situations: Millisecond timestamps and snowflake IDs serialized as strings by JS frontends (Number precision loss); locale-formatted large numbers; blank strings sent instead of omitted fields; config files with quoted numbers and stray spaces.

Related errors


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