annotate_invalid_json Subroutine

private subroutine annotate_invalid_json(iunit, str)

Arguments

Type IntentOptional AttributesName
integer(kind=IK), intent(in) :: iunit

file unit number

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

string with JSON data

Description

Generate a warning message if there was an error parsing a JSON file or string.

Calls

proc~~annotate_invalid_json~~CallsGraph proc~annotate_invalid_json annotate_invalid_json proc~get_current_line_from_file_sequential get_current_line_from_file_sequential proc~annotate_invalid_json->proc~get_current_line_from_file_sequential proc~integer_to_string integer_to_string proc~annotate_invalid_json->proc~integer_to_string proc~get_current_line_from_file_stream get_current_line_from_file_stream proc~annotate_invalid_json->proc~get_current_line_from_file_stream
Help

Called By

proc~~annotate_invalid_json~~CalledByGraph proc~annotate_invalid_json annotate_invalid_json proc~json_parse_file json_parse_file proc~json_parse_file->proc~annotate_invalid_json proc~json_parse_string json_parse_string proc~json_parse_string->proc~annotate_invalid_json interface~json_parse json_parse interface~json_parse->proc~json_parse_file interface~json_parse->proc~json_parse_string proc~test_14 test_14 proc~test_14->interface~json_parse proc~test_8 test_8 proc~test_8->interface~json_parse proc~json_file_load json_file_load proc~json_file_load->interface~json_parse proc~json_file_load_from_string json_file_load_from_string proc~json_file_load_from_string->interface~json_parse program~jf_test_14 jf_test_14 program~jf_test_14->proc~test_14 program~jf_test_8 jf_test_8 program~jf_test_8->proc~test_8 proc~wrap_json_file_load_from_string wrap_json_file_load_from_string proc~wrap_json_file_load_from_string->proc~json_file_load_from_string proc~wrap_json_parse_string wrap_json_parse_string proc~wrap_json_parse_string->proc~json_parse_string
Help

Variables

TypeVisibility AttributesNameInitial
character(kind=CK,len=:), public, allocatable:: line
character(kind=CK,len=:), public, allocatable:: arrow_str
character(kind=CK,len=10), public :: line_str
character(kind=CK,len=10), public :: char_str
integer(kind=IK), public :: i
integer(kind=IK), public :: i_nl_prev
integer(kind=IK), public :: i_nl

Source Code

    subroutine annotate_invalid_json(iunit,str)

    implicit none

    integer(IK),intent(in) :: iunit             !! file unit number
    character(kind=CK,len=*),intent(in) :: str  !! string with JSON data

    character(kind=CK,len=:),allocatable :: line, arrow_str
    character(kind=CK,len=10) :: line_str, char_str
    integer(IK) :: i, i_nl_prev, i_nl

    !
    !  If there was an error reading the file, then
    !   print the line where the error occurred:
    !
    if (exception_thrown) then

        !the counters for the current line and the last character read:
        call integer_to_string(line_count, line_str)
        call integer_to_string(char_count, char_str)

        !draw the arrow string that points to the current character:
        arrow_str = repeat('-',max( 0, char_count - 1) )//'^'

        if (line_count>0 .and. char_count>0) then

            if (iunit/=0) then

                if (use_unformatted_stream) then
                    call get_current_line_from_file_stream(iunit,line)
                else
                    call get_current_line_from_file_sequential(iunit,line)
                end if

            else

                !get the current line from the string:
                ! [this is done by counting the newline characters]
                i_nl_prev = 0  !index of previous newline character
                i_nl = 2  !just in case line_count = 0
                do i=1,line_count
                    i_nl = index(str(i_nl_prev+1:),newline)
                    if (i_nl==0) then   !last line - no newline character
                        i_nl = len(str)+1
                        exit
                    end if
                    i_nl = i_nl + i_nl_prev   !index of current newline character
                    i_nl_prev = i_nl          !update for next iteration
                end do
                line = str(i_nl_prev+1 : i_nl-1)  !extract current line

            end if

        else
            !in this case, it was an empty line or file
            line = ''
        end if

        !create the error message:
        err_message = err_message//newline//&
                      'line: '//trim(adjustl(line_str))//', '//&
                      'character: '//trim(adjustl(char_str))//newline//&
                      trim(line)//newline//arrow_str

        if (allocated(line)) deallocate(line)

    end if

    end subroutine annotate_invalid_json