{"record":{"id":"7118ee0e698945f9","repo":"apache/beam","slug":"cannot-serialize-s-to-a-json-string","errorCode":null,"errorMessage":"Cannot serialize %s to a JSON string.","messagePattern":"Cannot serialize (.+?) to a JSON string\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryHelpers.java","lineNumber":630,"sourceCode":"    }\n    JobStatus status = job.getStatus();\n    if (status.getErrorResult() != null) {\n      return Status.FAILED;\n    } else if (status.getErrors() != null && !status.getErrors().isEmpty()) {\n      return Status.FAILED;\n    } else {\n      return Status.SUCCEEDED;\n    }\n  }\n\n  public static @PolyNull String toJsonString(@PolyNull Object item) {\n    if (item == null) {\n      return null;\n    }\n    try {\n      return BigQueryIO.JSON_FACTORY.toString(item);\n    } catch (IOException e) {\n      throw new RuntimeException(\n          String.format(\"Cannot serialize %s to a JSON string.\", item.getClass().getSimpleName()),\n          e);\n    }\n  }\n\n  public static <T> @PolyNull T fromJsonString(@PolyNull String json, Class<T> clazz) {\n    if (json == null) {\n      return null;\n    }\n    try {\n      // If T is Void then this ends up null, otherwise it is not; kind of a tough invariant\n      @SuppressWarnings({\n        \"nullness\" // TODO(https://github.com/apache/beam/issues/20497)\n      })\n      @NonNull T result = BigQueryIO.JSON_FACTORY.fromString(json, clazz);\n      return result;\n    } catch (IOException e) {\n      throw new RuntimeException(","sourceCodeStart":612,"sourceCodeEnd":648,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryHelpers.java#L612-L648","documentation":"toJsonString() serializes BigQuery API model objects to JSON via BigQueryIO's Jackson JSON_FACTORY. If Jackson throws IOException during serialization, the library wraps it in a RuntimeException stating the class couldn't be serialized. This almost always indicates the object contains content Jackson cannot write (rare with the API model classes).","triggerScenarios":"BigQueryHelpers.toJsonString(item) called with a TableReference/Job/JobConfiguration whose Jackson serialization fails (non-standard getters, self-referencing fields, custom subclasses not recognized by the JSON factory).","commonSituations":"Passing a custom subclass of a BigQuery model class that introduces properties the Jackson factory can't handle, or a corrupted object graph; uncommon with plain API model objects.","solutions":["Check the wrapped IOException cause for the Jackson serialization detail.","Use the standard com.google.api.services.bigquery.model classes rather than custom subclasses.","If serialization of a custom type is needed, register a module/serializer on your own ObjectMapper instead of relying on BigQueryIO.JSON_FACTORY."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  String json = BigQueryHelpers.toJsonString(item);\n} catch (RuntimeException e) {\n  // inspect cause IOException for the Jackson serialization failure\n}","preventionTips":["Use stock BigQuery API model classes rather than custom subclasses.","Avoid object graphs with cycles or non-serializable custom fields.","Do your own serialization with a configured ObjectMapper for custom types."],"tags":["java","bigquery","json-serialization","jackson"],"backgroundTag":"json-marshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}