2022-06-06 19:25:02 +03:00
|
|
|
import json
|
|
|
|
|
2022-11-26 19:23:26 +03:00
|
|
|
struct Result[T] {
|
2022-06-06 19:25:02 +03:00
|
|
|
ok bool
|
|
|
|
result T
|
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
id int
|
|
|
|
username string
|
|
|
|
}
|
|
|
|
|
2022-11-26 19:23:26 +03:00
|
|
|
fn func[T]() !T {
|
2022-06-06 19:25:02 +03:00
|
|
|
text := '{"ok": true, "result":{"id":37467243, "username": "ciao"}}'
|
2022-11-26 19:23:26 +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() {
|
2022-11-26 19:23:26 +03:00
|
|
|
ret := func[User]()!
|
2022-06-06 19:25:02 +03:00
|
|
|
println(ret)
|
|
|
|
assert ret.id == 37467243
|
|
|
|
assert ret.username == 'ciao'
|
|
|
|
}
|