Skip to content

Commit 923b707

Browse files
Merge pull request #345 from jacobwilliams/344-json-check-for-errors
json_check_for_errors arguments
2 parents d2f080c + 089b012 commit 923b707

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/json_value_module.F90

+20-15
Original file line numberDiff line numberDiff line change
@@ -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
@@ -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
@@ -8875,8 +8880,8 @@ subroutine annotate_invalid_json(json,iunit,str)
88758880
end if
88768881

88778882
!create the error message:
8878-
json%err_message = json%err_message//newline//&
8879-
'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))//', '//&
88808885
'character: '//trim(adjustl(char_str))//newline//&
88818886
trim(line)//newline//arrow_str
88828887

src/tests/jf_test_15.F90

+6
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ subroutine test_15(error_cnt)
5252
call json%get(p2,'logical',d)
5353
call json%get(p2,'integer',tf)
5454
call json%get(p2,'real', tf)
55+
5556
call json%check_for_errors(status_ok, error_msg) !error condition true
57+
call json%check_for_errors(status_ok) !error condition true
58+
call json%check_for_errors(error_msg=error_msg) !error condition true
59+
5660
call json%initialize(print_signs=.true.) !print signs flag
5761

5862
call json%check_for_errors(status_ok, error_msg) !error condition false
63+
call json%check_for_errors(status_ok) !error condition false
64+
call json%check_for_errors(error_msg=error_msg) !error condition false - not allocated
5965

6066
call file1%move(file2) !should throw an exception since points are not associated
6167
call file1%initialize()

0 commit comments

Comments
 (0)