@@ -732,6 +732,7 @@ module json_value_module
732
732
procedure :: json_value_print
733
733
procedure :: string_to_int
734
734
procedure :: string_to_dble
735
+ procedure :: parse_end = > json_parse_end
735
736
procedure :: parse_value
736
737
procedure :: parse_number
737
738
procedure :: parse_string
@@ -8719,7 +8720,7 @@ subroutine json_parse_file(json, file, p, unit)
8719
8720
logical (LK) :: has_duplicate ! ! if checking for duplicate keys
8720
8721
character (kind= CK,len= :),allocatable :: path ! ! path to any duplicate key
8721
8722
8722
- ! clear any exceptions and initialize:
8723
+ ! clear any exceptions and initialize:
8723
8724
call json% initialize()
8724
8725
8725
8726
if ( present (unit) ) then
@@ -8731,7 +8732,7 @@ subroutine json_parse_file(json, file, p, unit)
8731
8732
8732
8733
iunit = unit
8733
8734
8734
- ! check to see if the file is already open
8735
+ ! check to see if the file is already open
8735
8736
! if it is, then use it, otherwise open the file with the name given.
8736
8737
inquire (unit= iunit, opened= is_open, iostat= istat)
8737
8738
if (istat== 0 .and. .not. is_open) then
@@ -8745,7 +8746,7 @@ subroutine json_parse_file(json, file, p, unit)
8745
8746
iostat = istat &
8746
8747
FILE_ENCODING )
8747
8748
else
8748
- ! if the file is already open, then we need to make sure
8749
+ ! if the file is already open, then we need to make sure
8749
8750
! that it is open with the correct form/access/etc...
8750
8751
end if
8751
8752
@@ -8779,6 +8780,7 @@ subroutine json_parse_file(json, file, p, unit)
8779
8780
8780
8781
! parse as a value
8781
8782
call json% parse_value(unit= iunit, str= CK_' ' , value= p)
8783
+ call json% parse_end(unit= iunit, str= CK_' ' )
8782
8784
8783
8785
! check for errors:
8784
8786
if (json% exception_thrown) then
@@ -8828,7 +8830,7 @@ subroutine json_parse_string(json, p, str)
8828
8830
logical (LK) :: has_duplicate ! ! if checking for duplicate keys
8829
8831
character (kind= CK,len= :),allocatable :: path ! ! path to any duplicate key
8830
8832
8831
- ! clear any exceptions and initialize:
8833
+ ! clear any exceptions and initialize:
8832
8834
call json% initialize()
8833
8835
8834
8836
! create the value and associate the pointer
@@ -8840,6 +8842,7 @@ subroutine json_parse_string(json, p, str)
8840
8842
8841
8843
! parse as a value
8842
8844
call json% parse_value(unit= iunit, str= str, value= p)
8845
+ call json% parse_end(unit= iunit, str= str)
8843
8846
8844
8847
if (json% exception_thrown) then
8845
8848
call json% annotate_invalid_json(iunit,str)
@@ -8858,6 +8861,41 @@ subroutine json_parse_string(json, p, str)
8858
8861
end subroutine json_parse_string
8859
8862
! *****************************************************************************************
8860
8863
8864
+ ! *****************************************************************************************
8865
+ ! >
8866
+ ! An error checking routine to call after a file (or string) has been parsed.
8867
+ ! It will throw an exception if there are any other non-whitespace characters
8868
+ ! in the file.
8869
+
8870
+ subroutine json_parse_end (json , unit , str )
8871
+
8872
+ implicit none
8873
+
8874
+ class(json_core),intent (inout ) :: json
8875
+ integer (IK),intent (in ) :: unit ! ! file unit number
8876
+ character (kind= CK,len=* ),intent (in ) :: str ! ! string containing JSON
8877
+ ! ! data (only used if `unit=0`)
8878
+
8879
+ logical (LK) :: eof ! ! end-of-file flag
8880
+ character (kind= CK,len= 1 ) :: c ! ! character read from file
8881
+ ! ! (or string) by [[pop_char]]
8882
+
8883
+ ! first check for exceptions:
8884
+ if (json% exception_thrown) return
8885
+
8886
+ ! pop the next non whitespace character off the file
8887
+ call json% pop_char(unit, str= str, eof= eof, skip_ws= .true. , &
8888
+ skip_comments= json% allow_comments, popped= c)
8889
+
8890
+ if (.not. eof) then
8891
+ call json% throw_exception(' Error in json_parse_end:' // &
8892
+ ' Unexpected character found after parsing value. "' // &
8893
+ c// ' "' )
8894
+ end if
8895
+
8896
+ end subroutine json_parse_end
8897
+ ! *****************************************************************************************
8898
+
8861
8899
! *****************************************************************************************
8862
8900
! >
8863
8901
! Alternate version of [[json_parse_string]], where `str` is kind=CDK.
0 commit comments