Just encountered the same problem. You can write a trivial custom deserializer for the JsonElement
class:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
GsonBuilder gson_builder = new GsonBuilder();
gson_builder.registerTypeAdapter(
JsonElement.class,
new JsonDeserializer<JsonElement>() {
@Override
public JsonElement deserialize(JsonElement arg0,
Type arg1,
JsonDeserializationContext arg2)
throws JsonParseException {
return arg0;
}
} );
String str = "{ \"a\": \"A\", \"b\": true }";
Gson gson = gson_builder.create();
JsonElement element = gson.fromJson(str, JsonElement.class);
JsonObject object = element.getAsJsonObject();