| 1 | import json |
| 2 | import time |
| 3 | |
| 4 | const default_time = time.parse('2025-04-16 12:00:00') or {time.now()} |
| 5 | |
| 6 | type Any = string | []map[string]?Any | time.Time |
| 7 | |
| 8 | struct Users { |
| 9 | mut: |
| 10 | id string |
| 11 | created_at time.Time |
| 12 | } |
| 13 | |
| 14 | struct EmbeddedStruct { |
| 15 | v int |
| 16 | } |
| 17 | |
| 18 | struct MyStruct { |
| 19 | a int |
| 20 | opt_a ?int |
| 21 | s string |
| 22 | opt_s ?string |
| 23 | t time.Time = default_time |
| 24 | opt_v ?EmbeddedStruct |
| 25 | opt_t ?time.Time |
| 26 | opt_u64 ?u64 |
| 27 | } |
| 28 | |
| 29 | data_all := [Users{ |
| 30 | id: '123' |
| 31 | created_at: default_time |
| 32 | }, Users{ |
| 33 | id: 'asd' |
| 34 | created_at: default_time |
| 35 | }] |
| 36 | |
| 37 | mut result := []map[string]?Any{} |
| 38 | for raw in data_all { |
| 39 | mut data := map[string]?Any{} |
| 40 | data['id'] = raw.id |
| 41 | data['time'] = raw.created_at |
| 42 | result << data |
| 43 | } |
| 44 | |
| 45 | println(result) |
| 46 | |
| 47 | msg := json.encode(result) |
| 48 | println(msg) |
| 49 | |
| 50 | x := MyStruct{ |
| 51 | a: 1 |
| 52 | s: 'hello' |
| 53 | t: default_time |
| 54 | // opt_t : default_time |
| 55 | opt_u64: 123456 |
| 56 | } |
| 57 | |
| 58 | y := json.encode(x) |
| 59 | println(y) |
| 60 | |
| 61 | k := json.decode(MyStruct, y)! |
| 62 | println(k) |
| 63 | |