v2 / vlib / x / json2 / tests / encode_embedded_structs_test.v
127 lines · 110 sloc · 2.63 KB · 715a2f8df0b00c7697c27d9eb16a9544e84b81b5
Raw
1import x.json2 as json
2
3struct Baz {
4 c bool = true
5}
6
7struct Foo {
8 Baz
9 a int = 1
10 b int = 2
11}
12
13struct Bar {
14 Foo
15 a string = '1'
16 c string = '3'
17}
18
19struct Environment {
20 vflags string
21}
22
23struct HistoryEvent {
24 Environment
25}
26
27struct History {
28pub mut:
29 history_events []HistoryEvent
30}
31
32struct Classifier {
33 History
34}
35
36struct RoundtripInner {
37 inner []f64
38}
39
40struct RoundtripOuter {
41 RoundtripInner
42 test f64
43}
44
45struct Article {
46 title string
47 description string
48}
49
50struct ArticlePlus {
51 Article
52 perex string
53}
54
55fn test_embed() {
56 assert json.encode(Bar{}) == '{"Foo.Baz.c":true,"Foo.a":1,"b":2,"a":"1","c":"3"}'
57 assert json.encode(Bar{}, prettify: true) == '{
58 "Foo.Baz.c": true,
59 "Foo.a": 1,
60 "b": 2,
61 "a": "1",
62 "c": "3"
63}'
64}
65
66fn test_decode_embed_with_prefixed_keys() {
67 decoded := json.decode[Bar]('{"a":"1","c":"3","Foo.a":4,"b":2,"Foo.Baz.c":false}')!
68 assert decoded.a == '1'
69 assert decoded.c == '3'
70 assert decoded.Foo.a == 4
71 assert decoded.Foo.b == 2
72 assert decoded.Foo.Baz.c == false
73}
74
75fn test_decode_embed_roundtrip_with_flattened_keys() {
76 mut classifier := Classifier{}
77 classifier.history_events = [
78 HistoryEvent{
79 Environment: Environment{
80 vflags: 'one'
81 }
82 },
83 HistoryEvent{
84 Environment: Environment{
85 vflags: 'two'
86 }
87 },
88 ]
89 encoded := json.encode(classifier)
90 assert encoded == '{"history_events":[{"vflags":"one"},{"vflags":"two"}]}'
91 decoded := json.decode[Classifier](encoded)!
92 assert decoded.history_events.len == 2
93 assert decoded.history_events[0].Environment.vflags == 'one'
94 assert decoded.history_events[1].Environment.vflags == 'two'
95}
96
97fn test_decode_embed_with_nested_objects() {
98 decoded :=
99 json.decode[Classifier]('{"History":{"history_events":[{"Environment":{"vflags":"legacy"}}]}}')!
100 assert decoded.history_events.len == 1
101 assert decoded.history_events[0].Environment.vflags == 'legacy'
102}
103
104fn test_embed_roundtrip_preserves_declaration_order() {
105 str := '{"inner":[1,2,3,4,5],"test":1.2}'
106 data := json.decode[RoundtripOuter](str)!
107 assert json.encode(data) == str
108}
109
110fn test_encode_array_of_embedded_structs_preserves_declaration_order() {
111 list_of_object := [
112 ArticlePlus{
113 title: 'One good title'
114 description: 'this is the first'
115 },
116 ArticlePlus{
117 title: 'Other good title'
118 description: 'more one'
119 },
120 ArticlePlus{
121 title: 'Other good title'
122 description: 'more one'
123 perex: 'good perex'
124 },
125 ]
126 assert json.encode(list_of_object) == '[{"title":"One good title","description":"this is the first","perex":""},{"title":"Other good title","description":"more one","perex":""},{"title":"Other good title","description":"more one","perex":"good perex"}]'
127}
128