@@ -726,6 +726,7 @@ module json_value_module
726
726
procedure :: json_value_print
727
727
procedure :: string_to_int
728
728
procedure :: string_to_dble
729
+ procedure :: parse_end = > json_parse_end
729
730
procedure :: parse_value
730
731
procedure :: parse_number
731
732
procedure :: parse_string
@@ -8708,7 +8709,7 @@ subroutine json_parse_file(json, file, p, unit)
8708
8709
logical (LK) :: has_duplicate ! ! if checking for duplicate keys
8709
8710
character (kind= CK,len= :),allocatable :: path ! ! path to any duplicate key
8710
8711
8711
- ! clear any exceptions and initialize:
8712
+ ! clear any exceptions and initialize:
8712
8713
call json% initialize()
8713
8714
8714
8715
if ( present (unit) ) then
@@ -8720,7 +8721,7 @@ subroutine json_parse_file(json, file, p, unit)
8720
8721
8721
8722
iunit = unit
8722
8723
8723
- ! check to see if the file is already open
8724
+ ! check to see if the file is already open
8724
8725
! if it is, then use it, otherwise open the file with the name given.
8725
8726
inquire (unit= iunit, opened= is_open, iostat= istat)
8726
8727
if (istat== 0 .and. .not. is_open) then
@@ -8734,7 +8735,7 @@ subroutine json_parse_file(json, file, p, unit)
8734
8735
iostat = istat &
8735
8736
FILE_ENCODING )
8736
8737
else
8737
- ! if the file is already open, then we need to make sure
8738
+ ! if the file is already open, then we need to make sure
8738
8739
! that it is open with the correct form/access/etc...
8739
8740
end if
8740
8741
@@ -8763,6 +8764,7 @@ subroutine json_parse_file(json, file, p, unit)
8763
8764
8764
8765
! parse as a value
8765
8766
call json% parse_value(unit= iunit, str= CK_' ' , value= p)
8767
+ call json% parse_end(unit= iunit, str= CK_' ' )
8766
8768
8767
8769
! check for errors:
8768
8770
if (json% exception_thrown) then
@@ -8812,7 +8814,7 @@ subroutine json_parse_string(json, p, str)
8812
8814
logical (LK) :: has_duplicate ! ! if checking for duplicate keys
8813
8815
character (kind= CK,len= :),allocatable :: path ! ! path to any duplicate key
8814
8816
8815
- ! clear any exceptions and initialize:
8817
+ ! clear any exceptions and initialize:
8816
8818
call json% initialize()
8817
8819
8818
8820
! create the value and associate the pointer
@@ -8824,6 +8826,7 @@ subroutine json_parse_string(json, p, str)
8824
8826
8825
8827
! parse as a value
8826
8828
call json% parse_value(unit= iunit, str= str, value= p)
8829
+ call json% parse_end(unit= iunit, str= str)
8827
8830
8828
8831
if (json% exception_thrown) then
8829
8832
call json% annotate_invalid_json(iunit,str)
@@ -8842,6 +8845,41 @@ subroutine json_parse_string(json, p, str)
8842
8845
end subroutine json_parse_string
8843
8846
! *****************************************************************************************
8844
8847
8848
+ ! *****************************************************************************************
8849
+ ! >
8850
+ ! An error checking routine to call after a file (or string) has been parsed.
8851
+ ! It will throw an exception if there are any other non-whitespace characters
8852
+ ! in the file.
8853
+
8854
+ subroutine json_parse_end (json , unit , str )
8855
+
8856
+ implicit none
8857
+
8858
+ class(json_core),intent (inout ) :: json
8859
+ integer (IK),intent (in ) :: unit ! ! file unit number
8860
+ character (kind= CK,len=* ),intent (in ) :: str ! ! string containing JSON
8861
+ ! ! data (only used if `unit=0`)
8862
+
8863
+ logical (LK) :: eof ! ! end-of-file flag
8864
+ character (kind= CK,len= 1 ) :: c ! ! character read from file
8865
+ ! ! (or string) by [[pop_char]]
8866
+
8867
+ ! first check for exceptions:
8868
+ if (json% exception_thrown) return
8869
+
8870
+ ! pop the next non whitespace character off the file
8871
+ call json% pop_char(unit, str= str, eof= eof, skip_ws= .true. , &
8872
+ skip_comments= json% allow_comments, popped= c)
8873
+
8874
+ if (.not. eof) then
8875
+ call json% throw_exception(' Error in json_parse_end:' // &
8876
+ ' Unexpected character found after parsing value. "' // &
8877
+ c// ' "' )
8878
+ end if
8879
+
8880
+ end subroutine json_parse_end
8881
+ ! *****************************************************************************************
8882
+
8845
8883
! *****************************************************************************************
8846
8884
! >
8847
8885
! Alternate version of [[json_parse_string]], where `str` is kind=CDK.
0 commit comments