Skip to content

Instantly share code, notes, and snippets.

@dain
Created July 11, 2012 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dain/3092046 to your computer and use it in GitHub Desktop.
Save dain/3092046 to your computer and use it in GitHub Desktop.
Json pretty-print an object
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import static org.codehaus.jackson.map.SerializationConfig.Feature.INDENT_OUTPUT;
public class JsonPretty {
private static final ObjectMapper mapper = new ObjectMapper().configure(INDENT_OUTPUT, true);
public static String toPrettyJson(Object instance)
{
try {
return mapper.writeValueAsString(instance);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("%s could not be converted to json", instance.getClass().getName()), e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment