Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Jackson from outputting pretty print JSON?

Tags:

java

json

jackson

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?

like image 623
tamuren Avatar asked Aug 07 '12 18:08

tamuren


People also ask

What is pretty print JSON?

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 .

How do I unescape a JSON string using Java Jackson?

out. println(StringEscapeUtils. unescapeJava(test)); This might help you.

How does Jackson convert object to JSON?

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.

What is the use of writerWithDefaultPrettyPrinter ()?

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.


1 Answers

Depending on what version of Spring you are using the MappingJacksonHttpMessageConverte‌​r 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.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 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

like image 151
Alex Avatar answered Oct 24 '22 20:10

Alex