v icon indicating copy to clipboard operation
v copied to clipboard

json parser not reporting syntax errors

Open despiegk opened this issue 5 years ago • 14 comments

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?

despiegk avatar Nov 24 '20 09:11 despiegk

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 avatar Nov 24 '20 10:11 larpon

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

medvednikov avatar Nov 24 '20 13:11 medvednikov

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

nedpals avatar Nov 24 '20 14:11 nedpals

oeps that was really stupid, sorry for that

despiegk avatar Nov 24 '20 15:11 despiegk

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

medvednikov avatar Nov 24 '20 16:11 medvednikov

This runs now, but produces no output.

Dialga avatar Oct 01 '21 15:10 Dialga

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?

ArtemkaKun avatar Feb 12 '22 16:02 ArtemkaKun

I will close the issue since the author didn't respond, and code produce expected output

ArtemkaKun avatar May 06 '22 18:05 ArtemkaKun

Ah, I'm so dumb, forgive me. I read the thread one more time and realised where the problem is

ArtemkaKun avatar May 06 '22 18:05 ArtemkaKun

WIth the last V (0.2.4; actual for 06.05.2022) error output is still unclear

Failed to decode json
"age": 25
"contact2":

ArtemkaKun avatar May 06 '22 18:05 ArtemkaKun

indeed is just a reporting issue if there is a format issue

despiegk avatar Jul 25 '22 04:07 despiegk

Failed to decode json
x.json2.UnknownTokenError: [x.json2] unknown token '[99, 111, 110, 116, 97, 99, 116, 50]' when decoding object. (13:17)

enghitalo avatar Dec 09 '22 20:12 enghitalo

Would the following error be good enough? This is an easy fix.

x.json2.InvalidTokenError: [x.json2] invalid token str_, expecting comma (13:17)

pierrec avatar Jan 20 '24 11:01 pierrec

Would the following error be good enough? This is an easy fix.

x.json2.InvalidTokenError: [x.json2] invalid token str_, expecting comma (13:17)

Looks better

enghitalo avatar Jan 20 '24 14:01 enghitalo