v2 / vlib / x / json2 / tests / attributes_test.v
150 lines · 127 sloc · 3.65 KB · 9adeb6c49f0c64e33c0d810d48e5612b7ef4d6c3
Raw
1import x.json2 as json
2
3struct StruWithJsonAttribute {
4 a int
5 name2 string @[json: 'name']
6 b int
7}
8
9struct StruWithSkipAttribute {
10 a int
11 name ?string @[skip]
12 b int
13}
14
15struct StruWithJsonSkipAttribute {
16 a int
17 name ?string @[json: '-']
18 b int
19}
20
21struct StruWithOmitemptyAttribute {
22 a int
23 name ?string @[omitempty]
24 b int
25}
26
27struct StruWithRawAttribute {
28 a int
29 name string @[raw]
30 object string @[raw]
31 b int
32}
33
34struct StruWithOptionalRawAttribute {
35 data ?string @[raw]
36}
37
38struct StruWithRequiredAttribute {
39 a int
40 name string @[required]
41 skip_and_required ?string @[required; skip]
42 b int
43}
44
45struct Foo {
46 a int @[required]
47}
48
49struct JsonNullAttrBar {
50 name ?string @[json_null]
51}
52
53struct JsonNullAttrFoo {
54 name ?string @[json_null]
55 age ?int @[json_null]
56 text ?string
57 other ?JsonNullAttrBar
58 other2 ?JsonNullAttrBar @[json_null]
59}
60
61fn test_last_field_requiered() {
62 assert json.decode[Foo]('{"a":0}')! == Foo{
63 a: 0
64 }
65}
66
67fn test_json_null_attribute() {
68 assert json.encode(JsonNullAttrFoo{}) == '{"name":null,"age":null,"other2":null}'
69 assert json.encode(JsonNullAttrFoo{ name: '' }) == '{"name":"","age":null,"other2":null}'
70 assert json.encode(JsonNullAttrFoo{ age: 10 }) == '{"name":null,"age":10,"other2":null}'
71 assert json.encode(JsonNullAttrFoo{
72 age: 10
73 other2: JsonNullAttrBar{
74 name: none
75 }
76 }) == '{"name":null,"age":10,"other2":{"name":null}}'
77 assert json.decode[JsonNullAttrFoo](json.encode(JsonNullAttrFoo{}))! == JsonNullAttrFoo{}
78}
79
80fn test_skip_and_rename_attributes() {
81 assert json.decode[StruWithJsonAttribute]('{"name": "hola1", "a": 2, "b": 3}')! == StruWithJsonAttribute{
82 a: 2
83 name2: 'hola1'
84 b: 3
85 }, '`json` attribute not working'
86
87 assert json.decode[StruWithSkipAttribute]('{"name": "hola2", "a": 2, "b": 3}')! == StruWithSkipAttribute{
88 a: 2
89 name: none
90 b: 3
91 }, '`skip` attribute not working'
92
93 assert json.decode[StruWithJsonSkipAttribute]('{"name": "hola3", "a": 2, "b": 3}')! == StruWithJsonSkipAttribute{
94 a: 2
95 name: none
96 b: 3
97 }, " `json: '-'` skip attribute not working"
98
99 assert json.decode[StruWithOmitemptyAttribute]('{"name": "", "a": 2, "b": 3}')! == StruWithOmitemptyAttribute{
100 a: 2
101 name: none
102 b: 3
103 }, '`omitempty` attribute not working'
104
105 assert json.decode[StruWithOmitemptyAttribute]('{"name": "hola", "a": 2, "b": 3}')! == StruWithOmitemptyAttribute{
106 a: 2
107 name: 'hola'
108 b: 3
109 }, '`omitempty` attribute not working'
110}
111
112fn test_raw_attribute() {
113 assert json.decode[StruWithRawAttribute]('{"name": "hola", "a": 2, "object": {"c": 4, "d": 5}, "b": 3}')! == StruWithRawAttribute{
114 a: 2
115 name: '"hola"'
116 object: '{"c": 4, "d": 5}'
117 b: 3
118 }, '`raw` attribute not working'
119}
120
121fn test_optional_raw_attribute_preserves_source_bytes() {
122 raw_json := '{
123 "data": { "test": 1 }
124 }'
125 dto := json.decode[StruWithOptionalRawAttribute](raw_json)!
126 assert dto.data? == '{ "test": 1 }'
127}
128
129fn test_required_attribute() {
130 assert json.decode[StruWithRequiredAttribute]('{"name": "hola", "a": 2, "skip_and_required": "hola", "b": 3}')! == StruWithRequiredAttribute{
131 a: 2
132 name: 'hola'
133 skip_and_required: none
134 b: 3
135 }, '`required` attribute not working'
136
137 mut has_error := false
138
139 json.decode[StruWithRequiredAttribute]('{"name": "hola", "a": 2, "b": 3}') or {
140 if err is json.JsonDecodeError {
141 assert err.line == 1
142 assert err.character == 31
143 assert err.message == 'Data: missing required field `skip_and_required`'
144 }
145 has_error = true
146 }
147
148 assert has_error, '`required` attribute not working. It should have failed'
149 has_error = false
150}
151