Tencent/APIJSON · error · IllegalArgumentException

Cannot convert String value '" + value + "' to double: " + e

Error message

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

What it means

Thrown by apijson.JSON.getFloat(Map, String) (APIJSONORM/src/main/java/apijson/JSON.java:497) when the value is a String that Float.parseFloat cannot parse. The message text is misleading: it says "to double" but the method parses a float — a copy-paste artifact in the source; the real failure is a non-numeric string (empty, alphabetic, malformed decimal, stray locale comma).

Source

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

	 * @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;
		}

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

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

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

	/**
	 * 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 getFloatValue(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. Ignore the "double" wording — check the string at this key: trim it and normalize the decimal separator (',' → '.').
  2. Strip non-numeric decoration (currency symbols, units) before parsing, or fix the producer to send a bare JSON number.
  3. Treat blank/null-text values as absent (getFloat returns null for null) rather than letting parseFloat fail.
  4. For double precision needs, use getDouble/getDoubleValue instead of getFloat.

Example fix

// before
Float price = JSON.getFloat(item, "price"); // throws on "12,5"

// after
Object raw = item.get("price");
Float price = raw instanceof Number ? ((Number) raw).floatValue()
        : (raw instanceof String && !((String) raw).isBlank() ? Float.parseFloat(((String) raw).trim().replace(',', '.')) : null);
Defensive patterns

Strategy: validation

Validate before calling

Object v = item.get("price");
if (v instanceof String && !((String) v).trim().matches("[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?")) {
    throw new IllegalArgumentException("'price' not numeric text: " + v);
}

Type guard

static boolean isParsableFloat(Object v) {
    if (v instanceof Number) return true;
    if (v instanceof String) return ((String) v).trim().matches("[+-]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?");
    return false;
}

Try / catch

try {
    Float price = JSON.getFloat(item, "price");
} catch (IllegalArgumentException e) {
    log.warn("Bad float at 'price' (message says 'double' — known wording bug): {}", e.getMessage());
    // normalize locale separators or reject
}

Prevention

When it happens

Trigger: Value is "12,5" (European decimal comma), "", "NaN-", "abc", " 1.5 " with whitespace, or null-spelled "null"; a currency string like "$1.50" or "1.50 USD".

Common situations: Locale-formatted prices and rates serialized as strings; blank strings for optional numeric fields; currency/unit text embedded in the value; developers misdirected by the "double" wording into debugging the wrong method.

Related errors


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