json_value_get_child_by_index Subroutine

private subroutine json_value_get_child_by_index(json, p, idx, child, found)

Returns a child in the object or array given the index.

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: json
type(json_value), intent(in), pointer:: p

object or array JSON data

integer(kind=IK), intent(in) :: idx

index of the child (this is a 1-based Fortran style array index).

type(json_value), pointer:: child

pointer to the child

logical(kind=LK), intent(out), optional :: found

true if the value was found (if not present, an exception will be thrown if it was not found. If present and not found, no exception will be thrown).


Contents


Source Code

    subroutine json_value_get_child_by_index(json, p, idx, child, found)

    implicit none

    class(json_core),intent(inout)      :: json
    type(json_value),pointer,intent(in) :: p      !! object or array JSON data
    integer(IK),intent(in)              :: idx    !! index of the child
                                                  !! (this is a 1-based Fortran
                                                  !! style array index).
    type(json_value),pointer            :: child  !! pointer to the child
    logical(LK),intent(out),optional    :: found  !! true if the value was found
                                                  !! (if not present, an exception
                                                  !! will be thrown if it was not
                                                  !! found.  If present and not
                                                  !! found, no exception will be
                                                  !! thrown).

    integer(IK) :: i  !! counter

    nullify(child)

    if (.not. json%exception_thrown) then

        if (associated(p%children)) then

            child => p%children

            do i = 1, idx - 1

                if (associated(child%next)) then
                    child => child%next
                else
                    call json%throw_exception('Error in json_value_get_child_by_index:'//&
                                              ' child%next is not associated.')
                    nullify(child)
                    exit
                end if

            end do

        else

            call json%throw_exception('Error in json_value_get_child_by_index:'//&
                                      ' p%children is not associated.')

        end if

        ! found output:
        if (json%exception_thrown) then
            if (present(found)) then
                call json%clear_exceptions()
                found = .false.
            end if
        else
            if (present(found)) found = .true.
        end if

    else
        if (present(found)) found = .false.
    end if

    end subroutine json_value_get_child_by_index