{"record":{"id":"a11853f05039928c","repo":"apache/iceberg","slug":"failed-to-serialize-object","errorCode":null,"errorMessage":"Failed to serialize object","messagePattern":"Failed to serialize object","errorType":"exception","errorClass":"UncheckedIOException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/util/SerializationUtil.java","lineNumber":68,"sourceCode":"   * Serialize an object to bytes. If the object implements {@link HadoopConfigurable}, the\n   * confSerializer will be used to serialize Hadoop configuration used by the object.\n   *\n   * @param obj object to serialize\n   * @param confSerializer serializer for the Hadoop configuration\n   * @return serialized bytes\n   */\n  public static byte[] serializeToBytes(\n      Object obj, Function<Configuration, SerializableSupplier<Configuration>> confSerializer) {\n    if (obj instanceof HadoopConfigurable) {\n      ((HadoopConfigurable) obj).serializeConfWith(confSerializer);\n    }\n\n    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        ObjectOutputStream oos = new ObjectOutputStream(baos)) {\n      oos.writeObject(obj);\n      return baos.toByteArray();\n    } catch (IOException e) {\n      throw new UncheckedIOException(\"Failed to serialize object\", e);\n    }\n  }\n\n  @SuppressWarnings({\"DangerousJavaDeserialization\", \"unchecked\"})\n  public static <T> T deserializeFromBytes(byte[] bytes) {\n    if (bytes == null) {\n      return null;\n    }\n\n    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);\n        ObjectInputStream ois = new ObjectInputStream(bais)) {\n      return (T) ois.readObject();\n    } catch (IOException e) {\n      throw new UncheckedIOException(\"Failed to deserialize object\", e);\n    } catch (ClassNotFoundException e) {\n      throw new RuntimeException(\"Could not read object \", e);\n    }\n  }","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/util/SerializationUtil.java#L50-L86","documentation":"SerializationUtil.serializeToBytes Java-serializes an object to a byte array; if ObjectOutputStream raises an IOException (non-serializable field, custom writeObject failure) it is rethrown as UncheckedIOException with this message.","triggerScenarios":"Calling serializeToBytes(obj) with an object graph containing a non-Serializable member whose serialization throws IOException — e.g. wrapping a stream/connection inside a Serializable task.","commonSituations":"Serializing tables/functions for broadcast or distributed execution where a field holds a FileIO holding open resources; lambdas capturing non-serializable state.","solutions":["Make all fields of the serialized object Serializable (e.g. use SerializableTable wrapper)","Remove or mark transient fields holding streams/connections","Ensure nested objects' writeObject/readObject are correct"],"exampleFix":"// before\nbyte[] bytes = SerializationUtil.serializeToBytes(table); // table holds open FileIO\n// after\nbyte[] bytes = SerializationUtil.serializeToBytes(SerializableTable.copyOf(table));","handlingStrategy":"try-catch","validationCode":"if (!(obj instanceof java.io.Serializable)) throw new IllegalArgumentException(\"object must be Serializable to serializeToBytes\");","typeGuard":"boolean isSerializable(Object o) { return o instanceof java.io.Serializable; }","tryCatchPattern":"try { byte[] b = SerializationUtil.serializeToBytes(obj); } catch (UncheckedIOException e) { /* fix non-serializable field */ }","preventionTips":["Wrap tables in SerializableTable before serialization","Mark streams/connections transient","Test serialization in unit tests for any class sent to executors"],"tags":["serialization","java-io","unchecked"],"backgroundTag":"json-serialization-failed","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}