groovy
  1. groovy
  2. GROOVY-5162

JsonOutput fails with uninformative NPE when map has null key

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8.4
    • Fix Version/s: 1.8.5, 2.0-beta-2
    • Component/s: groovy-jdk
    • Labels:
    • Environment:
      Mac Java 6
    • Number of attachments :
      0

      Description

      import groovy.json.JsonOutput
      
      def group = [1, 2, null].countBy{it}
      def result = use (JsonOutput) {
          group.toJson()
      }
      

      throws NullPointerException due to the presence of a null a as key to map entry.
      The stacktrace is rather unhelpful, so it would be nice if the JsonOutput would translate this to indicate a problem with the map content.
      http://groovyconsole.appspot.com/script/601001

        Activity

        Hide
        Roshan Dawrani added a comment -

        Just tried to see how Jackson JSON mapper handles the null key in a map and that rejects it saying "Null key for a Map not allowed in Json"

        Is it ok to have the same behavior in JsonOutput and reject the null key in a map?

        Show
        Roshan Dawrani added a comment - Just tried to see how Jackson JSON mapper handles the null key in a map and that rejects it saying "Null key for a Map not allowed in Json" Is it ok to have the same behavior in JsonOutput and reject the null key in a map?
        Hide
        John Bito added a comment -

        An IllegalArgumentException would be appropriate. It's a minor issue: the NPE is not entirely inappropriate, but it is certainly unhelpful.

        Show
        John Bito added a comment - An IllegalArgumentException would be appropriate. It's a minor issue: the NPE is not entirely inappropriate, but it is certainly unhelpful.
        Hide
        Roshan Dawrani added a comment -

        Correct, NPE is not so good. I just wanted to check if are in sync on rejecting null key in the map.

        Show
        Roshan Dawrani added a comment - Correct, NPE is not so good. I just wanted to check if are in sync on rejecting null key in the map.
        Hide
        Guillaume Laforge added a comment -

        According to the JSON spec, null is indeed not allowed as a key of an object.
        So JsonOutput should reject it.

        You want to have a stab at this issue Roshan?

        Show
        Guillaume Laforge added a comment - According to the JSON spec, null is indeed not allowed as a key of an object. So JsonOutput should reject it. You want to have a stab at this issue Roshan?
        Hide
        Roshan Dawrani added a comment -

        Yes Guillaume, I want to.

        Show
        Roshan Dawrani added a comment - Yes Guillaume, I want to.
        Hide
        Guillaume Laforge added a comment -

        Thank you Roshan!

        Show
        Guillaume Laforge added a comment - Thank you Roshan!

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            John Bito
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: