v2 / vlib / x / json2 / tests / decode_array_test.v
49 lines · 40 sloc · 922 bytes · 2d33a7f2819dd5fc1f4aa3b3ca0bcc660810d7af
Raw
1import x.json2 as json
2
3struct StructType[T] {
4mut:
5 val T
6}
7
8fn test_array_of_strings() {
9 assert json.decode[[]int]('[1, 2, 3]')! == [1, 2, 3]
10
11 assert json.decode[[]string]('["a", "b", "c"]')! == ['a', 'b', 'c']
12
13 assert json.decode[[]bool]('[true, false, true]')! == [true, false, true]
14
15 assert json.decode[[]f64]('[1.1, 2.2, 3.3]')! == [1.1, 2.2, 3.3]
16
17 // nested arrays
18 assert json.decode[[][]int]('[[1, 22], [333, 4444]]')! == [
19 [1, 22],
20 [333, 4444],
21 ]
22
23 assert json.decode[[][]string]('[["a", "b"], ["c", "d"]]')! == [
24 ['a', 'b'],
25 ['c', 'd'],
26 ]
27
28 assert json.decode[[][]bool]('[[true, false], [false, true]]')! == [
29 [true, false],
30 [false, true],
31 ]
32}
33
34fn test_array_of_struct() {
35 assert json.decode[[]StructType[int]]('[{"val": 1}, {"val": 2}, {"val": 3}, {"val": 4}]')! == [
36 StructType{
37 val: 1
38 },
39 StructType{
40 val: 2
41 },
42 StructType{
43 val: 3
44 },
45 StructType{
46 val: 4
47 },
48 ]
49}
50