Last active
December 28, 2017 08:56
-
-
Save ankushs92/e76d666bff46db98b0d6f304e1e5c928 to your computer and use it in GitHub Desktop.
Some Json utilities that I use for every project.Uses Jackson library.
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 java.io.IOException; | |
import com.fasterxml.jackson.core.JsonParseException; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class JsonUtils { | |
private static final ObjectMapper objMapper = new ObjectMapper(); | |
public static <T> T toObject(final String json , final Class<T> clazz) throws Exception{ | |
return objMapper.readValue(json,clazz); | |
} | |
public static String toJson(final Object object) throws JsonProcessingException{ | |
return objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment