@@ -14,6 +14,7 @@ module jf_test_14_mod
1414 character (len=* ),parameter :: dir = ' ../files/inputs/' ! ! working directory
1515 character (len=* ),parameter :: filename1 = ' test1.json' ! ! the file to read
1616 integer :: icount = 0 ! ! a count of the number of "name" variables found
17+ character (len= :),allocatable :: new_name ! ! name to change to
1718
1819contains
1920
@@ -29,6 +30,7 @@ subroutine test_14(error_cnt)
2930
3031 type (json_core) :: json
3132 type (json_value),pointer :: p
33+ type (json_file) :: f
3234
3335 write (error_unit,' (A)' ) ' '
3436 write (error_unit,' (A)' ) ' ================================='
@@ -37,7 +39,9 @@ subroutine test_14(error_cnt)
3739 write (error_unit,' (A)' ) ' '
3840
3941 error_cnt = 0
42+
4043 icount = 0 ! number of name changes (should be 2)
44+ new_name = ' Fred' ! change all names to this
4145
4246 call json% initialize() ! initialize the module
4347
@@ -60,7 +64,7 @@ subroutine test_14(error_cnt)
6064
6165 if (error_cnt== 0 ) then
6266 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 // ' :'
6468 write (error_unit,' (A)' ) ' '
6569 call json% print (p,output_unit)
6670 write (error_unit,' (A)' ) ' '
@@ -72,6 +76,40 @@ subroutine test_14(error_cnt)
7276 error_cnt = error_cnt + 1
7377 end if
7478
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+
75113 end subroutine test_14
76114
77115 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
91129
92130 ! it must be a string named "name":
93131 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
97135 icount = icount + 1
98136 end if
99137
0 commit comments