@@ -1917,7 +1917,7 @@ end subroutine wrap_json_throw_exception
19171917! * [[json_failed]]
19181918! * [[json_throw_exception]]
19191919
1920- pure subroutine json_check_for_errors (json ,status_ok ,error_msg )
1920+ subroutine json_check_for_errors (json ,status_ok ,error_msg )
19211921
19221922 implicit none
19231923
@@ -1927,14 +1927,23 @@ pure subroutine json_check_for_errors(json,status_ok,error_msg)
19271927 ! ! (not allocated if
19281928 ! ! there were no errors)
19291929
1930+ #if defined __GFORTRAN__
1931+ character (kind= CK,len= :),allocatable :: tmp ! ! workaround for gfortran bugs
1932+ #endif
1933+
19301934 if (present (status_ok)) status_ok = .not. json% exception_thrown
19311935
19321936 if (present (error_msg)) then
19331937 if (json% exception_thrown) then
19341938 ! if an exception has been thrown,
19351939 ! then this will always be allocated
19361940 ! [see json_throw_exception]
1941+ #if defined __GFORTRAN__
1942+ tmp = json% err_message
1943+ error_msg = tmp
1944+ #else
19371945 error_msg = json% err_message
1946+ #endif
19381947 end if
19391948 end if
19401949
@@ -8752,12 +8761,9 @@ subroutine json_parse_file(json, file, p, unit)
87528761 ! but we'll allocate something here just in case.
87538762 p% name = trim (file) ! use the file name
87548763
8755- ! parse as a value
8764+ ! parse as a value
87568765 call json% parse_value(unit= iunit, str= CK_' ' , value= p)
87578766
8758- ! close the file if necessary
8759- close (unit= iunit, iostat= istat)
8760-
87618767 ! check for errors:
87628768 if (json% exception_thrown) then
87638769 call json% annotate_invalid_json(iunit,CK_' ' )
@@ -8773,6 +8779,9 @@ subroutine json_parse_file(json, file, p, unit)
87738779 end if
87748780 end if
87758781
8782+ ! close the file:
8783+ close (unit= iunit, iostat= istat)
8784+
87768785 else
87778786
87788787 call json% throw_exception(' Error in json_parse_file: Error opening file: ' // trim (file))
@@ -8872,8 +8881,8 @@ subroutine annotate_invalid_json(json,iunit,str)
88728881 integer (IK) :: i_nl_prev ! ! index of previous newline character
88738882 integer (IK) :: i_nl ! ! index of current newline character
88748883
8875- ! If there was an error reading the file, then
8876- ! print the line where the error occurred:
8884+ ! If there was an error reading the file, then
8885+ ! print the line where the error occurred:
88778886 if (json% exception_thrown) then
88788887
88798888 ! the counters for the current line and the last character read:
@@ -8918,8 +8927,13 @@ subroutine annotate_invalid_json(json,iunit,str)
89188927 end if
89198928
89208929 ! create the error message:
8921- if (allocated (json% err_message)) json% err_message = json% err_message// newline
8922- json% err_message = ' line: ' // trim (adjustl (line_str))// ' , ' // &
8930+ if (allocated (json% err_message)) then
8931+ json% err_message = json% err_message// newline
8932+ else
8933+ json% err_message = ' '
8934+ end if
8935+ json% err_message = json% err_message// &
8936+ ' line: ' // trim (adjustl (line_str))// ' , ' // &
89238937 ' character: ' // trim (adjustl (char_str))// newline// &
89248938 trim (line)// newline// arrow_str
89258939
@@ -9883,6 +9897,7 @@ subroutine parse_string(json, unit, str, string)
98839897 ! ! if necessary)
98849898
98859899 logical (LK) :: eof ! ! end of file flag
9900+ logical (LK) :: escape ! ! for escape string parsing
98869901 character (kind= CK,len= 1 ) :: c ! ! character returned by [[pop_char]]
98879902 integer (IK) :: ip ! ! index to put next character,
98889903 ! ! to speed up by reducing the number
@@ -9895,7 +9910,8 @@ subroutine parse_string(json, unit, str, string)
98959910 if (.not. json% exception_thrown) then
98969911
98979912 ! initialize:
9898- ip = 1
9913+ escape = .false.
9914+ ip = 1
98999915
99009916 do
99019917
@@ -9907,7 +9923,7 @@ subroutine parse_string(json, unit, str, string)
99079923 call json% throw_exception(' Error in parse_string: Expecting end of string' )
99089924 return
99099925
9910- else if (c== quotation_mark) then ! end of string
9926+ else if (c== quotation_mark .and. .not. escape ) then ! end of string
99119927
99129928 exit
99139929
@@ -9920,6 +9936,15 @@ subroutine parse_string(json, unit, str, string)
99209936 string (ip:ip) = c
99219937 ip = ip + 1
99229938
9939+ ! check for escape character, so we don't
9940+ ! exit prematurely if escaping a quotation
9941+ ! character:
9942+ if (escape) then
9943+ escape = .false.
9944+ else
9945+ escape = (c== backslash)
9946+ end if
9947+
99239948 end if
99249949
99259950 end do
0 commit comments