Last active
October 9, 2015 15:07
-
-
Save szydan/3528052 to your computer and use it in GitHub Desktop.
Convert java Object to class instance using jackson
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
// where data is Object | |
ObjectMapper mapper = new ObjectMapper(); | |
MyClass myClass = mapper.readValue(mapper.writeValueAsString(data), ConsoleEvent.class); | |
// then to convert the object to json | |
mapper.writeValue(System.out, myClass); | |
// with pretty printng | |
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); | |
System.out.println(writer.writeValueAsString(myClass)); | |
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
<dependency> | |
<groupId>org.codehaus.jackson</groupId> | |
<artifactId>jackson-core-asl</artifactId> | |
<version>1.9.13</version> | |
</dependency> | |
<dependency> | |
<groupId>org.codehaus.jackson</groupId> | |
<artifactId>jackson-mapper-asl</artifactId> | |
<version>1.9.13</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment