Spring Framework
  1. Spring Framework
  2. SPR-9302

Use Jackson 2.0 for Jackson based json processing such as MappingJacksonJsonView

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Complete
    • Affects Version/s: 3.0.5
    • Fix Version/s: 3.2 M1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Due to recent release of Jackson 2.0, hope it is on the road map to give Spring user the option to use Jackson 2.0 for Jackson related json functions. Jackson 2.x has different package name for its classes to let code with 1.x be able to coexist. Maybe a new set of the Spring classes need to be created for the same reason for those who like to use the 2.x while others can remain on 1.x. For example MappingJacksonJsonView remains but with MappingJacksonJsonView2 for the 2.x Jackson. I believe someone has better idea how to get it done.

        Issue Links

          Activity

          Show
          Bruno Hansen added a comment - Support Jackson 2.0 http://forum.springsource.org/showthread.php?125213-Support-for-Jackson-2-0&p=408474#post408474
          Hide
          Keith Donald added a comment -

          I created a HttpMessageConverter implementation for Jackson 2.0 here: https://gist.github.com/2423129.

          The version includes support for "pretty printing" JSON, which is not easily configurable given how Spring MVC uses the Jackson ObjectMapper API to write JSON.

          Show
          Keith Donald added a comment - I created a HttpMessageConverter implementation for Jackson 2.0 here: https://gist.github.com/2423129 . The version includes support for "pretty printing" JSON, which is not easily configurable given how Spring MVC uses the Jackson ObjectMapper API to write JSON.
          Hide
          Dmitry Katsubo added a comment -

          Keith, original MappingJacksonHttpMessageConverter and MappingJackson2HttpMessageConverter you have proposed miss one feature: if SerializationConfig.Feature.INDENT_OUTPUT feature was enabled on ObjectMapper, it is ignored (see SPR-7201 for request).

          The solution direction is to use ObjectMapper.writeNNN() method, as only that guarantees that options are applied correctly (see ObjectMapper._configAndWriteValue() for more details).

          My understanding of the fix is attached as patch. However I can also fork from your branch & commit (if I won't get lost with git)...

          Show
          Dmitry Katsubo added a comment - Keith, original MappingJacksonHttpMessageConverter and MappingJackson2HttpMessageConverter you have proposed miss one feature: if SerializationConfig.Feature.INDENT_OUTPUT feature was enabled on ObjectMapper , it is ignored (see SPR-7201 for request). The solution direction is to use ObjectMapper.writeNNN() method, as only that guarantees that options are applied correctly (see ObjectMapper._configAndWriteValue() for more details). My understanding of the fix is attached as patch. However I can also fork from your branch & commit (if I won't get lost with git)...
          Hide
          Mike Yin added a comment - - edited

          Keith's MappingJackson2HttpMessageConverter appears to handle the INDENT_OUTPUT with a workaround.

          		// This is a workaround for the fact JsonGenerators created by ObjectMapper#getJsonFactory
          		// do not have ObjectMapper serialization features applied.
          		// See https://github.com/FasterXML/jackson-databind/issues/12
          		if (objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
          			jsonGenerator.useDefaultPrettyPrinter();
          		}
          

          I would say that your solution is definitely cleaner. My only concern is that Jackson has some optimizations for UTF-8 when it handles the encoding as opposed to being sent a Writer object. I'm not sure what the extent of the optimizations are.

          Show
          Mike Yin added a comment - - edited Keith's MappingJackson2HttpMessageConverter appears to handle the INDENT_OUTPUT with a workaround. // This is a workaround for the fact JsonGenerators created by ObjectMapper#getJsonFactory // do not have ObjectMapper serialization features applied. // See https://github.com/FasterXML/jackson-databind/issues/12 if (objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) { jsonGenerator.useDefaultPrettyPrinter(); } I would say that your solution is definitely cleaner. My only concern is that Jackson has some optimizations for UTF-8 when it handles the encoding as opposed to being sent a Writer object. I'm not sure what the extent of the optimizations are.
          Hide
          Dmitry Katsubo added a comment -

          Mike, indeed, I have overlooked this code. INDENT_OUTPUT should work. But for example SerializationConfig.Feature.CLOSE_CLOSEABLE won't work (and any other feature that needs special processing in ObjectMapper and will be added in the future).

          Indeed it looks like Jackson has two major generators: Utf8Generator and WriterBasedGenerator (what is the difference in performance?) The choice is speed vs bug-free.

          Show
          Dmitry Katsubo added a comment - Mike, indeed, I have overlooked this code. INDENT_OUTPUT should work. But for example SerializationConfig.Feature.CLOSE_CLOSEABLE won't work (and any other feature that needs special processing in ObjectMapper and will be added in the future). Indeed it looks like Jackson has two major generators: Utf8Generator and WriterBasedGenerator (what is the difference in performance?) The choice is speed vs bug-free.
          Hide
          Rossen Stoyanchev added a comment -
          Show
          Rossen Stoyanchev added a comment - See pull request comments: https://github.com/SpringSource/spring-framework/pull/76
          Hide
          Burkhard Graves added a comment -

          Hi, what about spring 3.1.2 (2nd maintenance release in the Spring 3.1.x line), will it also support jackson 2.0.x? Would be very nice!

          Show
          Burkhard Graves added a comment - Hi, what about spring 3.1.2 (2nd maintenance release in the Spring 3.1.x line), will it also support jackson 2.0.x? Would be very nice!
          Hide
          Chris Beams added a comment -

          Rossen, please create a Backport subtask for this if you see fit.

          Show
          Chris Beams added a comment - Rossen, please create a Backport subtask for this if you see fit.
          Hide
          Burkhard Graves added a comment -

          As I said, support for jackson 2.0.x would be nice in spring 3.1.2 - hard to wait until spring 3.2 ...

          Show
          Burkhard Graves added a comment - As I said, support for jackson 2.0.x would be nice in spring 3.1.2 - hard to wait until spring 3.2 ...

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Xun Wang
              Last updater:
              Rossen Stoyanchev
            • Votes:
              9 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 44 weeks, 3 days ago