Skip to content

json2.decode silently drops decoding all map values other than string #18734

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

Open
einar-hjortdal opened this issue Jul 1, 2023 · 3 comments
Open
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@einar-hjortdal
Copy link
Contributor

Describe the bug

json2.encode on map[string]Any outputs a properly formatted json. Calling json2.decode on this string quietly removes all integers in the resulting map[string]Any

Problem illustrated here

payload: {"sub":"453636","aud":"Coachonko","iss":"Coachonko","nbf":1688229809,"iat":1688229809,"jti":"e0a26a81-9cc0-4f52-b672-4f6f3d3b44a5","exp":1688273009}
decoded payload: {"sub":"453636","aud":"Coachonko","iss":"Coachonko","jti":"e0a26a81-9cc0-4f52-b672-4f6f3d3b44a5"}

Expected Behavior

No loss of data going from json2.encode to json2.decode

Current Behavior

All integers disappear

Reproduction Steps

Please see linked repo

Possible Solution

No response

Additional Information/Context

No response

V version

Current V version: V 0.4.0 049d685, timestamp: 2023-07-01 14:07:51 +0300

Environment details (OS name and version, etc.)

V full version: V 0.4.0 ee4150f.049d685
OS: linux, Linux version 5.14.0-284.18.1.el9_2.x86_64 (mockbuild@euro.kojibuilder101) (gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4), GNU ld version 2.35.2-37.el9) #1 SMP PREEMPT_DYNAMIC Fri Jun 23 16:35:13 CEST 2023
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-5005U CPU @ 2.00GHz

getwd: /home/coachonko/Documents/projects/vlang/sessions
vexe: /home/coachonko/.local/lib64/v/v
vexe mtime: 2023-07-01 12:37:03

vroot: OK, value: /home/coachonko/.local/lib64/v
VMODULES: OK, value: /home/coachonko/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.39.3
Git vroot status: 0.4-2-g049d6853
.git/config present: true

CC version: cc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
@einar-hjortdal einar-hjortdal added the Bug This tag is applied to issues which reports bugs. label Jul 1, 2023
@SurmanPP
Copy link
Contributor

SurmanPP commented Jul 1, 2023

json2 seems to not support decoding anything other than strings as map values. See here:

v/vlib/x/json2/json2.v

Lines 134 to 148 in 049d685

} $else $if T is $map {
for k, v in res {
// // TODO - make this work to decode types like `map[string]StructType[bool]`
// $if typeof(typ[k]).idx is string {
// typ[k] = v.str()
// } $else $if typeof(typ[k]).idx is $struct {
// }
match v {
string {
typ[k] = v.str()
}
else {}
}
}

@SurmanPP
Copy link
Contributor

SurmanPP commented Jul 1, 2023

Maybe rename the issue to something like: json2.decode silently drops decoding all other map values than string.

@SurmanPP
Copy link
Contributor

SurmanPP commented Jul 1, 2023

If you want to decode a map of Any from json you can just use json2.raw_decode it returns an !json2.Any type but does not seem to guarantee that a its a map[string]Any you would have to check first. This workaround should also make decoding a little bit faster.

@einar-hjortdal einar-hjortdal changed the title json2.decode function silently fails to decode integers in a map[string]Any json2.decode silently drops decoding all map values other than string Jul 1, 2023
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.
Projects
None yet
Development

No branches or pull requests

2 participants