parse_array Subroutine

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

Core parsing routine.

Type Bound

json_core

Arguments

Type IntentOptional Attributes Name
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 json_core%parse_array none~add~2 json_core%add proc~parse_array->none~add~2 none~destroy~3 json_core%destroy proc~parse_array->none~destroy~3 none~throw_exception json_core%throw_exception proc~parse_array->none~throw_exception proc~json_value_create json_value_create proc~parse_array->proc~json_value_create proc~pop_char json_core%pop_char proc~parse_array->proc~pop_char proc~json_value_add_integer json_core%json_value_add_integer none~add~2->proc~json_value_add_integer proc~json_value_add_integer_vec json_core%json_value_add_integer_vec none~add~2->proc~json_value_add_integer_vec proc~json_value_add_logical json_core%json_value_add_logical none~add~2->proc~json_value_add_logical proc~json_value_add_logical_vec json_core%json_value_add_logical_vec none~add~2->proc~json_value_add_logical_vec proc~json_value_add_member json_core%json_value_add_member none~add~2->proc~json_value_add_member proc~json_value_add_null json_core%json_value_add_null none~add~2->proc~json_value_add_null proc~json_value_add_real json_core%json_value_add_real none~add~2->proc~json_value_add_real proc~json_value_add_real32 json_core%json_value_add_real32 none~add~2->proc~json_value_add_real32 proc~json_value_add_real32_vec json_core%json_value_add_real32_vec none~add~2->proc~json_value_add_real32_vec proc~json_value_add_real_vec json_core%json_value_add_real_vec none~add~2->proc~json_value_add_real_vec proc~json_value_add_string json_core%json_value_add_string none~add~2->proc~json_value_add_string proc~json_value_add_string_vec json_core%json_value_add_string_vec none~add~2->proc~json_value_add_string_vec proc~destroy_json_core json_core%destroy_json_core none~destroy~3->proc~destroy_json_core proc~json_value_destroy json_core%json_value_destroy none~destroy~3->proc~json_value_destroy proc~json_throw_exception json_core%json_throw_exception none~throw_exception->proc~json_throw_exception proc~json_value_add_integer->none~add~2 none~create_integer json_core%create_integer proc~json_value_add_integer->none~create_integer proc~json_value_add_integer_vec->none~add~2 none~create_array json_core%create_array proc~json_value_add_integer_vec->none~create_array proc~json_value_add_logical->none~add~2 none~create_logical json_core%create_logical proc~json_value_add_logical->none~create_logical proc~json_value_add_logical_vec->none~add~2 proc~json_value_add_logical_vec->none~create_array proc~json_value_add_member->none~throw_exception proc~json_value_add_null->none~add~2 none~create_null json_core%create_null proc~json_value_add_null->none~create_null proc~json_value_add_real->none~add~2 none~create_real~2 json_core%create_real proc~json_value_add_real->none~create_real~2 proc~json_value_add_real32->none~add~2 proc~json_value_add_real32_vec->none~add~2 proc~json_value_add_real_vec->none~add~2 proc~json_value_add_real_vec->none~create_array proc~json_value_add_string->none~add~2 none~create_string json_core%create_string proc~json_value_add_string->none~create_string proc~json_value_add_string_vec->none~add~2 proc~json_value_add_string_vec->none~create_array proc~json_value_destroy->none~destroy~3 proc~destroy_json_data destroy_json_data proc~json_value_destroy->proc~destroy_json_data proc~json_value_create_array json_core%json_value_create_array none~create_array->proc~json_value_create_array proc~json_value_create_integer json_core%json_value_create_integer none~create_integer->proc~json_value_create_integer proc~json_value_create_logical json_core%json_value_create_logical none~create_logical->proc~json_value_create_logical proc~json_value_create_null json_core%json_value_create_null none~create_null->proc~json_value_create_null proc~json_value_create_real32 json_core%json_value_create_real32 none~create_real~2->proc~json_value_create_real32 proc~json_value_create_string json_core%json_value_create_string none~create_string->proc~json_value_create_string proc~json_value_create_array->proc~json_value_create proc~to_array json_core%to_array proc~json_value_create_array->proc~to_array proc~json_value_create_integer->proc~json_value_create proc~to_integer json_core%to_integer proc~json_value_create_integer->proc~to_integer proc~json_value_create_logical->proc~json_value_create proc~to_logical json_core%to_logical proc~json_value_create_logical->proc~to_logical proc~json_value_create_null->proc~json_value_create proc~to_null json_core%to_null proc~json_value_create_null->proc~to_null proc~json_value_create_real32->none~create_real~2 proc~json_value_create_string->proc~json_value_create proc~to_string json_core%to_string proc~json_value_create_string->proc~to_string proc~to_array->proc~destroy_json_data proc~to_integer->proc~destroy_json_data proc~to_logical->proc~destroy_json_data proc~to_null->proc~destroy_json_data proc~to_string->proc~destroy_json_data

Called by

proc~~parse_array~~CalledByGraph proc~parse_array json_core%parse_array proc~parse_value_recursive parse_value_recursive proc~parse_value_recursive->proc~parse_array

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]]
    logical(LK) :: expecting_next_element !! to check for trailing delimiters

    expecting_next_element = .false.

    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 deallocate an empty array value
        if (associated(element)) then
            expecting_next_element = .false.
            call json%add(array, element)
        end if

        ! popped the next character
        call json%pop_char(unit, str=str, eof=eof, skip_ws=.true., &
                           skip_comments=json%allow_comments, c=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
            expecting_next_element = .true.
            cycle
        else if (end_array == c) then
            ! end of array
            if (expecting_next_element .and. .not. json%allow_trailing_comma) then
                ! this is a dangling comma.
                call json%throw_exception('Error in parse_array: '//&
                                          'Dangling comma when parsing an array.')
            end if
            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