Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json decode silently discards quoted numeric types #23424

Open
louis77 opened this issue Jan 10, 2025 · 4 comments
Open

json decode silently discards quoted numeric types #23424

louis77 opened this issue Jan 10, 2025 · 4 comments
Labels
Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Modules: x.json2 Bugs related to *only* x.json2 (the pure V implementation) Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: vlib Bugs/feature requests, that are related to the vlib.

Comments

@louis77
Copy link
Contributor

louis77 commented Jan 10, 2025

Describe the bug

The json parser silently discards numeric values which are quoted (which is sadly often encountered using external APIs). x.json2 properly casts these values into the desired numeric type.

This behaviour was already reported in #13996 but the issue was closed without a solution.

Reproduction Steps

module main

import json
import x.json2

struct Rating {
	name          string
	score         f64
	sources_count int
	reviews       int
}

fn main() {
	jsonstr := '{"name": "V Language", "score": "4.9", "sources_count": 6, "reviews": "9999"}'

	rating := json.decode(Rating, jsonstr)!

	println(rating)

	// Prints:
	//
	// Rating{
	// 	name: 'V Language'
	// 	score: 0.0
	// 	sources_count: 6
	//  reviews: 0
	// }

	rating2 := json2.decode[Rating](jsonstr)!
	println(rating2)

	// Prints:
	//
	// Rating{
	// 	name: 'V Language'
	// 	score: 4.9
	// 	sources_count: 6
	// 	reviews: 9999
	// }
}

Expected Behavior

I expect json.decode() to either fail with an error describing which field couldn't be parsed, or try casting the values like json2.decode() does.

Current Behavior

json silently ignores the fields, which can lead to uncaught bugs in applications, i.e. when external APIs change their schema.

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.9 3953445

Environment details (OS name and version, etc.)

V full version: V 0.4.9 3953445
OS: macos, macOS, 15.2, 24C101
Processor: 8 cpus, 64bit, little endian, Apple M2

vexe: /opt/homebrew/Cellar/vlang/0.4.9/libexec/v
vexe mtime: 2024-12-22 00:48:41

vroot: OK, value: /opt/homebrew/Cellar/vlang/0.4.9/libexec
VMODULES: OK, value: /Users/user/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: 4.4.15 (62319 commit(s) behind V master)
.git/config present: false

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc: N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@louis77 louis77 added the Bug This tag is applied to issues which reports bugs. label Jan 10, 2025
@spytheman spytheman added Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: vlib Bugs/feature requests, that are related to the vlib. labels Jan 10, 2025
@JalonSolov
Copy link
Contributor

JalonSolov commented Jan 10, 2025

According to the JSON spec, characters in quotes are strings, not numbers. If json is discarding them, that seems wrong, but understandable if it is supposed to be filling in a number field - strings aren't numbers.

If x.json2 is converting the strings to numbers, that could also be considered incorrect, compared to the JSON standard, but it is a lot more convenient.

What it comes down to, then, is that json is more strictly following the JSON spec, which some may prefer. x.json2 is handling things as some others prefer.

Truthfully, it should be an option in x.json2 whether to follow the spec strictly or not.

@enghitalo
Copy link
Contributor

I think json2 needs be fixed. json2.decoder2 is ok

@felipensp felipensp added the Modules: x.json2 Bugs related to *only* x.json2 (the pure V implementation) label Jan 11, 2025
@Delta456
Copy link
Member

Should we have a custom attribute like [decode_to_int]?

@JalonSolov
Copy link
Contributor

I'd rather see an option to the decode function in x.json2. By default, it should do what it's doing now. But if you want to see warnings/errors for out-of-spec stuff, an option could be passed to turn on strict mode. This could be done with a trailing params struct, with the default in the struct to be set to lenient mode (or passive, or whatever you want to call it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. Modules: x.json2 Bugs related to *only* x.json2 (the pure V implementation) Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: vlib Bugs/feature requests, that are related to the vlib.
Projects
None yet
Development

No branches or pull requests

6 participants