{"record":{"id":"58ca232e5943e255","repo":"alibaba/arthas","slug":"conversion-from-json-to-type-failed","errorCode":null,"errorMessage":"Conversion from JSON to {type} failed","messagePattern":"Conversion from JSON to (.+?) failed","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"arthas-mcp-server/src/main/java/com/taobao/arthas/mcp/server/util/JsonParser.java","lineNumber":73,"sourceCode":"\tprivate JsonParser() {\n\t}\n\n\tpublic static ObjectMapper getObjectMapper() {\n\t\treturn OBJECT_MAPPER;\n\t}\n\n\tpublic static <T> T fromJson(String json, Class<T> type) {\n\t\tAssert.notNull(json, \"json cannot be null\");\n\t\tAssert.notNull(type, \"type cannot be null\");\n\n\t\ttry {\n\t\t\treturn JSON.parseObject(json, type);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\ttry {\n\t\t\t\treturn OBJECT_MAPPER.readValue(json, type);\n\t\t\t} catch (JsonProcessingException jacksonEx) {\n\t\t\t\tthrow new IllegalStateException(\"Conversion from JSON to \" + type.getName() + \" failed\", ex);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic static <T> T fromJson(String json, Type type) {\n\t\tAssert.notNull(json, \"json cannot be null\");\n\t\tAssert.notNull(type, \"type cannot be null\");\n\n\t\ttry {\n\t\t\treturn JSON.parseObject(json, type);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\ttry {\n\t\t\t\treturn OBJECT_MAPPER.readValue(json, OBJECT_MAPPER.constructType(type));\n\t\t\t} catch (JsonProcessingException jacksonEx) {\n\t\t\t\tthrow new IllegalStateException(\"Conversion from JSON to \" + type.getTypeName() + \" failed\", ex);\n\t\t\t}\n\t\t}","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/arthas-mcp-server/src/main/java/com/taobao/arthas/mcp/server/util/JsonParser.java#L55-L91","documentation":"JsonParser.fromJson(String, Class<T>) first tries fastjson's JSON.parseObject, and on failure falls back to Jackson's OBJECT_MAPPER.readValue. If Jackson also throws JsonProcessingException, both are discarded as cause and an IllegalStateException(\"Conversion from JSON to <type> failed\") is raised. The wrapped cause is the fastjson exception, not the Jackson one.","triggerScenarios":"Passing malformed JSON, JSON whose structure cannot bind to the target Class (e.g. array into an object type, wrong field types), or null/non-text content to fromJson(json, SomeClass.class).","commonSituations":"Upstream MCP/tool returns an unexpected schema; wrong target Class supplied (e.g. Map vs POJO); numeric/string type mismatch in nested fields; truncated payload from a network read.","solutions":["Log and inspect the raw JSON payload at the call site to find the schema mismatch.","Correct the target Class to match the actual JSON shape, or fix the producer to send the expected schema.","If the payload may legitimately vary, deserialize into a permissive type (Map/Object) first then map manually."],"exampleFix":"// before\nFoo f = JsonParser.fromJson(json, Foo.class);\n\n// after - diagnose then bind defensively\nObject parsed = JsonParser.fromJson(json, Object.class);\nlogger.debug(\"payload={}\", parsed);\nFoo f = JsonParser.fromJson(json, Foo.class);","handlingStrategy":"try-catch","validationCode":"if (json == null || json.isBlank()) {\n    return defaultValue;\n}\n// optional: cheap pre-check that it's a JSON object/array\n","typeGuard":null,"tryCatchPattern":"try {\n    return JsonParser.fromJson(json, Target.class);\n} catch (IllegalStateException e) {\n    logger.error(\"Failed to bind JSON to {}: payload={}\", Target.class, json, e);\n    throw new MyApiException(\"Invalid payload shape\", e);\n}","preventionTips":["Log the raw JSON at the boundary to diagnose schema drift fast.","Match the target Class precisely to the producer's contract.","For variable shapes, deserialize to Map/Object first, then map defensively."],"tags":["json","serialization","fastjson","jackson"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}