V: json parser not reporting syntax errors

Created on 24 Nov 2020  路  5Comments  路  Source: vlang/v

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?

Bug

Most helpful comment

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

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markgraydev picture markgraydev  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

ArcDrake picture ArcDrake  路  3Comments

vtereshkov picture vtereshkov  路  3Comments