@@ -14,6 +14,7 @@ module jf_test_14_mod
14
14
character (len=* ),parameter :: dir = ' ../files/inputs/' ! ! working directory
15
15
character (len=* ),parameter :: filename1 = ' test1.json' ! ! the file to read
16
16
integer :: icount = 0 ! ! a count of the number of "name" variables found
17
+ character (len= :),allocatable :: new_name ! ! name to change to
17
18
18
19
contains
19
20
@@ -29,6 +30,7 @@ subroutine test_14(error_cnt)
29
30
30
31
type (json_core) :: json
31
32
type (json_value),pointer :: p
33
+ type (json_file) :: f
32
34
33
35
write (error_unit,' (A)' ) ' '
34
36
write (error_unit,' (A)' ) ' ================================='
@@ -37,7 +39,9 @@ subroutine test_14(error_cnt)
37
39
write (error_unit,' (A)' ) ' '
38
40
39
41
error_cnt = 0
42
+
40
43
icount = 0 ! number of name changes (should be 2)
44
+ new_name = ' Fred' ! change all names to this
41
45
42
46
call json% initialize() ! initialize the module
43
47
@@ -60,7 +64,7 @@ subroutine test_14(error_cnt)
60
64
61
65
if (error_cnt== 0 ) then
62
66
write (error_unit,' (A)' ) ' '
63
- write (error_unit,' (A)' ) ' All names changed to Fred :'
67
+ write (error_unit,' (A)' ) ' All names changed to ' // new_name // ' :'
64
68
write (error_unit,' (A)' ) ' '
65
69
call json% print (p,output_unit)
66
70
write (error_unit,' (A)' ) ' '
@@ -72,6 +76,40 @@ subroutine test_14(error_cnt)
72
76
error_cnt = error_cnt + 1
73
77
end if
74
78
79
+ ! now, test traversal from a json_file:
80
+ new_name = ' Bob'
81
+ icount = 0
82
+ call f% initialize()
83
+ call f% load_file(dir// filename1) ! read the file
84
+ if (f% failed()) then
85
+ call f% print_error_message(error_unit)
86
+ error_cnt = error_cnt + 1
87
+ end if
88
+ call f% traverse(rename) ! traverse all nodes in the structure
89
+ if (f% failed()) then
90
+ call f% print_error_message(error_unit)
91
+ error_cnt = error_cnt + 1
92
+ end if
93
+
94
+ if (icount/= 2 ) then
95
+ write (error_unit,' (A)' ) ' Error: should be 2 "name" variables in this file: ' // filename1
96
+ error_cnt = error_cnt + 1
97
+ end if
98
+
99
+ if (error_cnt== 0 ) then
100
+ write (error_unit,' (A)' ) ' '
101
+ write (error_unit,' (A)' ) ' All names changed to ' // new_name// ' :'
102
+ write (error_unit,' (A)' ) ' '
103
+ call f% print_file(output_unit)
104
+ write (error_unit,' (A)' ) ' '
105
+ end if
106
+
107
+ call f% destroy() ! clean up
108
+ if (f% failed()) then
109
+ call f% print_error_message(error_unit)
110
+ error_cnt = error_cnt + 1
111
+ end if
112
+
75
113
end subroutine test_14
76
114
77
115
subroutine rename (json ,p ,finished ) ! ! change all "name" variable values to "Fred"
@@ -91,9 +129,9 @@ subroutine rename(json,p,finished) !! change all "name" variable values to "Fre
91
129
92
130
! it must be a string named "name":
93
131
if (var_type== json_string .and. str==' name' ) then
94
- call json% get(p,' @' ,str) ! get original name
95
- call json% update(p,' @' ,' Fred ' ,found) ! change it
96
- write (error_unit,' (A)' ) str// ' name changed'
132
+ call json% get(p,' @' ,str) ! get original name
133
+ call json% update(p,' @' ,new_name ,found) ! change it
134
+ write (error_unit,' (A)' ) str// ' name changed to ' // new_name
97
135
icount = icount + 1
98
136
end if
99
137
0 commit comments