From 184f70e097fe72e1ac170daba1d76ff653129f3b Mon Sep 17 00:00:00 2001 From: BOURNE Emily EPFL Date: Mon, 5 Feb 2024 16:11:37 +0100 Subject: [PATCH 1/2] Make json_traverse directly recursive --- src/json_value_module.F90 | 71 ++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/src/json_value_module.F90 b/src/json_value_module.F90 index 4bae98a3e4..84be71306f 100644 --- a/src/json_value_module.F90 +++ b/src/json_value_module.F90 @@ -9530,7 +9530,7 @@ end subroutine json_get_array ! This routine calls the user-specified [[json_traverse_callback_func]] ! for each element of the structure. - subroutine json_traverse(json,p,traverse_callback) + recursive subroutine json_traverse(json,p,traverse_callback) implicit none @@ -9538,50 +9538,43 @@ subroutine json_traverse(json,p,traverse_callback) type(json_value),pointer,intent(in) :: p procedure(json_traverse_callback_func) :: traverse_callback - logical(LK) :: finished !! can be used to stop the process - - if (.not. json%exception_thrown) call traverse(p) - - contains - - recursive subroutine traverse(p) - - !! recursive [[json_value]] traversal. - - implicit none - - type(json_value),pointer,intent(in) :: p + type(json_value),pointer :: element !! a child element + integer(IK) :: i !! counter + integer(IK) :: icount !! number of children - type(json_value),pointer :: element !! a child element - integer(IK) :: i !! counter - integer(IK) :: icount !! number of children + logical(LK) :: finished !! can be used to stop the process - if (json%exception_thrown) return - call traverse_callback(json,p,finished) ! first call for this object - if (finished) return + if (json%exception_thrown) return - !for arrays and objects, have to also call for all children: - if (p%var_type==json_array .or. p%var_type==json_object) then - - icount = json%count(p) ! number of children - if (icount>0) then - element => p%children ! first one - do i = 1, icount ! call for each child - if (.not. associated(element)) then - call json%throw_exception('Error in json_traverse: '//& - 'Malformed JSON linked list') - return - end if - call traverse(element) - if (finished .or. json%exception_thrown) exit - element => element%next - end do - end if - nullify(element) + !! recursive [[json_value]] traversal. + if (json%exception_thrown) return + call traverse_callback(json,p,finished) ! first call for this object + if (finished) return + + !for arrays and objects, have to also call for all children: + if (p%var_type==json_array .or. p%var_type==json_object) then + + print *, loc(p), associated(p) + icount = json%count(p) ! number of children + print *, icount + if (icount>0) then + print *, icount, ">0" + element => p%children ! first one + do i = 1, icount ! call for each child + if (.not. associated(element)) then + call json%throw_exception('Error in json_traverse: '//& + 'Malformed JSON linked list') + return + end if + call json%traverse(element, traverse_callback) + if (finished .or. json%exception_thrown) exit + element => element%next + end do end if + nullify(element) - end subroutine traverse + end if end subroutine json_traverse !***************************************************************************************** From 7695f8c66af0cbddef67b017c02284294a341785 Mon Sep 17 00:00:00 2001 From: BOURNE Emily EPFL Date: Mon, 5 Feb 2024 16:20:21 +0100 Subject: [PATCH 2/2] Trigger CI test