How to prevent Jackson from displaying pretty printed JSON?

I want to make sure that the JSON files created by Jackson are never quite printed. I'm the youngest, working on an existing project, so I need to work in reverse order to find out all the ways that JSON can be configured to output in the form of a beautiful print. I can confirm that there are 0 references to .defaultPrettyPrintingWriter () in the project, as well as 0 references to .setSerializationConfig, which, I believe, can also be used to enable printing. A.

So how else is this possible? Also, is there a way to make sure the JSON file is not very printed?

+6
source share
3 answers

Depending on which version of Spring you are using MappingJacksonHttpMessageConverte‌​r , you must have a boolean property called prettyPrint to configure the printer when serializing JSON.

So this XML configuration should do the trick (if you are using the latest version of Spring 3)

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAda‌​pter"> <property name="messageConverters"> <list> <ref bean="jsonConverter" /> </list> </property> </bean> <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​r"> <property name="supportedMediaTypes" value="application/json" /> <property name="objectMapper" ref="jacksonObjectMapper" /> <property name="prettyPrint" value="false" /> </bean> 

You can see the related commit on github, representing the property. And the trunk version of the class that includes this property. Lastly, this is a Spring Jira SPR-7201 issue related to a previous commit.

Or you could try upgrading your version of Jackson to a later version that includes the useDefaultPrettyPrinter and setPrettyPrinter methods mentioned by Alexander Ryzhov

+2
source

The most elegant solution is to put the switch for beautiful / ugly in the filter and reuse static configuration objects. This prevents useless garbage collection.

 /** * Evaluate the "pretty" query parameter. If given without any value, or with "true": pretty JSON output. * Eg /test?pretty or /test?pretty=true * Otherwise output without any formatting. * * @see https://stackoverflow.com/questions/10532217/jax-rs-json-pretty-output */ @Provider @Produces(MediaType.APPLICATION_JSON) public class RestPrettyJsonFilter implements ContainerResponseFilter { public static final String QUERYPARAM_PRETTY = "pretty"; private static final IndentingModifier INDENTING_MODIFIER_PRETTY = new IndentingModifier(true); private static final IndentingModifier INDENTING_MODIFIER_NOT_PRETTY = new IndentingModifier(false); @Override public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException { boolean pretty = false; UriInfo uriInfo = reqCtx.getUriInfo(); //log.info("prettyFilter: "+uriInfo.getPath()); MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters(); if(queryParameters.containsKey(QUERYPARAM_PRETTY)) { // Pretty query parameter is present String prettyParam = queryParameters.getFirst(QUERYPARAM_PRETTY); // Pretty is present without any value, or value is set to "true"? if (prettyParam == null || "".equals(prettyParam) || "true".equals(prettyParam)) { pretty = true; } } // Prevent recreation of objects over and over again //ObjectWriterInjector.set(new IndentingModifier(pretty)); if (pretty) { ObjectWriterInjector.set(INDENTING_MODIFIER_PRETTY); } else { ObjectWriterInjector.set(INDENTING_MODIFIER_NOT_PRETTY); } } /** * Used to switch on / off pretty output for each response. */ public static class IndentingModifier extends ObjectWriterModifier { private final boolean indent; /** Minimal pretty printer is not printing pretty. */ private final static PrettyPrinter NOT_PRETTY_PRINTER = new com.fasterxml.jackson.core.util.MinimalPrettyPrinter(); public IndentingModifier(boolean indent) { this.indent = indent; } /* (non-Javadoc) * @see com.fasterxml.jackson.jaxrs.cfg.ObjectWriterModifier#modify(com.fasterxml.jackson.jaxrs.cfg.EndpointConfigBase, javax.ws.rs.core.MultivaluedMap, java.lang.Object, com.fasterxml.jackson.databind.ObjectWriter, com.fasterxml.jackson.core.JsonGenerator) */ @Override public ObjectWriter modify(EndpointConfigBase<?> endpointConfigBase, MultivaluedMap<String, Object> multivaluedMap, Object o, ObjectWriter objectWriter, JsonGenerator jsonGenerator) throws IOException { if(indent) { // Pretty jsonGenerator.useDefaultPrettyPrinter(); } else { // Not pretty jsonGenerator.setPrettyPrinter(NOT_PRETTY_PRINTER); } return objectWriter; } } } 
+2
source

You do not know which version of Jackson you are using, but in the latest version (1.9.10), the default behavior of JsonGenerator is not quite sad. The easiest way to enable it is to call generator.useDefaultPrettyPrinter() or generator.setPrettyPrinter(new DefaultPrettyPrinter()) . Try to find useDefaultPrettyPrinter and setPrettyPrinter and delete these instructions.

0
source

Source: https://habr.com/ru/post/922312/


All Articles