Skip to content

Commit d2f080c

Browse files
Merge pull request #343 from jacobwilliams/342-json-value-print
json_value_print error checking
2 parents 360cc84 + 25367d5 commit d2f080c

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

src/json_value_module.F90

+33-17
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,11 @@ module json_value_module
463463
! type(json_core) :: json
464464
! type(json_value) :: p
465465
! !...
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]]
467467
!````
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
471471

472472
!>
473473
! Destructor routine for a [[json_value]] pointer.
@@ -5225,7 +5225,7 @@ end subroutine json_value_to_string
52255225
!
52265226
! Print the [[json_value]] structure to a file.
52275227

5228-
subroutine json_print_1(json,p,iunit)
5228+
subroutine json_print_to_unit(json,p,iunit)
52295229

52305230
implicit none
52315231

@@ -5234,15 +5234,16 @@ subroutine json_print_1(json,p,iunit)
52345234
integer(IK),intent(in) :: iunit !! the file unit (the file must
52355235
!! already have been opened, can't be -1).
52365236

5237-
character(kind=CK,len=:),allocatable :: dummy
5237+
character(kind=CK,len=:),allocatable :: dummy !! dummy for `str` argument
5238+
!! to [[json_value_print]]
52385239

52395240
if (iunit/=unit2str) then
52405241
call json%json_value_print(p,iunit,str=dummy, indent=1_IK, colon=.true.)
52415242
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.')
52435244
end if
52445245

5245-
end subroutine json_print_1
5246+
end subroutine json_print_to_unit
52465247
!*****************************************************************************************
52475248

52485249
!*****************************************************************************************
@@ -5251,7 +5252,7 @@ end subroutine json_print_1
52515252
!
52525253
! Print the [[json_value]] structure to a file.
52535254

5254-
subroutine json_print_2(json,p,filename)
5255+
subroutine json_print_to_filename(json,p,filename)
52555256

52565257
implicit none
52575258

@@ -5260,18 +5261,19 @@ subroutine json_print_2(json,p,filename)
52605261
character(kind=CDK,len=*),intent(in) :: filename !! the filename to print to
52615262
!! (should not already be open)
52625263

5263-
integer(IK) :: iunit,istat
5264+
integer(IK) :: iunit !! file unit for `open` statement
5265+
integer(IK) :: istat !! `iostat` code for `open` statement
52645266

52655267
open(newunit=iunit,file=filename,status='REPLACE',iostat=istat FILE_ENCODING )
52665268
if (istat==0) then
52675269
call json%print(p,iunit)
52685270
close(iunit,iostat=istat)
52695271
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: '//&
52715273
trim(filename))
52725274
end if
52735275

5274-
end subroutine json_print_2
5276+
end subroutine json_print_to_filename
52755277
!*****************************************************************************************
52765278

52775279
!*****************************************************************************************
@@ -5291,16 +5293,17 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
52915293

52925294
class(json_core),intent(inout) :: json
52935295
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)
52955298
integer(IK),intent(in),optional :: indent !! indention level
52965299
logical(LK),intent(in),optional :: is_array_element !! if this is an array element
52975300
logical(LK),intent(in),optional :: need_comma !! if it needs a comma after it
52985301
logical(LK),intent(in),optional :: colon !! if the colon was just written
52995302
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]].
53045307
logical(LK),intent(in),optional :: is_compressed_vector !! if True, this is an element
53055308
!! from an array being printed
53065309
!! on one line [default is False]
@@ -5326,6 +5329,16 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
53265329

53275330
if (.not. json%exception_thrown) then
53285331

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+
53295342
if (present(is_compressed_vector)) then
53305343
is_vector = is_compressed_vector
53315344
else
@@ -5420,6 +5433,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
54205433
! recursive print of the element
54215434
call json%json_value_print(element, iunit=iunit, indent=tab + 1, &
54225435
need_comma=i<count, colon=.true., str=str)
5436+
if (json%exception_thrown) return
54235437

54245438
! get the next child the list:
54255439
element => element%next
@@ -5500,6 +5514,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
55005514
call json%json_value_print(element, iunit=iunit, indent=tab,&
55015515
need_comma=i<count, is_array_element=.true., str=str)
55025516
end if
5517+
if (json%exception_thrown) return
5518+
55035519
! get the next child the list:
55045520
element => element%next
55055521

src/tests/jf_test_02.F90

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ subroutine test_2(error_cnt)
5151
error_cnt = error_cnt + 1
5252
end if
5353

54+
! first we test an incorrect usage case:
55+
! [trying to print a null json_value pointer]
56+
nullify(p)
57+
call json%print(p,error_unit)
58+
if (.not. json%failed()) then
59+
write(error_unit,'(A)') 'Error: printing a null pointer should have raised an exception.'
60+
error_cnt = error_cnt + 1
61+
end if
62+
call json%initialize() ! clears exceptions
63+
5464
!root:
5565
call json%create_object(p,dir//filename2) ! create the value and associate the pointer
5666
! add the file name as the name of the overall structure

0 commit comments

Comments
 (0)