{"record":{"id":"ae75b0f45e228417","repo":"jwtk/jjwt","slug":"cannot-serialize-name-to-json-cause-t-getme","errorCode":null,"errorMessage":"Cannot serialize ${name} to JSON. Cause: ${t.getMessage()}","messagePattern":"Cannot serialize (.+?) to JSON\\. Cause: (.+?)","errorType":"exception","errorClass":"io.jsonwebtoken.io.SerializationException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/io/NamedSerializer.java","lineNumber":42,"sourceCode":"import java.util.Map;\n\npublic class NamedSerializer extends AbstractSerializer<Map<String, ?>> {\n\n    private final String name;\n    private final Serializer<Map<String, ?>> DELEGATE;\n\n    public NamedSerializer(String name, Serializer<Map<String, ?>> serializer) {\n        this.DELEGATE = Assert.notNull(serializer, \"JSON Serializer cannot be null.\");\n        this.name = Assert.hasText(name, \"Name cannot be null or empty.\");\n    }\n\n    @Override\n    protected void doSerialize(Map<String, ?> m, OutputStream out) throws SerializationException {\n        try {\n            this.DELEGATE.serialize(m, out);\n        } catch (Throwable t) {\n            String msg = String.format(\"Cannot serialize %s to JSON. Cause: %s\", this.name, t.getMessage());\n            throw new SerializationException(msg, t);\n        }\n    }\n}\n","sourceCodeStart":24,"sourceCodeEnd":46,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/io/NamedSerializer.java#L24-L46","documentation":"Thrown as a SerializationException when the delegate JSON serializer fails to write the given claims Map to the output stream. The message names the entity being serialized (e.g. 'JWT payload') and includes the underlying cause. Any Throwable during delegate.serialize is wrapped here.","triggerScenarios":"Calling serialize on NamedSerializer with a Map containing values the JSON serializer cannot handle (unserializable types, cyclic references, non-JSON-friendly objects like Date without a converter) or when the target OutputStream fails.","commonSituations":"Putting custom Java objects (Date, BigDecimal with unusual settings, POJOs without registered converters) into claims; cyclic object graphs; OutputStream write failures (closed stream, disk full).","solutions":["Inspect the cause message: unserializable types are the most frequent root cause.","Convert claim values to JSON-friendly types (String, Number, Boolean, Map, List) before serializing.","Register a JJWT Serializer (e.g. Jackson or Gson based) that supports your custom types via converters.","Verify the target OutputStream is open and writable."],"exampleFix":"// before\nclaims.put(\"expires\", new Date()); // unserializable without converter\nJwts.builder().setClaims(claims).compact();\n// after\nclaims.put(\"expires\", new Date().getTime() / 1000); // JSON-friendly number\nJwts.builder().setClaims(claims).compact();","handlingStrategy":"type-guard","validationCode":"// Java\nstatic void assertJsonFriendly(Map<String,?> claims) {\n    for (Map.Entry<String,?> e : claims.entrySet()) {\n        Object v = e.getValue();\n        boolean ok = v == null || v instanceof String || v instanceof Number\n            || v instanceof Boolean || v instanceof Map || v instanceof List;\n        if (!ok) throw new IllegalArgumentException(\n            \"Claim '\" + e.getKey() + \"' has non-JSON type: \" + v.getClass().getName());\n    }\n}","typeGuard":"static boolean isJsonFriendly(Object v) {\n    return v == null || v instanceof String || v instanceof Number\n        || v instanceof Boolean || v instanceof Map || v instanceof List;\n}","tryCatchPattern":"try {\n    String jwt = Jwts.builder().setClaims(claims).compact();\n} catch (SerializationException e) {\n    log.error(\"Claims not serializable: {}\", e.getMessage(), e.getCause());\n    throw new IllegalArgumentException(\"Claims contain non-JSON-friendly values\", e);\n}","preventionTips":["Store only JSON-friendly types (String, Number, Boolean, Map, List) in claims.","Represent dates as epoch seconds/millis numbers, not java.util.Date objects.","Register a JJWT Serializer (Jackson/Gson) suited to your object model at bootstrap.","Check the exception cause to pinpoint which claim value failed serialization."],"tags":["json","serialization","jwt"],"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"}