| 1 | import json |
| 2 | |
| 3 | enum Foo { |
| 4 | yay @[json: 'A'; yay] |
| 5 | foo @[foo; json: 'B'] |
| 6 | } |
| 7 | |
| 8 | struct FooStruct { |
| 9 | item Foo |
| 10 | } |
| 11 | |
| 12 | fn test_comptime() { |
| 13 | $for f in Foo.values { |
| 14 | println(f) |
| 15 | if f.value == Foo.yay { |
| 16 | assert f.attrs[0] == "json: 'A'" |
| 17 | assert f.attrs[1] == 'yay' |
| 18 | } |
| 19 | if f.value == Foo.foo { |
| 20 | assert f.attrs[1] == "json: 'B'" |
| 21 | assert f.attrs[0] == 'foo' |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn test_json_encode() { |
| 27 | assert dump(json.encode(Foo.yay)) == '"A"' |
| 28 | assert dump(json.encode(Foo.foo)) == '"B"' |
| 29 | |
| 30 | assert dump(json.encode(FooStruct{ item: Foo.yay })) == '{"item":"A"}' |
| 31 | assert dump(json.encode(FooStruct{ item: Foo.foo })) == '{"item":"B"}' |
| 32 | } |
| 33 | |
| 34 | fn test_json_decode() { |
| 35 | dump(json.decode(FooStruct, '{"item": "A"}')!) |
| 36 | dump(json.decode(FooStruct, '{"item": "B"}')!) |
| 37 | } |
| 38 | |