Skip to content

x.json2 can't decode array of struct but json did it. #24409

Open
@changrui

Description

@changrui

Describe the bug

import x.json2
// import json

const json2_str = '[{"indicator":{"id":"NY.GDP.PCAP.CD","value":"GDP per capita (current US$)"},"country":{"id":"US","value":"United States"},"countryiso3code":"USA","date":"2024","value":null,"unit":"","obs_status":"","decimal":1},{"indicator":{"id":"NY.GDP.PCAP.CD","value":"GDP per capita (current US$)"},"country":{"id":"US","value":"United States"},"countryiso3code":"USA","date":"2023","value":82769.4122114216,"unit":"","obs_status":"","decimal":1},{"indicator":{"id":"NY.GDP.PCAP.CD","value":"GDP per capita (current US$)"},"country":{"id":"US","value":"United States"},"countryiso3code":"USA","date":"2022","value":78035.1753604212,"unit":"","obs_status":"","decimal":1}]'

fn decode_gdp_pc() ? {
	data := json2.decode[[]Data](json2_str) or { println('>>> Err: ${err}.') return none} //err
	// data := json.decode([]Data,json2_str) or { println('>>> Err: ${err}.') return none } //OK
	for k, v in data {
		println('${k}: ${v}')
	}
}

struct Data {
	indicator struct {id string value string}
	country struct {id string value string}
	iso3code string @[json: 'countryiso3code']
	date string
	value ?f64
	unit string
	obs_status string
	decimal int
}

Reproduction Steps

Run code above. https://play.vlang.io/p/7d9a69e82a

Expected Behavior

print out 3 struct.

Current Behavior

Err: The type []Data can't be decoded..

Possible Solution

No response

Additional Information/Context

No response

V version

V full version: V 0.4.10 b98ca31.27e4b02

Environment details (OS name and version, etc.)

windows 11 home.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.Modules: x.json2Bugs related to *only* x.json2 (the pure V implementation)Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Status: Has WorkaroundThis issue has workaround.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions