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

[x.json2] Decoding array type panics #21168

Closed
revosw opened this issue Apr 2, 2024 · 1 comment · Fixed by #21186
Closed

[x.json2] Decoding array type panics #21168

revosw opened this issue Apr 2, 2024 · 1 comment · Fixed by #21186
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.

Comments

@revosw
Copy link

revosw commented Apr 2, 2024

Describe the bug

When json decoding an array type, V panics

Reproduction Steps

import x.json2

pub struct Value {
	value string
}

fn main() {
	println(json2.decode[[]Value]('[{"value": "abc"}, {"value": "def"}]')!)
}

Expected Behavior

This is printed

[
  Value {
    value: "abc"
  },
  Value {
    value: "def"
  },
]

Current Behavior

================ V panic ================
   module: main
 function: main()
  message: The type `[]Value` can't be decoded.
     file: code.v:8
   v hash: a1b6360
=========================================
/tmp/v_60000/../../../../../../home/admin/v/vlib/builtin/builtin.c.v:66: at panic_debug: Backtrace
/tmp/v_60000/../../../../../../box/code.v:10: by main__main
/tmp/v_60000/../../../../../../tmp/v_60000/code.01HTEKB9M24DZTMKFASK5NH047.tmp.c:17052: by main
Exited with error status 1

Possible Solution

Handle array type as type argument

Additional Information/Context

No response

V version

V 0.4.5 a1b6360

Environment details (OS name and version, etc.)

V full version: V 0.4.5 4687f8c.a1b6360
OS: windows, Microsoft Windows 11 Pro v22631 64-bit
Processor: 12 cpus, 64bit, little endian,

getwd: C:\dev\v\winmd
vexe: C:\dev_tools\v\v.exe
vexe mtime: 2024-04-02 04:56:12

vroot: OK, value: C:\dev_tools\v
VMODULES: OK, value: C:\Users\simen.vmodules
VTMP: OK, value: C:\Users\simen\AppData\Local\Temp\v_0

Git version: git version 2.40.0.windows.1
Git vroot status: weekly.2024.13-70-ga1b6360d
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 a39eb79b

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.

@revosw revosw added the Bug This tag is applied to issues which reports bugs. label Apr 2, 2024
@ttytm
Copy link
Member

ttytm commented Apr 2, 2024

There are some limitations regarding the x.json2 module.
Putting it even simpler, this will not work as well atm:

println(json2.decode[[]string]('["value", "abc"]')!)

A primitive array will have to be wrapped inside an object as a (struct)field to be decoded. That'll work:

import x.json2

pub struct Foo {
	vals []string
}

fn main() {
	println(json2.decode[Foo]('{"vals": ["value"]}')!)
}

Trying to wrap a user defined type will give an empty array.

import x.json2

pub struct Value {
	value string
}

pub struct Field {
	vals []Value
}

fn main() {
	println(json2.decode[Field]('{"vals": [{"value": "abc"}]}')!)
}

So there definitely is open work. Until then, the regular json module might be the right tool.

@felipensp felipensp added the Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. label Apr 4, 2024
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants