groovy
  1. groovy
  2. GROOVY-5247

Sorting a map then passing it to JsonBuilder results in a NullPointerException only in 1.8.5

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.8.5
    • Fix Version/s: 1.8.6, 2.0-beta-3
    • Component/s: None
    • Labels:
      None
    • Environment:
      Mac OSX Lion
    • Number of attachments :
      0

      Description

      import groovy.json.*
      
      aMap = [a:1, b:2, c:3]
      new JsonBuilder(aMap) // works as expected
      
      aMap = [a:1, b:2, c:3].sort()
      new JsonBuilder(aMap) // fails with a NullPointerException in 1.8.5. Works as expected in 1.8.4
      

        Activity

        Hide
        John Wagenleitner added a comment - - edited

        I believe this issue may be related to commit c5456c808f0af5a. The sort() call returns a TreeMap and that is the root issue here, same error occurs for Hashtable (and Properties). A call to containsKey with a null key will throw a NPE for those Map implementations (unless used with a comparator that handles null keys).

        import groovy.json.*
        
        def m = new TreeMap()
        m.a = 1
        
        JsonOutput.toJson(m)
        
        Show
        John Wagenleitner added a comment - - edited I believe this issue may be related to commit c5456c808f0af5a . The sort() call returns a TreeMap and that is the root issue here, same error occurs for Hashtable (and Properties ). A call to containsKey with a null key will throw a NPE for those Map implementations (unless used with a comparator that handles null keys). import groovy.json.* def m = new TreeMap() m.a = 1 JsonOutput.toJson(m)
        Hide
        John Wagenleitner added a comment - - edited

        In case it might be useful, I opened a pull request for this issue at https://github.com/groovy/groovy-core/pull/12.

        Show
        John Wagenleitner added a comment - - edited In case it might be useful, I opened a pull request for this issue at https://github.com/groovy/groovy-core/pull/12 .
        Hide
        Guillaume Laforge added a comment -

        Thanks for the pull request!

        Show
        Guillaume Laforge added a comment - Thanks for the pull request!

          People

          • Assignee:
            Guillaume Laforge
            Reporter:
            François-Xavier Guillemette
          • Votes:
            4 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: