I have the following:
public class ChargeRequest {
@Expose
private String customerName;
@Expose
private String stripeToken;
@Expose
private String plan;
@Expose
private String[] products;
gettersAndSetters()...
public Map<String, Object> toMap() {
return gson.fromJson(this, new TypeToken<Map<String, Object>>() {
}.getType());
}
public String toString() {
return gson.toJson(this, getClass());
}
}
I'm trying to convert ChargeRequest
into a Map<String, Object>
with Gson.
My adapter:
public static class JsonAdapter implements JsonDeserializer<ChargeRequest>{
@Override
public ChargeRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ChargeRequest cr = new ChargeRequest();
JsonObject o = json.getAsJsonObject();
o.add("customerName", o.get("customerName"));
o.add("stripeToken", o.get("stripeToken"));
o.add("plan", o.get("plan"));
JsonArray jProds = o.get("products").getAsJsonArray();
cr.products = new String[jProds.size()];
for (int i = 0; i < jProds.size(); i++) {
cr.products[i] = jProds.get(i).getAsString();
}
return cr;
}
}
I'm getting: Type information is unavailable, and the target is not a primitive
for the array of strings. what's wrong?
Final update: I've finally decided to move back to Jackson and everything works as expected.
Code:
ObjectMapper om = new ObjectMapper();
Map<String, Object> req = om.convertValue(request, Map.class);