2022-11-20 12:18:14 +03:00
|
|
|
import x.json2 as json
|
|
|
|
|
2022-12-05 17:32:15 +03:00
|
|
|
struct Result[T] {
|
2022-11-20 12:18:14 +03:00
|
|
|
ok bool
|
|
|
|
result T
|
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
id int
|
|
|
|
username string
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:32:15 +03:00
|
|
|
fn func[T]() !T {
|
2022-11-20 12:18:14 +03:00
|
|
|
text := '{"ok": true, "result":{"id":37467243, "username": "ciao"}}'
|
2022-12-05 17:32:15 +03:00
|
|
|
a := json.decode[Result[T]](text)!
|
2022-11-20 12:18:14 +03:00
|
|
|
return a.result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_decode_with_generic_struct() {
|
2022-12-05 17:32:15 +03:00
|
|
|
ret := func[User]()!
|
2022-11-20 12:18:14 +03:00
|
|
|
println(ret)
|
|
|
|
assert ret.id == 37467243
|
|
|
|
assert ret.username == 'ciao'
|
|
|
|
}
|