v2 / vlib / v / gen / c / testdata / json_option_time.vv
62 lines · 50 sloc · 929 bytes · dead5e69b44b4f8c4999ac82b730c21250cb0583
Raw
1import json
2import time
3
4const default_time = time.parse('2025-04-16 12:00:00') or {time.now()}
5
6type Any = string | []map[string]?Any | time.Time
7
8struct Users {
9mut:
10 id string
11 created_at time.Time
12}
13
14struct EmbeddedStruct {
15 v int
16}
17
18struct 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
29data_all := [Users{
30 id: '123'
31 created_at: default_time
32}, Users{
33 id: 'asd'
34 created_at: default_time
35}]
36
37mut result := []map[string]?Any{}
38for 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
45println(result)
46
47msg := json.encode(result)
48println(msg)
49
50x := MyStruct{
51 a: 1
52 s: 'hello'
53 t: default_time
54 // opt_t : default_time
55 opt_u64: 123456
56}
57
58y := json.encode(x)
59println(y)
60
61k := json.decode(MyStruct, y)!
62println(k)
63