I need to convert a json string to particular type based on the function's return type. The json string could also be a plain string. I am facing issues while converting the strings using gson.fromJson method.
Why is an exception thrown in gson.fromJson if the string contains a space? And how do I get around this?
import java.lang.reflect.Method;
import com.google.gson.Gson;
class Trial {
Object retu() {
return "BEVERLY OUTLAW";
}
}
public class sample {
public static void main (String args[]) {
Gson gson = new Gson();
Trial obj = new Trial();
Method func = obj.getClass().getDeclaredMethods()[0];
Object o = gson.fromJson("beverlyoutlaw", func.getGenericReturnType());
System.out.println(o); ---> this prints beverlyoutlaw
o = gson.fromJson("beverly outlaw", func.getGenericReturnType()); ---> This throws an exception!
System.out.println(o);
}
}