json_traverse Subroutine

private subroutine json_traverse(json, p, traverse_callback)

Traverse a JSON structure. This routine calls the user-specified json_traverse_callback_func for each element of the structure.

Type Bound

json_core

Arguments

Type IntentOptional Attributes Name
class(json_core), intent(inout) :: json
type(json_value), intent(in), pointer :: p
procedure(json_traverse_callback_func) :: traverse_callback

Calls

proc~~json_traverse~~CallsGraph proc~json_traverse json_core%json_traverse none~throw_exception json_core%throw_exception proc~json_traverse->none~throw_exception proc~json_throw_exception json_core%json_throw_exception none~throw_exception->proc~json_throw_exception

Called by

proc~~json_traverse~~CalledByGraph proc~json_traverse json_core%json_traverse proc~json_check_all_for_duplicate_keys json_core%json_check_all_for_duplicate_keys proc~json_check_all_for_duplicate_keys->proc~json_traverse proc~json_file_traverse json_file%json_file_traverse proc~json_file_traverse->proc~json_traverse proc~json_value_is_child_of json_core%json_value_is_child_of proc~json_value_is_child_of->proc~json_traverse proc~json_parse_file json_core%json_parse_file proc~json_parse_file->proc~json_check_all_for_duplicate_keys proc~json_parse_string json_core%json_parse_string proc~json_parse_string->proc~json_check_all_for_duplicate_keys proc~json_value_swap json_core%json_value_swap proc~json_value_swap->proc~json_value_is_child_of proc~json_value_validate json_core%json_value_validate proc~json_value_validate->proc~json_check_all_for_duplicate_keys none~deserialize json_core%deserialize none~deserialize->proc~json_parse_string none~load json_core%load none~load->proc~json_parse_file proc~json_file_load json_file%json_file_load proc~json_file_load->none~load proc~json_file_load_from_string json_file%json_file_load_from_string proc~json_file_load_from_string->none~deserialize proc~wrap_json_parse_string wrap_json_parse_string proc~wrap_json_parse_string->none~deserialize none~deserialize~2 json_file%deserialize none~deserialize~2->proc~json_file_load_from_string proc~assign_string_to_json_file json_file%assign_string_to_json_file proc~assign_string_to_json_file->none~deserialize~2 proc~initialize_json_file_from_string initialize_json_file_from_string proc~initialize_json_file_from_string->none~deserialize~2 proc~initialize_json_file_from_string_v2 initialize_json_file_from_string_v2 proc~initialize_json_file_from_string_v2->none~deserialize~2 proc~wrap_json_file_load_from_string wrap_json_file_load_from_string proc~wrap_json_file_load_from_string->none~deserialize~2 interface~json_file json_file interface~json_file->proc~initialize_json_file_from_string interface~json_file->proc~initialize_json_file_from_string_v2 proc~wrap_assign_string_to_json_file wrap_assign_string_to_json_file proc~wrap_assign_string_to_json_file->proc~assign_string_to_json_file proc~wrap_initialize_json_file_from_string wrap_initialize_json_file_from_string proc~wrap_initialize_json_file_from_string->proc~initialize_json_file_from_string proc~wrap_initialize_json_file_from_string_v2 wrap_initialize_json_file_from_string_v2 proc~wrap_initialize_json_file_from_string_v2->proc~initialize_json_file_from_string_v2

Source Code

    subroutine json_traverse(json,p,traverse_callback)

    implicit none

    class(json_core),intent(inout)         :: json
    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

        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

            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)

        end if

        end subroutine traverse

    end subroutine json_traverse