You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Yuki Kawata (Jira)" <ji...@apache.org> on 2021/02/25 05:12:00 UTC
[jira] [Created] (GROOVY-9954) JsonSlurper LAX parser skips the
first character after comments
Yuki Kawata created GROOVY-9954:
-----------------------------------
Summary: JsonSlurper LAX parser skips the first character after comments
Key: GROOVY-9954
URL: https://issues.apache.org/jira/browse/GROOVY-9954
Project: Groovy
Issue Type: Bug
Components: JSON
Affects Versions: 4.0.0-alpha-2
Reporter: Yuki Kawata
h2. Problem
JsonSlurper with JsonParseType.LAX skips the first character after comments.
This can cause unexpected parse results or even failures in some cases.
h3. Example 1
h4. Input
{code:groovy}
println new JsonSlurper().setType(JsonParserType.LAX).parseText("""
// comment
{
"flag": true,
"flag2": false
}
""")
{code}
h4. Expected result
{noformat}
[flag2:false, flag:true]
{noformat}
h4. Actual result
{noformat}
flag
{noformat}
h3. Example 2
h4. Input
{code:groovy}
println new JsonSlurper().setType(JsonParserType.LAX).parseText("""
{
/* comment */"flag": true,
"flag2": false
}
""")
{code}
h4. Expected result
{noformat}
[flag2:false, flag:true]
{noformat}
h4. Actual result
{noformat}
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
{noformat}
h2. 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.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)