json_get_alloc_string_vec Subroutine

private subroutine json_get_alloc_string_vec(json, me, vec, ilen)

Get a string vector from a json_value. This is an alternate version of json_get_string_vec. This one returns an allocatable length character (where the string length is the maximum length of any element in the array). It also returns an integer array of the actual sizes of the strings in the JSON structure.

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: json
type(json_value), intent(in), pointer:: me
character(kind=CK,len=:), intent(out), dimension(:), allocatable:: vec
integer(kind=IK), intent(out), dimension(:), allocatable:: ilen

the actual length of each character string in the array


Contents


Source Code

    subroutine json_get_alloc_string_vec(json, me, vec, ilen)

    implicit none

    class(json_core),intent(inout)       :: json
    type(json_value),pointer,intent(in)  :: me
    character(kind=CK,len=:),dimension(:),allocatable,intent(out) :: vec
    integer(IK),dimension(:),allocatable,intent(out) :: ilen !! the actual length
                                                             !! of each character
                                                             !! string in the array

    logical(LK) :: initialized !! if the output array has been sized
    integer(IK) :: max_len     !! the length of the longest string in the array

    if ( json%exception_thrown ) return

    ! check for 0-length arrays first:
    select case (me%var_type)
    case (json_array)
        if (json%count(me)==0) then
            allocate(character(kind=CK,len=0) :: vec(0))
            allocate(ilen(0))
            return
        end if
    end select

    initialized = .false.

    call json%string_info(me,ilen=ilen,max_str_len=max_len)
    if (.not. json%exception_thrown) then
        ! now get each string using the callback function:
        call json%get(me, array_callback=get_chars_from_array)
    end if

    if (json%exception_thrown) then
        if (allocated(vec))  deallocate(vec)
        if (allocated(ilen)) deallocate(ilen)
    end if

    contains

        subroutine get_chars_from_array(json, element, i, count)

        !! callback function for chars

        implicit none

        class(json_core),intent(inout)      :: json
        type(json_value),pointer,intent(in) :: element
        integer(IK),intent(in)              :: i        !! index
        integer(IK),intent(in)              :: count    !! size of array

        character(kind=CK,len=:),allocatable :: cval  !! for getting string

        !size the output array:
        if (.not. initialized) then
            ! string length long enough to hold the longest one
            ! Note that this doesn't work with gfortran 4.9 or 5.
            allocate( character(kind=CK,len=max_len) :: vec(count) )
            initialized = .true.
        end if

        !populate the elements:
        call json%get(element, value=cval)
        if (allocated(cval)) then
            vec(i)  = cval
            ilen(i) = len(cval)  ! return the actual length
            deallocate(cval)
        else
            vec(i)  = CK_''
            ilen(i) = 0
        end if

        end subroutine get_chars_from_array

    end subroutine json_get_alloc_string_vec