From aeadc0a12aafa4b6abf1748fb10fe4a6b0a5052b Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Tue, 12 Mar 2024 18:16:08 +0100 Subject: [PATCH] x.json2: improve error message upon missing comma (#20602) --- vlib/x/json2/decoder.v | 6 +++--- vlib/x/json2/tests/decoder_test.v | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/vlib/x/json2/decoder.v b/vlib/x/json2/decoder.v index 024ba80ee48810..d23d3411785e4d 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 40d5659a4574f4..8532302ae1467b 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 + } +}