Skip to content

Commit a9b0a72

Browse files
Merge pull request #425 from jacobwilliams/424-json-info
json_info will now check for exceptions and raise one if the pointer …
2 parents ffca283 + 75e13f6 commit a9b0a72

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/json_value_module.F90

+21-7
Original file line numberDiff line numberDiff line change
@@ -1408,14 +1408,28 @@ subroutine json_info(json,p,var_type,n_children,name)
14081408
integer(IK),intent(out),optional :: n_children !! number of children
14091409
character(kind=CK,len=:),allocatable,intent(out),optional :: name !! variable name
14101410

1411-
if (present(var_type)) var_type = p%var_type
1412-
if (present(n_children)) n_children = json%count(p)
1413-
if (present(name)) then
1414-
if (allocated(p%name)) then
1415-
name = p%name
1416-
else
1417-
name = CK_''
1411+
if (.not. json%exception_thrown .and. associated(p)) then
1412+
1413+
if (present(var_type)) var_type = p%var_type
1414+
if (present(n_children)) n_children = json%count(p)
1415+
if (present(name)) then
1416+
if (allocated(p%name)) then
1417+
name = p%name
1418+
else
1419+
name = CK_''
1420+
end if
14181421
end if
1422+
1423+
else ! error
1424+
1425+
if (.not. json%exception_thrown) then
1426+
call json%throw_exception('Error in json_info: '//&
1427+
'pointer is not associated.' )
1428+
end if
1429+
if (present(var_type)) var_type = json_unknown
1430+
if (present(n_children)) n_children = 0
1431+
if (present(name)) name = CK_''
1432+
14191433
end if
14201434

14211435
end subroutine json_info

src/tests/jf_test_15.F90

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ subroutine test_15(error_cnt)
2626
integer,intent(out) :: error_cnt !! report number of errors to caller
2727

2828
type(json_core) :: json
29-
type(json_value),pointer :: p,p2
29+
type(json_value),pointer :: p,p2,p3
3030
type(json_file) :: file1, file2
3131
logical(LK) :: found,status_ok
3232
integer(IK) :: var_type,i,n_children
@@ -36,6 +36,7 @@ subroutine test_15(error_cnt)
3636
integer(IK),dimension(:),allocatable :: ivec
3737
real(wp),dimension(:),allocatable :: rvec
3838
logical(LK),dimension(:),allocatable :: lvec
39+
character(kind=CK,len=:),allocatable :: name
3940

4041
write(error_unit,'(A)') ''
4142
write(error_unit,'(A)') '================================='
@@ -220,6 +221,18 @@ subroutine test_15(error_cnt)
220221

221222
!****************************************
222223

224+
! try to get info for an unassociated pointer,
225+
! this should raise an exception:
226+
p3 => null()
227+
call json%info(p3,var_type,n_children,name)
228+
call json%check_for_errors(status_ok)
229+
if (status_ok) then
230+
error_cnt=error_cnt+1
231+
write(error_unit,'(A)') 'Error: info for unassociated pointer'
232+
end if
233+
234+
!****************************************
235+
223236
end if
224237

225238
if (error_cnt>0) then

0 commit comments

Comments
 (0)