v icon indicating copy to clipboard operation
v copied to clipboard

json2 is not decoding embedded structs properly

Open hungrybluedev opened this issue 1 year ago • 1 comments

V doctor:

V full version: V 0.4.9 7b9b3dd.c50d4ee
OS: macos, macOS, 15.1.1, 24B91
Processor: 10 cpus, 64bit, little endian, Apple M1 Pro

getwd: /Users/subhomoyhaldar/Documents/GitHub/VProjects/Consultation/VHamMLLTest
vexe: /Users/subhomoyhaldar/Documents/GitHub/v-main/v
vexe mtime: 2025-01-02 17:59:31

vroot: OK, value: /Users/subhomoyhaldar/Documents/GitHub/v-main
VMODULES: OK, value: /Users/subhomoyhaldar/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: weekly.2024.53-15-gc50d4ee1
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.6)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

What did you do? ./v -g -o vdbg cmd/v && ./vdbg src/main.v && src/main

import x.json2

pub struct Environment {
	vflags string
}

pub struct HistoryEvent {
	Environment
}

pub struct History {
pub mut:
	history_events []HistoryEvent
}

pub struct Classifier {
	History
}

pub fn main() {
	mut cl := Classifier{}
	cl.history_events = [HistoryEvent{}, HistoryEvent{}]
	dump(cl)
	s := json2.encode(cl)
	println(s)

	dcl := json2.decode[Classifier](s)!
	dump(dcl)
}

What did you see?

[src/main.v:23] cl: Classifier{
    History: History{
        history_events: [HistoryEvent{
            Environment: Environment{
                vflags: ''
            }
        }, HistoryEvent{
            Environment: Environment{
                vflags: ''
            }
        }]
    }
}
{"History":{"history_events":[{"Environment":{"vflags":""}},{"Environment":{"vflags":""}}]}}
[src/main.v:28] dcl: Classifier{
    History: History{
        history_events: []
    }
}

What did you expect to see?

Both the dump outputs should be the same. The encoded and decoded structs are different.

[!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.

Huly®: V_0.6-21785

hungrybluedev avatar Jan 03 '25 13:01 hungrybluedev

Currently decoding array of custom struct is only implemented on import x.json2.decoder2 as new_json, not x.json2.

felipensp avatar Jan 03 '25 14:01 felipensp