Tests the traversal of a JSON structure
It traverses the structure, looks for all "name" variables, and changes the name.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(out) | :: | error_cnt | report number of errors to caller |
subroutine test_14(error_cnt)
!! Tests the traversal of a JSON structure
!!
!! It traverses the structure, looks for all "name" variables, and changes the name.
implicit none
integer,intent(out) :: error_cnt !! report number of errors to caller
type(json_core) :: json
type(json_value),pointer :: p
type(json_file) :: f
write(error_unit,'(A)') ''
write(error_unit,'(A)') '================================='
write(error_unit,'(A)') ' TEST 14'
write(error_unit,'(A)') '================================='
write(error_unit,'(A)') ''
error_cnt = 0
icount = 0 !number of name changes (should be 2)
new_name = 'Fred' !change all names to this
call json%initialize() !initialize the module
call json%parse(dir//filename1,p) !read the file
if (json%failed()) then
call json%print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
call json%traverse(p,rename) !traverse all nodes in the structure
if (json%failed()) then
call json%print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
if (icount/=2) then
write(error_unit,'(A)') 'Error: should be 2 "name" variables in this file: '//filename1
error_cnt = error_cnt + 1
end if
if (error_cnt==0) then
write(error_unit,'(A)') ''
write(error_unit,'(A)') ' All names changed to '//new_name//':'
write(error_unit,'(A)') ''
call json%print(p,output_unit)
write(error_unit,'(A)') ''
end if
call json%destroy(p) !clean up
if (json%failed()) then
call json%print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
! now, test traversal from a json_file:
new_name = 'Bob'
icount = 0
call f%initialize()
call f%load_file(dir//filename1) !read the file
if (f%failed()) then
call f%print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
call f%traverse(rename) !traverse all nodes in the structure
if (f%failed()) then
call f%print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
if (icount/=2) then
write(error_unit,'(A)') 'Error: should be 2 "name" variables in this file: '//filename1
error_cnt = error_cnt + 1
end if
if (error_cnt==0) then
write(error_unit,'(A)') ''
write(error_unit,'(A)') ' All names changed to '//new_name//':'
write(error_unit,'(A)') ''
call f%print_file(output_unit)
write(error_unit,'(A)') ''
end if
call f%destroy() ! clean up
if (f%failed()) then
call f%print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
end subroutine test_14