eclipse-vertx/vert.x · error · DecodeException

Failed to decode:

Error message

Failed to decode:

What it means

JacksonCodec wraps any IOException raised while creating or driving a Jackson JsonParser over a Reader into a DecodeException with this message. It indicates the character stream being decoded could not be parsed as JSON. The original Jackson exception is preserved as the cause.

Source

Thrown at vertx-core/src/main/java/io/vertx/core/json/jackson/JacksonCodec.java:202

    try {
      JsonGenerator generator = createGenerator(out, false);
      generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
      generator.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
      encodeJson(object, generator);
      generator.close();
    } catch (IOException e) {
      throw new EncodeException(e.getMessage(), e);
    }
  }

  @Override
  public <T> T fromReader(Reader reader, Class<T> clazz) throws DecodeException {
    try {
      JsonParser parser = factory.createParser(reader);
      parser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
      return fromParser(parser, clazz);
    } catch (IOException e) {
      throw new DecodeException("Failed to decode:" + e.getMessage(), e);
    }
  }

  @Override
  public void toWriter(Object object, Writer writer) throws EncodeException {
    try {
      JsonGenerator generator = createGenerator(writer, false);
      generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
      generator.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
      encodeJson(object, generator);
      generator.close();
    } catch (IOException e) {
      throw new EncodeException(e.getMessage(), e);
    }
  }

  public static JsonParser createParser(String str) {
    try {

View on GitHub (pinned to fb308bd8c3)

Solutions

  1. Validate the JSON text before decoding
  2. Check DecodeException.getCause() for the exact Jackson parse error and location
  3. Ensure the Reader remains open and correctly encoded (UTF-8/UTF-16) until parsing finishes
  4. Handle partial reads from network/file sources before passing the Reader

Example fix

// before
MyDto dto = JacksonCodec.fromReader(reader, MyDto.class);
// after
try {
  MyDto dto = JacksonCodec.fromReader(reader, MyDto.class);
} catch (DecodeException e) {
  throw new IllegalArgumentException("Invalid JSON: " + e.getCause().getMessage(), e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Java
public static boolean looksLikeJson(Reader reader) throws IOException {
  reader.mark(1);
  int c = reader.read();
  reader.reset();
  return c == '{' || c == '[' || c == '"' || c == '-' || Character.isDigit(c);
}

Type guard

public static boolean isJsonStart(char c) {
  return c == '{' || c == '[' || c == '"' || c == '-' || Character.isDigit(c) || c == 't' || c == 'f' || c == 'n';
}

Try / catch

try {
  T value = JacksonCodec.fromReader(reader, T.class);
} catch (DecodeException e) {
  Throwable cause = e.getCause();
  logger.error("JSON decode from reader failed: {}", cause == null ? e.getMessage() : cause.getMessage());
  throw new IllegalArgumentException("Malformed JSON input", e);
}

Prevention

When it happens

Trigger: Calling JacksonCodec.fromReader(Reader, Class) with a Reader over malformed JSON, a closed Reader, invalid character encoding, or an I/O failure during parsing.

Common situations: Decoding JSON from a StringReader/FileReader containing truncated or syntactically invalid JSON; reading files with wrong charset; source writer closed before the reader finished.

Understand the failure class

Related errors


AI-assisted analysis of eclipse-vertx/vert.x@fb308bd8c3 (2026-09-06). Data as JSON: /api/errors/4e9744c55fe65d36. Report an issue: GitHub.