google/gson · error · UnsupportedOperationException

{getClass().getSimpleName()}

Error message

{getClass().getSimpleName()}

What it means

Default JsonElement.getAsBoolean() throws UnsupportedOperationException(getClass().getSimpleName()) so the exception message is the actual subclass name (e.g. 'JsonObject', 'JsonNull'). It is overridden only by JsonPrimitive and JsonArray; calling it on a JsonObject or JsonNull fails.

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 8b8628c656)

Solutions

  1. Guard with element.isJsonPrimitive() (and check the primitive's boolean-ness) before getAsBoolean().
  2. Handle isJsonNull() with a default boolean.
  3. Use element.getAsJsonPrimitive().getAsBoolean() only after a type check.
  4. Deserialize to a Boolean/boolean POJO field for stable schemas.

Example fix

// before
for (Map.Entry<String, JsonElement> e : obj.entrySet()) {
  boolean b = e.getValue().getAsBoolean(); // throws on null/object
}
// after
JsonElement v = e.getValue();
boolean b = v.isJsonPrimitive() ? v.getAsBoolean() : false;
Defensive patterns

Strategy: type-guard

Validate before calling

JsonElement v = el;
if (!v.isJsonPrimitive()) {
  throw new IllegalStateException("expected primitive boolean, got " + v.getClass().getSimpleName());
}
boolean b = v.getAsBoolean();

Type guard

boolean asBoolOrDefault(JsonElement e, boolean def) {
  return (e != null && e.isJsonPrimitive()) ? e.getAsBoolean() : def;
}

Try / catch

try {
  boolean b = el.getAsBoolean();
} catch (UnsupportedOperationException ex) {
  // message is subclass name; default / skip
}

Prevention

When it happens

Trigger: Calling jsonElement.getAsBoolean() (directly or via a generic loop) when the element is a JsonObject or JsonNull rather than a JsonPrimitive or single-element JsonArray.

Common situations: Iterating the values of a JsonObject and calling getAsBoolean() on each regardless of type; a field that is null where a boolean was expected; reading config that can be an object describing the flag.

Related errors


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/adba28d5a27297f0.json. Report an issue: GitHub.