v / vlib / json / tests / json_is_null_attr_test.v
26 lines · 23 sloc · 613 bytes · 8cdb507bedad05bc516c470c6f8245a67023f8f9
Raw
1import json
2
3struct Bar {
4 name ?string @[json_null]
5}
6
7struct Foo {
8 name ?string @[json_null]
9 age ?int @[json_null]
10 text ?string
11 other ?Bar
12 other2 ?Bar @[json_null]
13}
14
15fn test_main() {
16 assert json.encode(Foo{}) == '{"name":null,"age":null,"other2":null}'
17 assert json.encode(Foo{ name: '' }) == '{"name":"","age":null,"other2":null}'
18 assert json.encode(Foo{ age: 10 }) == '{"name":null,"age":10,"other2":null}'
19 assert json.encode(Foo{
20 age: 10
21 other2: Bar{
22 name: none
23 }
24 }) == '{"name":null,"age":10,"other2":{"name":null}}'
25 assert json.decode(Foo, json.encode(Foo{}))! == Foo{}
26}
27