2022-06-06 19:25:02 +03:00
|
|
|
import json
|
|
|
|
|
|
|
|
struct Result<T> {
|
|
|
|
ok bool
|
|
|
|
result T
|
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
id int
|
|
|
|
username string
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
fn func<T>() !T {
|
2022-06-06 19:25:02 +03:00
|
|
|
text := '{"ok": true, "result":{"id":37467243, "username": "ciao"}}'
|
2022-10-26 11:26:28 +03:00
|
|
|
a := json.decode(Result<T>, text)!
|
2022-06-06 19:25:02 +03:00
|
|
|
return a.result
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
fn test_decode_with_generic_struct() {
|
|
|
|
ret := func<User>()!
|
2022-06-06 19:25:02 +03:00
|
|
|
println(ret)
|
|
|
|
assert ret.id == 37467243
|
|
|
|
assert ret.username == 'ciao'
|
|
|
|
}
|