1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix json decode with generic array of struct (fix #18300) (#18308)

This commit is contained in:
yuyi
2023-05-30 20:25:33 +08:00
committed by GitHub
parent ac6289301c
commit 4dc338863c
2 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,44 @@
import json
struct Response[T] {
time string
cars []T
}
struct Car {
brand string
power int
}
fn string_to_response[T](text string) {
result := json.decode([]Response[T], text) or { panic(err) }
println(result)
assert result.len == 2
assert result[0].time == '12ms'
assert result[0].cars[0].brand == 'Ferrari'
assert result[0].cars[0].power == 232
assert result[0].cars[1].brand == 'Lamborghini'
assert result[0].cars[1].power == 345
assert result[1].time == '12ms'
assert result[1].cars[0].brand == 'Ferrari'
assert result[1].cars[0].power == 232
assert result[1].cars[1].brand == 'Lamborghini'
assert result[1].cars[1].power == 345
}
fn test_json_decode_with_generic_array() {
text := '[
{ "time":"12ms",
"cars":[
{"brand":"Ferrari","power":232},
{"brand":"Lamborghini", "power":345}
]},
{ "time":"12ms",
"cars":[
{"brand":"Ferrari","power":232},
{"brand":"Lamborghini", "power":345}
]}
]'
string_to_response[Car](text)
}