diff --git a/vlib/x/json2/decoder.v b/vlib/x/json2/decoder.v index 024ba80ee4..d23d341178 100644 --- a/vlib/x/json2/decoder.v +++ b/vlib/x/json2/decoder.v @@ -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()! diff --git a/vlib/x/json2/tests/decoder_test.v b/vlib/x/json2/tests/decoder_test.v index 40d5659a45..8532302ae1 100644 --- a/vlib/x/json2/tests/decoder_test.v +++ b/vlib/x/json2/tests/decoder_test.v @@ -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 + } +}