@@ -176,7 +176,10 @@ module json_value_module
176176 ! ! when an error is thrown in the class.
177177 ! ! Many of the methods will check this
178178 ! ! and return immediately if it is true.
179- character (kind= CK,len= :),allocatable :: err_message ! ! the error message
179+ character (kind= CK,len= :),allocatable :: err_message
180+ ! ! the error message.
181+ ! ! if `exception_thrown=False` then
182+ ! ! this variable is not allocated.
180183
181184 integer (IK) :: char_count = 0 ! ! character position in the current line
182185 integer (IK) :: line_count = 1 ! ! lines read counter
@@ -463,11 +466,11 @@ module json_value_module
463466 ! type(json_core) :: json
464467 ! type(json_value) :: p
465468 ! !...
466- ! call json%print(p,'test.json') !this is [[json_print_2 ]]
469+ ! call json%print(p,'test.json') !this is [[json_print_to_filename ]]
467470 ! ````
468- generic,public :: print = > json_print_1,json_print_2
469- procedure :: json_print_1
470- procedure :: json_print_2
471+ generic,public :: print = > json_print_to_unit,json_print_to_filename
472+ procedure :: json_print_to_unit
473+ procedure :: json_print_to_filename
471474
472475 ! >
473476 ! Destructor routine for a [[json_value]] pointer.
@@ -1804,7 +1807,7 @@ pure subroutine json_clear_exceptions(json)
18041807
18051808 ! clear the flag and message:
18061809 json% exception_thrown = .false.
1807- json% err_message = CK_ ' '
1810+ if ( allocated ( json% err_message)) deallocate (json % err_message)
18081811
18091812 end subroutine json_clear_exceptions
18101813! *****************************************************************************************
@@ -1908,25 +1911,27 @@ end subroutine wrap_json_throw_exception
19081911!
19091912! ### See also
19101913! * [[json_failed]]
1914+ ! * [[json_throw_exception]]
19111915
1912- subroutine json_check_for_errors (json ,status_ok ,error_msg )
1916+ pure subroutine json_check_for_errors (json ,status_ok ,error_msg )
19131917
19141918 implicit none
19151919
1916- class(json_core),intent (inout ) :: json
1917- logical (LK),intent (out ) :: status_ok ! ! true if there were no errors
1918- character (kind= CK,len= :),allocatable ,intent (out ) :: error_msg ! ! the error message (if there were errors)
1920+ class(json_core),intent (in ) :: json
1921+ logical (LK),intent (out ),optional :: status_ok ! ! true if there were no errors
1922+ character (kind= CK,len= :),allocatable ,intent (out ),optional :: error_msg ! ! the error message.
1923+ ! ! (not allocated if
1924+ ! ! there were no errors)
19191925
1920- status_ok = .not. json% exception_thrown
1926+ if ( present (status_ok)) status_ok = .not. json% exception_thrown
19211927
1922- if (.not. status_ok) then
1923- if (allocated (json% err_message)) then
1928+ if (present (error_msg)) then
1929+ if (json% exception_thrown) then
1930+ ! if an exception has been thrown,
1931+ ! then this will always be allocated
1932+ ! [see json_throw_exception]
19241933 error_msg = json% err_message
1925- else
1926- error_msg = ' Unknown error.'
19271934 end if
1928- else
1929- error_msg = CK_' '
19301935 end if
19311936
19321937 end subroutine json_check_for_errors
@@ -5225,7 +5230,7 @@ end subroutine json_value_to_string
52255230!
52265231! Print the [[json_value]] structure to a file.
52275232
5228- subroutine json_print_1 (json ,p ,iunit )
5233+ subroutine json_print_to_unit (json ,p ,iunit )
52295234
52305235 implicit none
52315236
@@ -5234,15 +5239,16 @@ subroutine json_print_1(json,p,iunit)
52345239 integer (IK),intent (in ) :: iunit ! ! the file unit (the file must
52355240 ! ! already have been opened, can't be -1).
52365241
5237- character (kind= CK,len= :),allocatable :: dummy
5242+ character (kind= CK,len= :),allocatable :: dummy ! ! dummy for `str` argument
5243+ ! ! to [[json_value_print]]
52385244
52395245 if (iunit/= unit2str) then
52405246 call json% json_value_print(p,iunit,str= dummy, indent= 1_IK , colon= .true. )
52415247 else
5242- call json% throw_exception(' Error in json_print_1 : iunit must not be -1.' )
5248+ call json% throw_exception(' Error in json_print_to_unit : iunit must not be -1.' )
52435249 end if
52445250
5245- end subroutine json_print_1
5251+ end subroutine json_print_to_unit
52465252! *****************************************************************************************
52475253
52485254! *****************************************************************************************
@@ -5251,7 +5257,7 @@ end subroutine json_print_1
52515257!
52525258! Print the [[json_value]] structure to a file.
52535259
5254- subroutine json_print_2 (json ,p ,filename )
5260+ subroutine json_print_to_filename (json ,p ,filename )
52555261
52565262 implicit none
52575263
@@ -5260,18 +5266,19 @@ subroutine json_print_2(json,p,filename)
52605266 character (kind= CDK,len=* ),intent (in ) :: filename ! ! the filename to print to
52615267 ! ! (should not already be open)
52625268
5263- integer (IK) :: iunit,istat
5269+ integer (IK) :: iunit ! ! file unit for `open` statement
5270+ integer (IK) :: istat ! ! `iostat` code for `open` statement
52645271
52655272 open (newunit= iunit,file= filename,status= ' REPLACE' ,iostat= istat FILE_ENCODING )
52665273 if (istat== 0 ) then
52675274 call json% print (p,iunit)
52685275 close (iunit,iostat= istat)
52695276 else
5270- call json% throw_exception(' Error in json_print_2 : could not open file: ' // &
5277+ call json% throw_exception(' Error in json_print_to_filename : could not open file: ' // &
52715278 trim (filename))
52725279 end if
52735280
5274- end subroutine json_print_2
5281+ end subroutine json_print_to_filename
52755282! *****************************************************************************************
52765283
52775284! *****************************************************************************************
@@ -5291,16 +5298,17 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
52915298
52925299 class(json_core),intent (inout ) :: json
52935300 type (json_value),pointer ,intent (in ) :: p
5294- integer (IK),intent (in ) :: iunit ! ! file unit to write to (6=console)
5301+ integer (IK),intent (in ) :: iunit ! ! file unit to write to (the
5302+ ! ! file is assumed to be open)
52955303 integer (IK),intent (in ),optional :: indent ! ! indention level
52965304 logical (LK),intent (in ),optional :: is_array_element ! ! if this is an array element
52975305 logical (LK),intent (in ),optional :: need_comma ! ! if it needs a comma after it
52985306 logical (LK),intent (in ),optional :: colon ! ! if the colon was just written
52995307 character (kind= CK,len= :),intent (inout ),allocatable :: str
5300- ! ! if `iunit==unit2str` (-1) then the structure is
5301- ! ! printed to this string rather than
5302- ! ! a file. This mode is used by
5303- ! ! [[json_value_to_string]].
5308+ ! ! if `iunit==unit2str` (-1) then
5309+ ! ! the structure is printed to this
5310+ ! ! string rather than a file. This mode
5311+ ! ! is used by [[json_value_to_string]].
53045312 logical (LK),intent (in ),optional :: is_compressed_vector ! ! if True, this is an element
53055313 ! ! from an array being printed
53065314 ! ! on one line [default is False]
@@ -5326,6 +5334,16 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
53265334
53275335 if (.not. json% exception_thrown) then
53285336
5337+ if (.not. associated (p)) then
5338+ ! note: a null() pointer will trigger this error.
5339+ ! However, if the pointer is undefined, then this will
5340+ ! crash (if this wasn't here it would crash below when
5341+ ! we try to access the contents)
5342+ call json% throw_exception(' Error in json_value_print: ' // &
5343+ ' the pointer is not associated' )
5344+ return
5345+ end if
5346+
53295347 if (present (is_compressed_vector)) then
53305348 is_vector = is_compressed_vector
53315349 else
@@ -5420,6 +5438,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
54205438 ! recursive print of the element
54215439 call json% json_value_print(element, iunit= iunit, indent= tab + 1 , &
54225440 need_comma= i< count, colon= .true. , str= str)
5441+ if (json% exception_thrown) return
54235442
54245443 ! get the next child the list:
54255444 element = > element% next
@@ -5500,6 +5519,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
55005519 call json% json_value_print(element, iunit= iunit, indent= tab,&
55015520 need_comma= i< count, is_array_element= .true. , str= str)
55025521 end if
5522+ if (json% exception_thrown) return
5523+
55035524 ! get the next child the list:
55045525 element = > element% next
55055526
@@ -8859,8 +8880,8 @@ subroutine annotate_invalid_json(json,iunit,str)
88598880 end if
88608881
88618882 ! create the error message:
8862- json% err_message = json% err_message// newline// &
8863- ' line: ' // trim (adjustl (line_str))// ' , ' // &
8883+ if ( allocated ( json% err_message)) json % err_message = json% err_message// newline
8884+ json % err_message = ' line: ' // trim (adjustl (line_str))// ' , ' // &
88648885 ' character: ' // trim (adjustl (char_str))// newline// &
88658886 trim (line)// newline// arrow_str
88668887
0 commit comments