apache/hadoop · error · RuntimeException

Failed to serialize object to JSON

Error message

Failed to serialize object to JSON

What it means

JsonUtils.toString(Object) serializes with Jackson's writeValueAsString; any serialization failure - a type with no serializable properties, a getter that throws, a cyclic object graph, or a type needing an unregistered module - surfaces as RuntimeException("Failed to serialize object to JSON", e) with the Jackson exception as cause.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonUtils.java:88

   */
  public static <T> T parse(String json, TypeReference<T> typeRef) {
    try {
      return MAPPER.readValue(json, typeRef);
    } catch (IOException e) {
      throw new RuntimeException("Failed to parse JSON", e);
    }
  }

  /**
   * Serialize an object to a JSON string.
   * @param obj the object to serialize
   * @return the JSON string
   */
  public static String toString(Object obj) {
    try {
      return MAPPER.writeValueAsString(obj);
    } catch (IOException e) {
      throw new RuntimeException("Failed to serialize object to JSON", e);
    }
  }
}

View on GitHub (pinned to 2add963021)

Solutions

  1. Read the cause: JsonMappingException pinpoints the offending property and the exact reason.
  2. Expose the data: public getters or fields annotated with @JsonProperty.
  3. Break cycles with @JsonIgnore or @JsonManagedReference/@JsonBackReference, and register required modules on the mapper.

Example fix

// before
class Node {
  private String name;          // no getter -> nothing serializable
  private Node parent;          // cycle
  String log() { return JsonUtils.toString(this); }
}

// after
class Node {
  @JsonProperty private String name;
  @JsonIgnore private Node parent;
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  String s = JsonUtils.toString(obj);
} catch (RuntimeException e) {
  // e.getCause() is a JsonMappingException naming the offending property
}

Prevention

When it happens

Trigger: toString(dto) where the DTO has only private fields and no getters; a lazy getter that throws; a bidirectional parent/child graph causing unbounded recursion; Java 8 date or Optional types without the matching Jackson module.

Common situations: Adding JsonUtils-based logging to domain objects that were never serialized before; ORM/proxy objects whose getters hit a closed session; DTOs gaining a back-reference during a refactor.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/81ff73132ee293ea. Report an issue: GitHub.