json parser not reporting syntax errors
module mymodule
import json
struct ContactItem {
description string
telnr string
}
struct User {
name string
age int
// contact1 struct ContactItem [skip]
contact2 ContactItem
}
pub fn json_test() {
data := '{
"name": "Frodo",
"age": 25
"contact2": {
"description": "descr",
"telnr": "+32333"
}
}'
user := json.decode(User, data) or {
eprintln('Failed to decode json')
eprintln(err)
return
}
println(user)
}
v run test.v
Failed to decode json
"contact2": {
"description": "descr",
"telnr": "+32333"
}
}
- the [skip] didn't work, so commented
- when trying to decode this gives the error but not really clear why
it could well be ofcourse I just don't know how to do it
if this works do list of subobjects work as well?
The current json module is going to be replaced by x.json2 which is written in pure V. (current json module is based on a C wrapper)
You could try using that by doing import x.json2 as json
The new json module has some more details in it's README that might be of more help.
(.. and please note that it's work in progress)
@Larpon current json is fine, I've used it to parse literally millions of lines of complex json without problems.
This particular problem is due to a missing comma after "age": 25. Need to print the location of the error.
This particular problem is due to a missing comma after "age": 25. Need to print the location of the error.
With x.json2, the parser will give you an (atleast) helpful error message and position of the error included:
Failed to decode json
[json] unknown token 'contact2' when decoding object. (4:14)
Note: I've used the raw_decode function just to save the time for implementing the decoder/encoder since codegen for that is WIP
oeps that was really stupid, sorry for that
@despiegk typos happen, and it's easy to miss a comma in JSON. This should stay open until JSON errors are reported well.
I just realised that V actually printed everything after the position of the parsing error.
This runs now, but produces no output.
With the following code
import json
struct ContactItem {
description string
telnr string
}
struct User {
name string
age int
// contact1 struct ContactItem [skip]
contact2 ContactItem
}
pub fn json_test() {
data := '{
"name": "Frodo",
"age": 25,
"contact2": {
"description": "descr",
"telnr": "+32333"
}
}'
user := json.decode(User, data) or {
eprintln('Failed to decode json')
eprintln(err)
return
}
println(user)
}
fn main() {
json_test()
}
I have the following output in the console
User {
name: 'Frodo'
age: 25
contact2: ContactItem{
description: 'descr'
telnr: '+32333'
}
}
@despiegk is this was an expected output?
I will close the issue since the author didn't respond, and code produce expected output
Ah, I'm so dumb, forgive me. I read the thread one more time and realised where the problem is
WIth the last V (0.2.4; actual for 06.05.2022) error output is still unclear
Failed to decode json
"age": 25
"contact2":
indeed is just a reporting issue if there is a format issue
Failed to decode json
x.json2.UnknownTokenError: [x.json2] unknown token '[99, 111, 110, 116, 97, 99, 116, 50]' when decoding object. (13:17)
Would the following error be good enough? This is an easy fix.
x.json2.InvalidTokenError: [x.json2] invalid token
str_, expectingcomma(13:17)
Would the following error be good enough? This is an easy fix.
x.json2.InvalidTokenError: [x.json2] invalid token
str_, expectingcomma(13:17)
Looks better