x.json2: improve error message upon missing comma (#20602)

This commit is contained in:
Pierre Curto 2024-03-12 18:16:08 +01:00 committed by GitHub
parent 365bd18542
commit aeadc0a12a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 3 deletions

View File

@ -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()!

View File

@ -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
}
}