I want to make sure that the JSON files generated by Jackson are never pretty print. I'm a junior working on a pre-existing project, so I need to work backwards to find out all the ways that JSON can be configured to output as pretty print. I can confirm there are 0 references to .defaultPrettyPrintingWriter() in the project, as well as 0 references to .setSerializationConfig, which I believe may also be used to enable pretty print.
So how else is this possible? Alternatively, is there a sure-fire way to ensure the JSON file is not pretty print?
Pretty printing is a form of stylistic formatting including indentation and colouring. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and for machines to parse and generate. The official Internet media type for JSON is application/json .
out. println(StringEscapeUtils. unescapeJava(test)); This might help you.
Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.
We can Pretty print JSON using the writerWithDefaultPrettyPrinter() of ObjectMapper class, it is a factory method for constructing ObjectWriter that will serialize objects using the default pretty printer for indentation.
Depending on what version of Spring you are using the MappingJacksonHttpMessageConverter
should have a boolean property named prettyPrint
to configure the printer when serializing JSON.
So this XML configuration should do the trick (if you are using a recent version of Spring 3)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<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 introducing the property. And the trunk version of the class too, that includes this property. Finally this is the Spring Jira issue SPR-7201 related to the previous commit.
Or you could try to update your version of Jackson to a more recent one that includes the useDefaultPrettyPrinter
and setPrettyPrinter
methods as mentioned by Alexander Ryzhov
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With