Skip to content

Instantly share code, notes, and snippets.

@TheLittleNaruto
Last active October 4, 2016 11:45
Show Gist options
  • Save TheLittleNaruto/1746f5a2c06d7a1ef15a48b564d691a8 to your computer and use it in GitHub Desktop.
Save TheLittleNaruto/1746f5a2c06d7a1ef15a48b564d691a8 to your computer and use it in GitHub Desktop.
Copy one bean properties to another bean by using Gson lib
import com.google.gson.Gson;
import android.util.Log;
/**
* Created by Baby Naruto on 04-10-2016.
*/
public class CopyUtils {
private static final String TAG = CopyUtils.class.getName();
public static<E, T> T copyBean(E source, Class<T> destinationType) {
if (source != null) {
try {
Gson gson = new Gson();
String copyData = gson.toJson(source);
return gson.fromJson(copyData, destinationType);
} catch (Exception e) {
Log.e(TAG, "unable to parse; source object may not have the same properties as destination.");
}
} else {
Log.e(TAG, "unable to parse the object that is NULL");
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment