Skip to content

Commit 91578d4

Browse files
committed
added unit test for get_path routine.
1 parent f9d4ba0 commit 91578d4

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/tests/jf_test_1.f90

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ subroutine test_1(error_cnt)
6868
error_cnt = error_cnt + 1
6969
end if
7070

71+
! print each variable:
72+
write(error_unit,'(A)') ''
73+
write(error_unit,'(A)') 'printing each variable...'
74+
call core%initialize()
75+
call json%get(p) ! get root
76+
call core%traverse(p,print_json_variable) ! print all the variables
77+
7178
! extract data from the parsed value
7279
write(error_unit,'(A)') ''
7380
write(error_unit,'(A)') 'get some data from the file...'
@@ -276,6 +283,62 @@ subroutine test_1(error_cnt)
276283

277284
end subroutine test_1
278285

286+
subroutine print_json_variable(json,p,finished)
287+
288+
!! A `traverse` routine for printing out all
289+
!! the variables in a JSON structure.
290+
291+
implicit none
292+
293+
class(json_core),intent(inout) :: json
294+
type(json_value),pointer,intent(in) :: p
295+
logical(json_LK),intent(out) :: finished !! set true to stop traversing
296+
297+
character(kind=json_CK,len=:),allocatable :: path !! path to the variable
298+
logical(json_LK) :: found !! error flag
299+
type(json_value),pointer :: child !! variable's first child
300+
character(kind=json_CK,len=:),allocatable :: value !! variable value as a string
301+
integer(json_IK) :: var_type !! JSON variable type
302+
303+
call json%get_child(p,child)
304+
finished = .false.
305+
306+
!only print the leafs:
307+
if (.not. associated(child)) then
308+
call json%get_path(p,path,found,&
309+
use_alt_array_tokens=.true.,&
310+
path_sep=json_CK_'%') ! fortran-style
311+
if (found) then
312+
313+
call json%info(p,var_type=var_type)
314+
select case (var_type)
315+
case (json_array)
316+
!an empty array
317+
value = json_CK_'()'
318+
case (json_object)
319+
!an empty object
320+
value = json_CK_'{}'
321+
case default
322+
! get the value as a string
323+
! [assumes strict_type_checking=false]
324+
! note: strings are returned escaped without quotes
325+
call json%get(p,value)
326+
end select
327+
328+
!check for errors:
329+
if (json%failed()) then
330+
finished = .true.
331+
else
332+
write(output_unit,'(A)') path//json_CK_' = '//value
333+
end if
334+
335+
else
336+
finished = .true.
337+
end if
338+
end if
339+
340+
end subroutine print_json_variable
341+
279342
end module jf_test_1_mod
280343
!*****************************************************************************************
281344

0 commit comments

Comments
 (0)