{"record":{"id":"cf91946d1b755cc8","repo":"chinabugotech/hutool","slug":"unsupported-tomap-value-type","errorCode":null,"errorMessage":"Unsupported toMap value type: {}","messagePattern":"Unsupported toMap value type: (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/MapConverter.java","lineNumber":86,"sourceCode":"\t\t\tfinal Class<?> mapClass = TypeUtil.getClass(this.mapType);\n\t\t\tif (null == mapClass || mapClass.isAssignableFrom(AbstractMap.class)) {\n\t\t\t\t// issue#I6YN2A，默认有序\n\t\t\t\tmap =  new LinkedHashMap<>();\n\t\t\t} else{\n\t\t\t\tmap = MapUtil.createMap(mapClass);\n\t\t\t}\n\t\t\tconvertMapToMap((Map) value, map);\n\t\t} else if (BeanUtil.isReadableBean(value.getClass())) {\n\t\t\tif(value.getClass().getName().equals(\"cn.hutool.json.JSONArray\")){\n\t\t\t\t// issue#3795 增加JSONArray转Map错误检查\n\t\t\t\tthrow new UnsupportedOperationException(StrUtil.format(\"Unsupported {} to Map.\", value.getClass().getName()));\n\t\t\t}\n\n\t\t\tmap = BeanUtil.beanToMap(value);\n\t\t\t// 二次转换，转换键值类型\n\t\t\tmap = convertInternal(map);\n\t\t} else {\n\t\t\tthrow new UnsupportedOperationException(StrUtil.format(\"Unsupported toMap value type: {}\", value.getClass().getName()));\n\t\t}\n\t\treturn map;\n\t}\n\n\t/**\n\t * Map转Map\n\t *\n\t * @param srcMap 源Map\n\t * @param targetMap 目标Map\n\t */\n\tprivate void convertMapToMap(Map<?, ?> srcMap, Map<Object, Object> targetMap) {\n\t\tfinal ConverterRegistry convert = ConverterRegistry.getInstance();\n\t\tsrcMap.forEach((key, value)->{\n\t\t\tkey = TypeUtil.isUnknown(this.keyType) ? key : convert.convert(this.keyType, key);\n\t\t\tvalue = TypeUtil.isUnknown(this.valueType) ? value : convert.convert(this.valueType, value);\n\t\t\ttargetMap.put(key, value);\n\t\t});\n\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/MapConverter.java#L68-L104","documentation":"MapConverter.convertInternal accepts a Map or a readable Bean (BeanUtil.isReadableBean). Any other source type (primitive, scalar String that is not a bean, array, etc.) reaches the else branch and throws UnsupportedOperationException naming the value class.","triggerScenarios":"Calling Convert.convert(scalar, Map.class) where scalar is a Number, Boolean, plain String, primitive array, etc.","commonSituations":"Misrouting a scalar through Map conversion; generic pipeline that assumes every value is bean-shaped.","solutions":["Wrap the scalar in a Map before converting.","Use the correct target type for the source (e.g. String -> String, Number -> Number).","Pre-check whether the source is a Map or a readable bean before targeting Map."],"exampleFix":"// before\nMap m = Convert.convert(\"hello\", Map.class);\n\n// after - wrap structured\nMap<String,Object> src = Collections.singletonMap(\"value\", \"hello\");\nMap m = Convert.convert(src, Map.class);","handlingStrategy":"type-guard","validationCode":"if (!(value instanceof Map) && !BeanUtil.isReadableBean(value.getClass())) {\n    value = Collections.singletonMap(\"value\", value); // or pick a different target\n}\nConvert.convert(value, Map.class);","typeGuard":"value instanceof Map || BeanUtil.isReadableBean(value.getClass())","tryCatchPattern":"try {\n    return Convert.convert(value, Map.class);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().startsWith(\"Unsupported toMap value type\")) { /* wrap scalar in a Map */ }\n    else throw e;\n}","preventionTips":["Only target Map when the source is a Map or a readable bean.","Wrap scalars in a Map if a Map truly is required."],"tags":["convert","map","type-mismatch"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}