v

Зеркало из https://github.com/vlang/v
Форк
0
/x
/
decode_map_test.v 
60 строк · 1.1 Кб
1
module main
2

3
import x.json2
4

5
const data = '
6
{
7
  "comments": {
8
    "26788945": {
9
      "id": "26788945",
10
      "message": "some comment 1"
11
    },
12
    "26788946": {
13
      "id": "26788946",
14
      "message": "some comment 2"
15
    },
16
    "26788947": {
17
      "id": "26788947",
18
      "message": "some comment 3"
19
    }
20
  },
21
  "comments2": {
22
  	"26788945": true,
23
	"26788946": false,
24
	"26788947": true
25
  },
26
  "comments3": {
27
  	"26788945": 1,
28
	"26788946": 2,
29
	"26788947": 3
30
  }
31
}
32
'
33

34
struct Comment {
35
	id      string
36
	message string
37
}
38

39
struct Comments {
40
mut:
41
	comments  map[string]Comment
42
	comments2 map[string]bool
43
	comments3 map[string]int
44
}
45

46
fn test_main() {
47
	mut root := json2.decode[Comments](data)!
48
	assert root.comments.len == 3
49
	assert root.comments['26788945']!.id == '26788945'
50
	assert root.comments['26788946']!.id == '26788946'
51
	assert root.comments['26788947']!.id == '26788947'
52

53
	assert root.comments2['26788945']! == true
54
	assert root.comments2['26788946']! == false
55
	assert root.comments2['26788947']! == true
56

57
	assert root.comments3['26788945']! == 1
58
	assert root.comments3['26788946']! == 2
59
	assert root.comments3['26788947']! == 3
60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.