v
v copied to clipboard
Error when decoding JSON to a struct with struct type optional field
Describe the bug
When an optional field in a structure has a structure type and you try to decode JSON to it with json module - a C error happens. x.json2 works as expected.
Expected Behavior
No error, successful compilation and run
Current Behavior
/home/yuart/Projects/v/v -test-runner teamcity -stats test -run-only *.test_check_if_node_contains_all_data_from_ast_json /home/yuart/Projects/c2v/src/node_ast_parse_test.v
Testing started at 18:02 ...
==================
/tmp/v_1000/tsession_7f4da39c1740_38004950994763/node_ast_parse_test.8262242213286401653.tmp.c:6167: error: '{' expected (got ";")
...
==================
(Use `v -cg` to print the entire error message)
builder error:
==================
C error. This should never happen.
This is a compiler bug, please report it using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
Reproduction Steps
import json
struct Node {
location NodeLocation [json: 'loc']
}
struct NodeLocation {
source_file ?SourceFile [json: 'includedFrom']
}
struct SourceFile {
path string [json: 'file']
}
fn main() {
node := json.decode(Node, '{"loc": { "includedFrom": { "file": "/bin/foo" } } }')!
println(node)
}
This works
import x.json2
struct Node {
location NodeLocation [json: 'loc']
}
struct NodeLocation {
source_file ?SourceFile [json: 'includedFrom']
}
struct SourceFile {
path string [json: 'file']
}
fn main() {
node := json2.decode[Node]('{"loc": { "includedFrom": { "file": "/bin/foo" } } }')!
println(node)
}
Possible Solution
@felipensp
Additional Information/Context
No response
V version
V 0.3.3 d971d93.2879afa
Environment details (OS name and version, etc.)
OS: linux, "Garuda Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 3800X 8-Core Processor
CC version: cc (GCC) 12.2.1 20230201
getwd: /home/yuart
vmodules: /home/yuart/.vmodules
vroot: /home/yuart/Projects/v
vexe: /home/yuart/Projects/v/v
vexe mtime: 2023-02-22 16:38:40
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.3 d971d93.2879afa
Git version: git version 2.39.2
Git vroot status: weekly.2023.08-11-g2879afad
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
Can you post a complete short reproducible case?
import x.json2
struct Node {
location NodeLocation [json: 'loc']
}
struct NodeLocation {
source_file ?SourceFile [json: 'includedFrom']
}
struct SourceFile {
path string [json: 'file']
}
fn main() {
node := json2.decode[Node]('{"loc": { "includedFrom": { "file": "/bin/foo" } } }')!
println((node.location.source_file or {SourceFile{}}).path)
}
It prints (NIL), but not breaks.
Hmm, let me think
@felipensp oh, excuse me very much! I provide an entirely wrong reason for the bug. This is an issue with json module, which doesn't exist in x.json2
@felipensp oh, excuse me very much! I provide an entirely wrong reason for the bug. This is an issue with
jsonmodule, which doesn't exist inx.json2
Yeah, I noticed it. I'm checking the bug, it is not breaking right now, but it's not filling data.
I don't think we should fix json, while x.json2 will replace it entirely in the near feature