I've written a few methods for convert by Gson library and java 1.8 .
thay are daynamic model for convert.
string to object
object to string
List to string
string to List
HashMap to String
String to JsonObj
//saeedmpt
public static String convertMapToString(Map<String, String> data) {
//convert Map to String
return new GsonBuilder().setPrettyPrinting().create().toJson(data);
}
public static <T> List<T> convertStringToList(String strListObj) {
//convert string json to object List
return new Gson().fromJson(strListObj, new TypeToken<List<Object>>() {
}.getType());
}
public static <T> T convertStringToObj(String strObj, Class<T> classOfT) {
//convert string json to object
return new Gson().fromJson(strObj, (Type) classOfT);
}
public static JsonObject convertStringToJsonObj(String strObj) {
//convert string json to object
return new Gson().fromJson(strObj, JsonObject.class);
}
public static <T> String convertListObjToString(List<T> listObj) {
//convert object list to string json for
return new Gson().toJson(listObj, new TypeToken<List<T>>() {
}.getType());
}
public static String convertObjToString(Object clsObj) {
//convert object to string json
String jsonSender = new Gson().toJson(clsObj, new TypeToken<Object>() {
}.getType());
return jsonSender;
}