parse_array Subroutine

private recursive subroutine parse_array(json, unit, str, array)

Core parsing routine.

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: json
integer(kind=IK), intent(in) :: unit

file unit number (if parsing from a file)

character(kind=CK,len=*), intent(in) :: str

JSON string (if parsing from a string)

type(json_value), pointer:: array

Calls

proc~~parse_array~~CallsGraph proc~parse_array parse_array proc~json_value_create json_value_create proc~parse_array->proc~json_value_create

Contents

Source Code


Source Code

    recursive subroutine parse_array(json, unit, str, array)

    implicit none

    class(json_core),intent(inout)      :: json
    integer(IK),intent(in)              :: unit   !! file unit number (if parsing from a file)
    character(kind=CK,len=*),intent(in) :: str    !! JSON string (if parsing from a string)
    type(json_value),pointer            :: array

    type(json_value),pointer :: element !! temp variable for array element
    logical(LK)              :: eof     !! end of file flag
    character(kind=CK,len=1) :: c       !! character returned by [[pop_char]]

    do

        if (json%exception_thrown) exit

        ! try to parse an element value
        nullify(element)
        call json_value_create(element)
        call json%parse_value(unit, str, element)
        if (json%exception_thrown) then
            if (associated(element)) call json%destroy(element)
            exit
        end if

        ! parse value will disassociate an empty array value
        if (associated(element)) call json%add(array, element)

        ! popped the next character
        call json%pop_char(unit, str=str, eof=eof, skip_ws=.true., &
                            skip_comments=json%allow_comments, popped=c)

        if (eof) then
            ! The file ended before array was finished:
            call json%throw_exception('Error in parse_array: '//&
                                 'End of file encountered when parsing an array.')
            exit
        else if (delimiter == c) then
            ! parse the next element
            cycle
        else if (end_array == c) then
            ! end of array
            exit
        else
            call json%throw_exception('Error in parse_array: '//&
                                      'Unexpected character encountered when parsing array.')
            exit
        end if

    end do

    end subroutine parse_array