@@ -732,6 +732,7 @@ module json_value_module
732732 procedure :: json_value_print
733733 procedure :: string_to_int
734734 procedure :: string_to_dble
735+ procedure :: parse_end = > json_parse_end
735736 procedure :: parse_value
736737 procedure :: parse_number
737738 procedure :: parse_string
@@ -8719,7 +8720,7 @@ subroutine json_parse_file(json, file, p, unit)
87198720 logical (LK) :: has_duplicate ! ! if checking for duplicate keys
87208721 character (kind= CK,len= :),allocatable :: path ! ! path to any duplicate key
87218722
8722- ! clear any exceptions and initialize:
8723+ ! clear any exceptions and initialize:
87238724 call json% initialize()
87248725
87258726 if ( present (unit) ) then
@@ -8731,7 +8732,7 @@ subroutine json_parse_file(json, file, p, unit)
87318732
87328733 iunit = unit
87338734
8734- ! check to see if the file is already open
8735+ ! check to see if the file is already open
87358736 ! if it is, then use it, otherwise open the file with the name given.
87368737 inquire (unit= iunit, opened= is_open, iostat= istat)
87378738 if (istat== 0 .and. .not. is_open) then
@@ -8745,7 +8746,7 @@ subroutine json_parse_file(json, file, p, unit)
87458746 iostat = istat &
87468747 FILE_ENCODING )
87478748 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
87498750 ! that it is open with the correct form/access/etc...
87508751 end if
87518752
@@ -8779,6 +8780,7 @@ subroutine json_parse_file(json, file, p, unit)
87798780
87808781 ! parse as a value
87818782 call json% parse_value(unit= iunit, str= CK_' ' , value= p)
8783+ call json% parse_end(unit= iunit, str= CK_' ' )
87828784
87838785 ! check for errors:
87848786 if (json% exception_thrown) then
@@ -8828,7 +8830,7 @@ subroutine json_parse_string(json, p, str)
88288830 logical (LK) :: has_duplicate ! ! if checking for duplicate keys
88298831 character (kind= CK,len= :),allocatable :: path ! ! path to any duplicate key
88308832
8831- ! clear any exceptions and initialize:
8833+ ! clear any exceptions and initialize:
88328834 call json% initialize()
88338835
88348836 ! create the value and associate the pointer
@@ -8840,6 +8842,7 @@ subroutine json_parse_string(json, p, str)
88408842
88418843 ! parse as a value
88428844 call json% parse_value(unit= iunit, str= str, value= p)
8845+ call json% parse_end(unit= iunit, str= str)
88438846
88448847 if (json% exception_thrown) then
88458848 call json% annotate_invalid_json(iunit,str)
@@ -8858,6 +8861,41 @@ subroutine json_parse_string(json, p, str)
88588861 end subroutine json_parse_string
88598862! *****************************************************************************************
88608863
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+
88618899! *****************************************************************************************
88628900! >
88638901! Alternate version of [[json_parse_string]], where `str` is kind=CDK.
0 commit comments