Uploaded image for project: 'Johnzon'
  1. Johnzon
  2. JOHNZON-151

Serializing nulls within a list, within a type or map produces unexpected results

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5
    • 1.1.7
    • Mapper
    • None

    Description

      When a Collection (such as a List) contains a null entry and is serialized through a parent map or defined type, the serialization produces unexpected results.

      With 1.1.0 through 1.1.4, the nulls are silently ignored.
      With 1.1.5, a NullPointerException occurs instead.
      I did not check 1.0.0

      Local analysis indicates that in 1.1.5, code block

      MappingGeneratorImpl.java
      private void writeValue(final Class<?> type, ...
      ...
      if (objectConverterToUse == null) {
          objectConverterToUse = config.findObjectConverterWriter(o.getClass());
      }
      

      is the culprit by trying to invoke getClass() on a null object.

      Nulls are ignored in 1.1.4 and prior due to code blocks

      MappingGeneratorImpl.java
      private void writeItem(final Object o, final Collection<String> ignoredProperties, JsonPointerTracker jsonPointer) {
          if (!writePrimitives(o)) {
              if (Collection.class.isInstance(o)) {
      
      MappingGeneratorImpl.java
      private boolean writePrimitives(final Object value) {
          boolean handled = false;
          if (value == null) {
              return true; // fake a write
          }
      

      That is, the writeItem invokes writePrimitives and writePrimitives says it handled the null, when it in fact did nothing.

      Currently serializing null within a list only produces correct results when the list is the root object of serialization.
      Test cases attached; run mvn test to see include tests against 1.1.5

      I've had to include our own altered copy of MappingGeneratorImpl to the classpath (at a higher priority) to workaround the issue indicated here in our project.

      Attachments

        Activity

          People

            struberg Mark Struberg
            steven.walters Steven Walters
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: