google/gson · error · UnsupportedOperationException

${getClass().getSimpleName()}

Error message

${getClass().getSimpleName()}

What it means

Base implementation of JsonElement.getAsBoolean() (JsonElement.java:224) throws UnsupportedOperationException(getClass().getSimpleName()); the message is just the runtime class name (e.g. 'JsonObject' or 'JsonNull'). JsonPrimitive and JsonArray override this method, so the throw only fires when called on a JsonObject or JsonNull. The method is meant to extract a boolean from a scalar.

Solutions

  1. Guard with element.isJsonPrimitive() (and null-check) before calling getAsBoolean()
  2. Handle JsonNull explicitly (e.g. map to a default or Optional.empty)
  3. Confirm the field's actual type in the payload
  4. Use a typed POJO with a Boolean (nullable) field via gson.fromJson

Example fix

// before
boolean active = root.get("active").getAsBoolean();
// after
JsonElement e = root.get("active");
boolean active = (e != null && e.isJsonPrimitive()) ? e.getAsBoolean() : false;
Defensive patterns

Strategy: type-guard

Validate before calling

JsonElement e = root.get("active");
boolean safe = (e != null && e.isJsonPrimitive());

Type guard

static boolean asBool(JsonElement e, boolean dflt) {
    return (e != null && e.isJsonPrimitive()) ? e.getAsBoolean() : dflt;
}

Try / catch

try {
    boolean b = element.getAsBoolean();
} catch (UnsupportedOperationException ex) {
    // ex.getMessage() is the class name, e.g. "JsonNull" or "JsonObject"
}

Prevention

When it happens

Trigger: Calling getAsBoolean() on a JsonObject or on a JsonNull. Common: root.get("flag").getAsBoolean() where 'flag' is explicitly JSON null, or where the wrong element (a container) was retrieved.

Common situations: A nullable boolean field is null in the payload and code chains .getAsBoolean() without a check; wrong tree node retrieved; schema mismatch where a boolean path is actually an object.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/105b627ae7251f05. Report an issue: GitHub.

Appendix: source

Thrown at gson/src/main/java/com/google/gson/JsonElement.java:224

  @CanIgnoreReturnValue // When this method is used only to verify that the value is JsonNull
  public JsonNull getAsJsonNull() {
    if (isJsonNull()) {
      return (JsonNull) this;
    }
    throw new IllegalStateException("Not a JSON Null: " + this);
  }

  /**
   * Convenience method to get this element as a boolean value.
   *
   * @return this element as a primitive boolean value.
   * @throws UnsupportedOperationException if this element is not a {@link JsonPrimitive} or {@link
   *     JsonArray}.
   * @throws IllegalStateException if this element is of the type {@link JsonArray} but contains
   *     more than a single element.
   */
  public boolean getAsBoolean() {
    throw new UnsupportedOperationException(getClass().getSimpleName());
  }

  /**
   * Convenience method to get this element as a {@link Number}.
   *
   * @return this element as a {@link Number}.
   * @throws UnsupportedOperationException if this element is not a {@link JsonPrimitive} or {@link
   *     JsonArray}, or cannot be converted to a number.
   * @throws IllegalStateException if this element is of the type {@link JsonArray} but contains
   *     more than a single element.
   */
  public Number getAsNumber() {
    throw new UnsupportedOperationException(getClass().getSimpleName());
  }

  /**
   * Convenience method to get this element as a string value.
   *

View on GitHub (pinned to 310ac341f2)