Skip to content

Instantly share code, notes, and snippets.

@ankushs92
Last active December 28, 2017 08:56
Show Gist options
  • Save ankushs92/e76d666bff46db98b0d6f304e1e5c928 to your computer and use it in GitHub Desktop.
Save ankushs92/e76d666bff46db98b0d6f304e1e5c928 to your computer and use it in GitHub Desktop.
Some Json utilities that I use for every project.Uses Jackson library.
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