{"record":{"id":"fd769047cc1b6581","repo":"json-path/JsonPath","slug":"can-not-map-a-src-getclass-to-boolea","errorCode":null,"errorMessage":"can not map a \" + src.getClass() + \" to \" + Boolean.class.getName()","messagePattern":"can not map a \" \\+ src\\.getClass\\(\\) \\+ \" to \" \\+ Boolean\\.class\\.getName\\(\\)","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java","lineNumber":260,"sourceCode":"                } catch (ParseException e) {\n                    throw new MappingException(e);\n                }\n            }\n            throw new MappingException(\"can not map a \" + src.getClass() + \" to \" + Date.class.getName());\n        }\n    }\n    private static class BooleanReader extends JsonReaderI<Boolean> {\n        public BooleanReader() {\n            super(null);\n        }\n        public Boolean convert(Object src) {\n            if(src == null){\n                return null;\n            }\n            if (Boolean.class.isAssignableFrom(src.getClass())) {\n                return (Boolean) src;\n            }\n            throw new MappingException(\"can not map a \" + src.getClass() + \" to \" + Boolean.class.getName());\n        }\n    }\n}\n","sourceCodeStart":242,"sourceCodeEnd":264,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java#L242-L264","documentation":"JsonSmartMappingProvider's BooleanReader.convert() only maps null and actual Boolean instances to Boolean; every other runtime type (String \"true\"/\"false\", numbers, containers) throws MappingException. Unlike the numeric readers, there is no String fallback, so stringly-typed booleans are rejected.","triggerScenarios":"map(obj, Boolean.class, configuration) via JsonSmartMappingProvider where obj is a String (e.g. \"true\"), an Integer 0/1, a JSONObject, or any non-Boolean type.","commonSituations":"JSON sources that encode booleans as \"true\"/\"false\" strings or 0/1 numbers; data produced by lenient providers (Jackson coerces \"true\" strings) then read through JsonSmart mapping; paths resolving to the wrong node.","solutions":["Pre-normalize the value: Boolean.parseBoolean(obj.toString()) when it is a String, or obj != 0 for numeric flags.","Make the upstream JSON produce real JSON booleans (true/false without quotes).","Verify the JsonPath selects a boolean leaf, not a string/number field.","Switch to JacksonMappingProvider/GsonMappingProvider, which coerce \"true\" strings to Boolean.","Register a custom MappingProvider that handles string/number-to-boolean conversion."],"exampleFix":"// before\nBoolean b = cfg.mappingProvider().map(\"true\", Boolean.class, cfg); // MappingException\n// after\nObject v = JsonPath.read(json, \"$.enabled\");\nBoolean b = (v instanceof Boolean) ? (Boolean) v : Boolean.parseBoolean(v.toString());","handlingStrategy":"type-guard","validationCode":"Object v = JsonPath.read(json, \"$.field\");\nif (!(v instanceof Boolean))\n    throw new IllegalArgumentException(\"Cannot map \" + (v == null ? \"null\" : v.getClass()) + \" to Boolean; JsonSmart provider accepts only real booleans\");","typeGuard":"static Boolean toBool(Object v) {\n    if (v instanceof Boolean) return (Boolean) v;\n    if (v instanceof String) return Boolean.parseBoolean((String) v);\n    if (v instanceof Number) return ((Number) v).intValue() != 0;\n    return null;\n}","tryCatchPattern":"try {\n    Boolean b = cfg.mappingProvider().map(value, Boolean.class, cfg);\n} catch (MappingException e) {\n    Boolean b = toBool(value);\n    if (b == null) throw new IllegalArgumentException(\"Non-boolean value\", e);\n}","preventionTips":["Emit real JSON booleans (true/false, unquoted) from upstream sources.","Never expect \"true\"/\"false\" strings or 0/1 numbers to coerce via JsonSmart mapping.","Read as Object and normalize with a helper before mapping.","If inputs are stringly-typed, prefer Jackson/Gson providers which coerce strings to Boolean."],"tags":["json-path","mapping","boolean","json-smart"],"backgroundTag":"incompatible-source-type","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}