@@ -463,11 +463,11 @@ module json_value_module
463
463
! type(json_core) :: json
464
464
! type(json_value) :: p
465
465
! !...
466
- ! call json%print(p,'test.json') !this is [[json_print_2 ]]
466
+ ! call json%print(p,'test.json') !this is [[json_print_to_filename ]]
467
467
! ````
468
- generic,public :: print = > json_print_1,json_print_2
469
- procedure :: json_print_1
470
- procedure :: json_print_2
468
+ generic,public :: print = > json_print_to_unit,json_print_to_filename
469
+ procedure :: json_print_to_unit
470
+ procedure :: json_print_to_filename
471
471
472
472
! >
473
473
! Destructor routine for a [[json_value]] pointer.
@@ -5225,7 +5225,7 @@ end subroutine json_value_to_string
5225
5225
!
5226
5226
! Print the [[json_value]] structure to a file.
5227
5227
5228
- subroutine json_print_1 (json ,p ,iunit )
5228
+ subroutine json_print_to_unit (json ,p ,iunit )
5229
5229
5230
5230
implicit none
5231
5231
@@ -5234,15 +5234,16 @@ subroutine json_print_1(json,p,iunit)
5234
5234
integer (IK),intent (in ) :: iunit ! ! the file unit (the file must
5235
5235
! ! already have been opened, can't be -1).
5236
5236
5237
- character (kind= CK,len= :),allocatable :: dummy
5237
+ character (kind= CK,len= :),allocatable :: dummy ! ! dummy for `str` argument
5238
+ ! ! to [[json_value_print]]
5238
5239
5239
5240
if (iunit/= unit2str) then
5240
5241
call json% json_value_print(p,iunit,str= dummy, indent= 1_IK , colon= .true. )
5241
5242
else
5242
- call json% throw_exception(' Error in json_print_1 : iunit must not be -1.' )
5243
+ call json% throw_exception(' Error in json_print_to_unit : iunit must not be -1.' )
5243
5244
end if
5244
5245
5245
- end subroutine json_print_1
5246
+ end subroutine json_print_to_unit
5246
5247
! *****************************************************************************************
5247
5248
5248
5249
! *****************************************************************************************
@@ -5251,7 +5252,7 @@ end subroutine json_print_1
5251
5252
!
5252
5253
! Print the [[json_value]] structure to a file.
5253
5254
5254
- subroutine json_print_2 (json ,p ,filename )
5255
+ subroutine json_print_to_filename (json ,p ,filename )
5255
5256
5256
5257
implicit none
5257
5258
@@ -5260,18 +5261,19 @@ subroutine json_print_2(json,p,filename)
5260
5261
character (kind= CDK,len=* ),intent (in ) :: filename ! ! the filename to print to
5261
5262
! ! (should not already be open)
5262
5263
5263
- integer (IK) :: iunit,istat
5264
+ integer (IK) :: iunit ! ! file unit for `open` statement
5265
+ integer (IK) :: istat ! ! `iostat` code for `open` statement
5264
5266
5265
5267
open (newunit= iunit,file= filename,status= ' REPLACE' ,iostat= istat FILE_ENCODING )
5266
5268
if (istat== 0 ) then
5267
5269
call json% print (p,iunit)
5268
5270
close (iunit,iostat= istat)
5269
5271
else
5270
- call json% throw_exception(' Error in json_print_2 : could not open file: ' // &
5272
+ call json% throw_exception(' Error in json_print_to_filename : could not open file: ' // &
5271
5273
trim (filename))
5272
5274
end if
5273
5275
5274
- end subroutine json_print_2
5276
+ end subroutine json_print_to_filename
5275
5277
! *****************************************************************************************
5276
5278
5277
5279
! *****************************************************************************************
@@ -5291,16 +5293,17 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
5291
5293
5292
5294
class(json_core),intent (inout ) :: json
5293
5295
type (json_value),pointer ,intent (in ) :: p
5294
- integer (IK),intent (in ) :: iunit ! ! file unit to write to (6=console)
5296
+ integer (IK),intent (in ) :: iunit ! ! file unit to write to (the
5297
+ ! ! file is assumed to be open)
5295
5298
integer (IK),intent (in ),optional :: indent ! ! indention level
5296
5299
logical (LK),intent (in ),optional :: is_array_element ! ! if this is an array element
5297
5300
logical (LK),intent (in ),optional :: need_comma ! ! if it needs a comma after it
5298
5301
logical (LK),intent (in ),optional :: colon ! ! if the colon was just written
5299
5302
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]].
5303
+ ! ! if `iunit==unit2str` (-1) then
5304
+ ! ! the structure is printed to this
5305
+ ! ! string rather than a file. This mode
5306
+ ! ! is used by [[json_value_to_string]].
5304
5307
logical (LK),intent (in ),optional :: is_compressed_vector ! ! if True, this is an element
5305
5308
! ! from an array being printed
5306
5309
! ! on one line [default is False]
@@ -5326,6 +5329,16 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
5326
5329
5327
5330
if (.not. json% exception_thrown) then
5328
5331
5332
+ if (.not. associated (p)) then
5333
+ ! note: a null() pointer will trigger this error.
5334
+ ! However, if the pointer is undefined, then this will
5335
+ ! crash (if this wasn't here it would crash below when
5336
+ ! we try to access the contents)
5337
+ call json% throw_exception(' Error in json_value_print: ' // &
5338
+ ' the pointer is not associated' )
5339
+ return
5340
+ end if
5341
+
5329
5342
if (present (is_compressed_vector)) then
5330
5343
is_vector = is_compressed_vector
5331
5344
else
@@ -5420,6 +5433,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
5420
5433
! recursive print of the element
5421
5434
call json% json_value_print(element, iunit= iunit, indent= tab + 1 , &
5422
5435
need_comma= i< count, colon= .true. , str= str)
5436
+ if (json% exception_thrown) return
5423
5437
5424
5438
! get the next child the list:
5425
5439
element = > element% next
@@ -5500,6 +5514,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
5500
5514
call json% json_value_print(element, iunit= iunit, indent= tab,&
5501
5515
need_comma= i< count, is_array_element= .true. , str= str)
5502
5516
end if
5517
+ if (json% exception_thrown) return
5518
+
5503
5519
! get the next child the list:
5504
5520
element = > element% next
5505
5521
0 commit comments