Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9954

JsonSlurper LAX parser skips the first character after comments

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 4.0.0-alpha-2
    • 3.0.8, 4.0.0-alpha-3
    • JSON
    • None

    Description

      Problem

      JsonSlurper with JsonParseType.LAX skips the first character after comments.
      This can cause unexpected parse results or even failures in some cases.

      Example 1

      Input

      println new JsonSlurper().setType(JsonParserType.LAX).parseText("""
      // comment
      {
        "flag": true,
        "flag2": false
      }
      """)
      

      Expected result

      [flag2:false, flag:true]
      

      Actual result

      flag
      

      Example 2

      Input

      println new JsonSlurper().setType(JsonParserType.LAX).parseText("""
      {
        /* comment */"flag": true,
        "flag2": false
      }
      """)
      

      Expected result

      [flag2:false, flag:true]
      

      Actual result

      The current character read is 'f' with an int value of 102
      expecting current character to be ':' but got 'f' with an int value of 102
      
      line number 4
      index number 35
        "flag2": false
      ...^
      groovy.json.JsonException: expecting current character to be ':' but got 'f' with an int value of 102
      

      Root cause

      JsonParserLax.handleComment() (and handleBashComment()) sets the index of the parsed string to the first character after the comment.
      This index is incremented by the for-loop before checking the next character.
      This can cause some unexpected behaviors if the skipped character is not a whitespace.

      Attachments

        Activity

          People

            Unassigned Unassigned
            ykawata Yuki Kawata
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h