Generate a warning message if there was an error parsing a JSON file or string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(json_core), | intent(inout) | :: | json | |||
integer(kind=IK), | intent(in) | :: | iunit | file unit number |
||
character(kind=CK,len=*), | intent(in) | :: | str | string with JSON data |
subroutine annotate_invalid_json(json,iunit,str)
implicit none
class(json_core),intent(inout) :: json
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 (json%exception_thrown) then
!the counters for the current line and the last character read:
call integer_to_string(json%line_count, int_fmt, line_str)
call integer_to_string(json%char_count, int_fmt, char_str)
!draw the arrow string that points to the current character:
arrow_str = repeat('-',max( 0, json%char_count - 1) )//'^'
if (json%line_count>0 .and. json%char_count>0) then
if (iunit/=0) then
if (use_unformatted_stream) then
call json%get_current_line_from_file_stream(iunit,line)
else
call json%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,json%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 = CK_''
end if
!create the error message:
json%err_message = json%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