Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=IK), | intent(in) | :: | iunit | file unit number |
||
character(kind=CK,len=*), | intent(in) | :: | str | string with JSON data |
Generate a warning message if there was an error parsing a JSON file or string.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
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 |
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