{"record":{"id":"f5efac31521c3e85","repo":"jwtk/jjwt","slug":"unable-to-serialize-object-of-type-to-json-using","errorCode":null,"errorMessage":"Unable to serialize object of type  to JSON using known heuristics.","messagePattern":"Unable to serialize object of type  to JSON using known heuristics\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"extensions/orgjson/src/main/java/io/jsonwebtoken/orgjson/io/OrgJsonSerializer.java","lineNumber":130,"sourceCode":"\n        if (object instanceof Map) {\n            Map<?, ?> map = (Map<?, ?>) object;\n            return toJSONObject(map);\n        }\n\n        if (Objects.isArray(object)) {\n            object = Collections.arrayToList(object); //sets object to List, will be converted in next if-statement:\n        }\n\n        if (object instanceof Collection) {\n            Collection<?> coll = (Collection<?>) object;\n            return toJSONArray(coll);\n        }\n\n        //not an immediately JSON-compatible object and probably a JavaBean (or similar).  We can't convert that\n        //directly without using a marshaller of some sort:\n        String msg = \"Unable to serialize object of type \" + object.getClass().getName() + \" to JSON using known heuristics.\";\n        throw new IOException(msg);\n    }\n\n    private JSONObject toJSONObject(Map<?, ?> m) throws IOException {\n\n        JSONObject obj = new JSONObject();\n\n        for (Map.Entry<?, ?> entry : m.entrySet()) {\n            Object k = entry.getKey();\n            Object value = entry.getValue();\n\n            String key = String.valueOf(k);\n            value = toJSONInstance(value);\n            obj.put(key, value);\n        }\n\n        return obj;\n    }\n","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/extensions/orgjson/src/main/java/io/jsonwebtoken/orgjson/io/OrgJsonSerializer.java#L112-L148","documentation":"OrgJsonSerializer.toJSONInstance throws IOException when the object to serialize is not a JSON-compatible type (Map, Collection, primitive/String/Number/Boolean, JSONObject/JSONArray, etc.) and no known heuristic can convert it. org.json has no JavaBean marshaller, so arbitrary POJOs are rejected.","triggerScenarios":"Calling Jwts.builder().claims().add(...) or the orgjson Serializer with a custom POJO/complex object as a claim value that is not a Map, Collection, array, or primitive wrapper.","commonSituations":"Putting domain entities (User, Address) directly into JWT claims; nesting custom types inside claims; forgetting that only JSON-native types are supported without a custom serializer.","solutions":["Convert the POJO to a Map<String,Object> (manually or via Jackson) before adding it as a claim","Only use JSON-native claim values: String, Number, Boolean, Map, List, null","Register a full Serializer implementation (Jackson/Gson) instead of the orgjson one if POJO support is needed","Add a toMap()/toJSON() method on your POJO and pass that"],"exampleFix":"// before\nJwts.builder().claim(\"user\", userPojo); // IOException on serialize\n// after\nMap<String, Object> userMap = new HashMap<>();\nuserMap.put(\"id\", userPojo.getId());\nJwts.builder().claim(\"user\", userMap);","handlingStrategy":"type-guard","validationCode":"static boolean isJsonCompatible(Object o) {\n    return o == null || o instanceof Map || o instanceof Collection || o instanceof String\n        || o instanceof Number || o instanceof Boolean || o instanceof org.json.JSONObject\n        || o instanceof org.json.JSONArray || (o != null && o.getClass().isArray());\n}","typeGuard":"see validationCode isJsonCompatible(Object)","tryCatchPattern":"try {\n    String json = serializer.serialize(value, out);\n} catch (IOException e) {\n    if (e.getMessage().startsWith(\"Unable to serialize object of type\")) {\n        value = convertToMap(value); // fallback converter\n    } else throw e;\n}","preventionTips":["Restrict JWT claim values to JSON-native types (String, Number, Boolean, Map, List)","Add an explicit toMap() on domain objects placed in claims","If POJO support is required, use the Jackson or Gson extension serializer instead of orgjson","Test claim round-trips (build then parse) for every claim type you use"],"tags":["java","jjwt","serialization","json","unsupported-type"],"backgroundTag":"json-marshal-failed","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}