4

I have the following code:


    JsonElement deviceConfig = null;

    JsonObject status = getRestAPI().Connectivity().getDeviceStatus(device);

    deviceConfig = status.get("deviceConfig");

    if (deviceConfig == null || deviceConfig.isJsonNull()) {
        deviceConfig = status.get("mConfig");
    }

    if (deviceConfig != null && !deviceConfig.isJsonNull()) {
        if (!deviceConfig.getAsString().isEmpty()) {
            break;
        }
    }

For some reasons, I get the following error:

java.lang.UnsupportedOperationException: JsonObject at com.google.gson.JsonElement.getAsString(JsonElement.java:191)

In this line:

if (!deviceConfig.getAsString().isEmpty()) {

Any idea why I get this exception although I checked that the JSON is not null?

5
  • it means that getAsString() for that class is not implemented Commented Mar 12, 2020 at 8:03
  • @Stultuske How is it possible, I see it is implemented for GSON JsonElement Commented Mar 12, 2020 at 8:05
  • 1
    yes, and all the implementation does, is throw that Exception: github.com/google/gson/blob/master/gson/src/main/java/com/… Commented Mar 12, 2020 at 8:12
  • Assuming deviceConfig is a json object, Is it legit to do deviceConfig.getAsJsonObject.toString() to fix this issue? Commented Mar 12, 2020 at 8:14
  • It might. I don't know what exactly you expect as result. Commented Mar 12, 2020 at 8:15

1 Answer 1

3

JsonElement Source code: https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/JsonElement.java

The JsonElement class is an abstract class, it's meant to be used through subclasses that provide further implementations, for which the abstract class isn't concrete enough.

The getAsString method exists, yes, but is implemented like this:

  /**
   * convenience method to get this element as a string value.
   *
   * @return get this element as a string value.
   * @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid
   * string value.
   * @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
   * more than a single element.
   */
  public String getAsString() {
    throw new UnsupportedOperationException(getClass().getSimpleName());
  }

Which basically means: you are expected to provide an implementation in your subclass.

So, in order to get the result you desire, you'll need to cast the variable to your subclass before calling getAsString() on it.

Sign up to request clarification or add additional context in comments.

1 Comment

Why does the documentation say it throws ClassCastException or IllegalStateException and not that it throws the UnsupportedOperationException that can only be seen in the implementation. seems a bit confusing to me. Is it because the other 2 Exceptions can occur when trying to throw the UnsupportedOperationException ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.