Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
4.0.0-alpha-2
-
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.