vendredi 12 janvier 2018

Getting Gson TypeToken from class name as a String

I want to do something like this - deserialize a class with generic type using Gson. The following code works like a charm.

Type type = new TypeToken<ActionTask<UptimeAction>>() {}.getType();

ActionTask task = (ActionTask) gson.fromJson(json, type);

But what if the type is provided as a String? I imagine something like the following.

String className = "UptimeAction";

Type type = ... // get the type somehow

ActionTask task = (ActionTask) gson.fromJson(json, type);

Is this possible at all?





Aucun commentaire:

Enregistrer un commentaire