Last active
October 4, 2016 11:45
-
-
Save TheLittleNaruto/1746f5a2c06d7a1ef15a48b564d691a8 to your computer and use it in GitHub Desktop.
Copy one bean properties to another bean by using Gson lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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