{"record":{"id":"eb2dabd0b698fcb9","repo":"redis/jedis","slug":"failed-to-serialize-object","errorCode":null,"errorMessage":"Failed to serialize object","messagePattern":"Failed to serialize object","errorType":"exception","errorClass":"JedisCacheException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/csc/CacheEntry.java","lineNumber":44,"sourceCode":"  }\n\n  public T getValue() {\n    return toObject(bytes);\n  }\n\n  public CacheConnection getConnection() {\n    return connection.get();\n  }\n\n  private static byte[] toBytes(Object object) {\n    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream oos = new ObjectOutputStream(baos)) {\n      oos.writeObject(object);\n      oos.flush();\n      oos.close();\n      return baos.toByteArray();\n    } catch (IOException e) {\n      throw new JedisCacheException(\"Failed to serialize object\", e);\n    }\n  }\n\n  private T toObject(byte[] data) {\n    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);\n        ObjectInputStream ois = new ObjectInputStream(bais)) {\n      return (T) ois.readObject();\n    } catch (IOException | ClassNotFoundException e) {\n      throw new JedisCacheException(\"Failed to deserialize object\", e);\n    }\n  }\n}\n","sourceCodeStart":26,"sourceCodeEnd":57,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/csc/CacheEntry.java#L26-L57","documentation":"CacheEntry.toBytes serializes the cached value with Java ObjectOutputStream; any IOException during writing is rethrown as JedisCacheException('Failed to serialize object'). Since ByteArrayOutputStream writes never really fail, this almost always means writeObject rejected the object itself. The most common cause is a value whose class does not implement java.io.Serializable.","triggerScenarios":"Storing (put/update on the cache) a value whose class does not implement Serializable; the object graph containing a non-serializable field (e.g. a Connection, lambda capture of non-serializable this, or a field whose class changed serialVersionUID incompatibly is the deserialize variant).","commonSituations":"Using the default Cacheable with POJOs from third-party libraries that are not Serializable; caching objects holding streams/sockets; passing method-local anonymous classes that capture outer non-serializable state.","solutions":["Make the cached value's class (and its whole object graph) implement java.io.Serializable.","Mark non-serializable fields transient if they are not needed after deserialization.","Register a custom Cacheable implementation that serializes via JSON/protobuf instead of Java serialization for non-Serializable types.","Use primitives/String/well-known types as cache values when possible."],"exampleFix":"// before\nclass UserDto { String name; } // not Serializable -> JedisCacheException\n// after\nclass UserDto implements java.io.Serializable {\n  private static final long serialVersionUID = 1L;\n  String name;\n}","handlingStrategy":"validation","validationCode":"static <T> void assertSerializable(T value) {\n  if (!(value instanceof java.io.Serializable)) {\n    throw new IllegalArgumentException(value.getClass() + \" must implement Serializable to be cached\");\n  }\n}","typeGuard":"<T extends java.io.Serializable> void putIfSerializable(Cache cache, String key, T value) {\n  cache.put(SafeEncoder.encode(key), value);\n}","tryCatchPattern":"try {\n  cache.put(key, value);\n} catch (JedisCacheException e) {\n  if (e.getMessage().contains(\"Failed to serialize\")) {\n    // log value.getClass(), use a custom Cacheable (JSON) or skip caching this type\n  } else throw e;\n}","preventionTips":["Make every cached value class implement Serializable with an explicit serialVersionUID","Mark streams/sockets/connections fields transient","Use a custom Cacheable with JSON serialization for third-party types you cannot edit"],"tags":["serialization","cache","client-side-caching"],"backgroundTag":"json-marshal-failed","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}