Created
April 29, 2020 08:28
-
-
Save sourabh86/ea6668c137e389c19275d3d0d45888a3 to your computer and use it in GitHub Desktop.
Java method to deep clone any object with Jackson ObjectMapper
This file contains hidden or 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.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.IOException; | |
public static <T> T getClone(T objectToClone, Class<T> classType){ | |
T clonedObject = null; | |
ObjectMapper objectMapper = new ObjectMapper(); | |
try { | |
clonedObject = objectMapper | |
.readValue(objectMapper.writeValueAsString(objectToClone),classType); | |
} catch(IOException e) { | |
//LOG.error(e.getMessage()); | |
} | |
return clonedObject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment