crockpotveggies
crockpotveggies

Reputation: 13320

GSON JsonObject "Unsupported Operation Exception: null" getAsString

Running a Play! app with Scala. I'm doing a request where the response is expected to be a JSON string. When checking the debugger, the JsonElement returns OK with all information as expected. However, the problem is when I try to actually run methods on that JsonElement.

val json = WS.url("http://maps.googleapis.com/maps/api/geocode/json?callback=?&sensor=true&address=%s", startAddress+","+startCity+","+startProvince).get.getJson
    val geocoder = json.getAsString

The only error I get back is Unsupported Operation Exception: null and I've tried this on getAsString and getAsJsonObject and getAsJsonPrimitive

Any idea why it's failing on all methods? Thanks.

Upvotes: 46

Views: 55983

Answers (5)

lostintranslation
lostintranslation

Reputation: 24583

To add to @Henry's answer. In the spirit of Kotlins "OrNull" Adding an extension function:

fun JsonElement.asStringOrNull(): String? {
   return if (isJsonNull) null else asString
}

Upvotes: 0

lleclerc
lleclerc

Reputation: 693

I had a similar problem and I had to change jsonObject.getAsString() to jsonObject.toString();

Upvotes: 61

Hammond95
Hammond95

Reputation: 576

The class JsonElement will throw Unsupported Operation Exception for any getAs<Type> method, because it's an abstract class and makes sense that it is implemented in this way.

For some reason the class JsonObject, does not implement the getAs<Type> methods, so any call to one of these methods will throw an exception.

Calling the toString method on a JsonElement object, may solve your issue in certain circumstances, but isn't probably what you want because it returns the json representation as String (e.g. \"value\") in some cases.

I found out that also a JsonPrimitive class exists and it does implement the getAs<Type> methods. So probably the correct way to proceed is something like this:

    String input = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
    JsonParser parser = new JsonParser();
    JsonElement jsonTree = parser.parse(input);

    if(jsonTree != null && jsonTree.isJsonObject()) {
        JsonObject jsonObject = jsonTree.getAsJsonObject();
        value = jsonObject.get("key1").getAsJsonPrimitive().getAsString()
    }

PS. I removed all the nullability mgmt part. If you are coding in Java you probably want to manage this in a better way.

see GitHub source code for JsonElement: https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/JsonElement.java#L178

Upvotes: -1

Andy Petrella
Andy Petrella

Reputation: 4345

Maybe your JsonElement is a JsonNull

What you could do is to first check that it isn't by using json.isJsonNull

Otherwise, try to get its String representation with json.toString

Upvotes: 56

Henry
Henry

Reputation: 1049

In my case I just needed to get the element as an empty string if it is null, so I wrote a function like this:

private String getNullAsEmptyString(JsonElement jsonElement) {
        return jsonElement.isJsonNull() ? "" : jsonElement.getAsString();
    }

So instead of

val geocoder = json.getAsString

You can just use this

val geocoder = getNullAsEmptyString(json);

It returns "" if the element is null and the actual string if it is not

Upvotes: 12

Related Questions