{"record":{"id":"5204bca41be6517e","repo":"apache/beam","slug":"failed-to-serialize-s-value-s","errorCode":null,"errorMessage":"Failed to serialize %s value: %s","messagePattern":"Failed to serialize (.+?) value: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/jackson/src/main/java/org/apache/beam/sdk/extensions/jackson/AsJsons.java","lineNumber":159,"sourceCode":"    return new AsJsonsWithFailures<>(exceptionHandler, exceptionHandler.getOutputTypeDescriptor());\n  }\n\n  private String writeValue(InputT input) throws JsonProcessingException {\n    ObjectMapper mapper = Optional.ofNullable(customMapper).orElse(DEFAULT_MAPPER);\n    return mapper.writeValueAsString(input);\n  }\n\n  @Override\n  public PCollection<String> expand(PCollection<InputT> input) {\n    return input.apply(\n        MapElements.via(\n            new SimpleFunction<InputT, String>() {\n              @Override\n              public String apply(InputT input) {\n                try {\n                  return writeValue(input);\n                } catch (IOException e) {\n                  throw new RuntimeException(\n                      \"Failed to serialize \" + inputClass.getName() + \" value: \" + input, e);\n                }\n              }\n            }));\n  }\n\n  /** A {@code PTransform} that adds exception handling to {@link AsJsons}. */\n  public class AsJsonsWithFailures<FailureT>\n      extends PTransform<PCollection<InputT>, WithFailures.Result<PCollection<String>, FailureT>> {\n\n    private @Nullable InferableFunction<WithFailures.ExceptionElement<InputT>, FailureT>\n        exceptionHandler;\n\n    private final transient @Nullable TypeDescriptor<FailureT> failureType;\n\n    AsJsonsWithFailures(\n        InferableFunction<WithFailures.ExceptionElement<InputT>, FailureT> exceptionHandler,\n        TypeDescriptor<FailureT> failureType) {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/jackson/src/main/java/org/apache/beam/sdk/extensions/jackson/AsJsons.java#L141-L177","documentation":"AsJsons<InputT> converts pipeline elements to JSON strings via a Jackson ObjectMapper. If writeValue throws IOException during serialization inside the DoFn, it is wrapped in a RuntimeException naming the input class and value, failing the element (and by default the pipeline).","triggerScenarios":"Applying AsJsons.of(TypeT) to an object graph Jackson cannot serialize: unserializable types, self-referencing structures causing infinite recursion, or a custom serializer throwing IOException.","commonSituations":"POJOs with non-standard types (InputStream, DateTime without JSR310 module), circular object references, or map keys of non-string types.","solutions":["Fix the input class so it is Jackson-serializable (add/adjust getters, @JsonProperty, remove circular refs)","Register needed modules on the ObjectMapper, e.g. jackson-datatype-jsr310 (via withObjectMapper)","Replace unserializable fields with serializable representations before the AsJsons transform"],"exampleFix":"// before\nAsJsons.of(Event.class) // Event has a java.time.Instant field, no JSR310 module\n// after\nnew ObjectMapper().registerModule(new JavaTimeModule());\nAsJsons.<Event>withObjectMapper(mapper).apply(events);","handlingStrategy":"try-catch","validationCode":"try { mapper.writeValueAsString(sample); } catch (IOException e) { throw new IllegalArgumentException(\"Type not Jackson-serializable\", e); }","typeGuard":null,"tryCatchPattern":"try { result = AsJsons.of(Event.class).apply(elements); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Failed to serialize\")) { /* fix POJO or configure mapper, re-run */ } else throw e; }","preventionTips":["Smoke-test serialization of a sample object in unit tests","Register jackson modules (jsr310, joda) for all custom types","Avoid circular references and unserializable field types in pipeline POJOs"],"tags":["jackson","json","serialization","java"],"backgroundTag":"json-serialization-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}