1 | import json |
2 | |
3 | struct Result[T] { |
4 | ok bool |
5 | result T |
6 | } |
7 | |
8 | struct User { |
9 | id int |
10 | username string |
11 | } |
12 | |
13 | fn func[T]() !T { |
14 | text := '{"ok": true, "result":{"id":37467243, "username": "ciao"}}' |
15 | a := json.decode(Result[T], text)! |
16 | return a.result |
17 | } |
18 | |
19 | fn test_decode_with_generic_struct() { |
20 | ret := func[User]()! |
21 | println(ret) |
22 | assert ret.id == 37467243 |
23 | assert ret.username == 'ciao' |
24 | } |