Skip to content

Commit

Permalink
x.json2: improve error message upon missing comma (#20602)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrec committed Mar 12, 2024
1 parent 365bd18 commit aeadc0a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions vlib/x/json2/decoder.v
Expand Up @@ -407,10 +407,10 @@ fn (mut p Parser) decode_object() !Any {
p.next_with_err()!
// step 3 -> value
fields[cur_key] = p.decode_value()!
if p.tok.kind != .comma && p.tok.kind != .rcbr {
return UnknownTokenError{
if p.tok.kind !in [.comma, .rcbr] {
return InvalidTokenError{
token: p.tok
kind: .object
expected: .comma
}
} else if p.tok.kind == .comma {
p.next_with_err()!
Expand Down
26 changes: 26 additions & 0 deletions vlib/x/json2/tests/decoder_test.v
Expand Up @@ -82,3 +82,29 @@ fn test_raw_decode_array_invalid() {
}
assert false
}

struct ContactItem {
description string
telnr string
}

struct User {
name string
age int
contact ContactItem
}

fn test_decode_missing_comma() {
data := '{
"name": "Frodo",
"age": 25
"contact": {
"description": "descr",
"telnr": "+32333"
}
}'
user := json.decode[User](data) or {
assert err.msg().contains('invalid token')
return
}
}

0 comments on commit aeadc0a

Please sign in to comment.