{"record":{"id":"4209a3430220e92c","repo":"json-path/JsonPath","slug":"can-not-be-converted-to-json","errorCode":null,"errorMessage":" can not be converted to JSON","messagePattern":" can not be converted to JSON","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonSmartJsonProvider.java","lineNumber":89,"sourceCode":"            return createParser().parse(new InputStreamReader(jsonStream, charset), mapper);\n        } catch (ParseException e) {\n            throw new InvalidJsonException(e);\n        } catch (UnsupportedEncodingException e) {\n            throw new JsonPathException(e);\n        }\n    }\n\n    @Override\n    public String toJson(Object obj) {\n\n        if (obj instanceof Map) {\n            return JSONObject.toJSONString((Map<String, ?>) obj, JSONStyle.LT_COMPRESS);\n        } else if (obj instanceof List) {\n            return JSONArray.toJSONString((List<?>) obj, JSONStyle.LT_COMPRESS);\n        } else if (obj instanceof Number ||  obj instanceof Boolean){\n            return JSONValue.toJSONString(obj);\n        } else {\n            throw new UnsupportedOperationException(obj.getClass().getName() + \" can not be converted to JSON\");\n        }\n    }\n\n    private JSONParser createParser() {\n        return new JSONParser(parseMode);\n    }\n}\n","sourceCodeStart":71,"sourceCodeEnd":97,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonSmartJsonProvider.java#L71-L97","documentation":"JsonSmartJsonProvider.toJson() serializes objects it knows about (Map via fastjson JSONObject.toJSONString, List, Number, Boolean via JSONValue). Any other type — a POJO, Date, nested custom object — falls into the else branch and throws UnsupportedOperationException naming the class. net.sf.json-smart's provider cannot serialize arbitrary Java objects to JSON.","triggerScenarios":"Calling JsonPath.toJson(obj) (or otherwise invoking JsonSmartJsonProvider.toJson) with an object that is not a Map, List, Number, or Boolean — e.g. a Java bean, POJO, or org.json JSONObject from a different provider.","commonSituations":"Trying to serialize mapped results of a custom MappingProvider (POJOs) back to JSON; mixing JSON libraries so the object at hand is from another library (org.json, Gson, Jackson types); serializing Dates or nested domain objects.","solutions":["Convert the object to a Map/List of JSON-smart-compatible types before calling toJson","Manually serialize with the object's own library (e.g. fastjson JSON.toJSONString(pojo)) and parse the result into a Map first","Use a different JsonProvider (Jackson/Gson based) that supports POJO serialization if toJson of arbitrary objects is required","If it's a bean, add a toMap()/toJson-friendly representation in the domain class"],"exampleFix":"// before\nString json = Configuration.defaultConfiguration().jsonProvider().toJson(myPojo);\n// after\nString json = JSON.toJSONString(myPojo); // fastjson handles arbitrary beans\n","handlingStrategy":"type-guard","validationCode":"Object o = ...;\nboolean jsonSmartSerializable = o instanceof Map || o instanceof List || o instanceof Number || o instanceof Boolean;\nif (!jsonSmartSerializable) o = convertToMapOrList(o);","typeGuard":"static boolean toJsonCapable(Object o) {\n    return o instanceof Map || o instanceof List || o instanceof Number || o instanceof Boolean;\n}","tryCatchPattern":"try {\n    String json = provider.toJson(obj);\n} catch (UnsupportedOperationException e) {\n    String json = JSON.toJSONString(obj); // fall back to fastjson direct serialization\n}","preventionTips":["Only pass Map/List/Number/Boolean into JsonSmartJsonProvider.toJson","Serialize POJOs with a serializer that supports beans, not the JSON-smart provider","Don't mix JSON library types across providers"],"tags":["json","serialization","jsonpath"],"backgroundTag":"json-serialization-failed","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"}