@@ -465,8 +465,9 @@ end subroutine array_callback_func
465
465
public :: to_array !
466
466
467
467
! exception handling [private variables]
468
- logical :: exception_thrown = .false. ! the error flag
469
- character (len= :),allocatable :: err_message ! the error message
468
+ logical :: is_verbose = .false. ! if true, all exceptions are immediately printed to console
469
+ logical :: exception_thrown = .false. ! the error flag
470
+ character (len= :),allocatable :: err_message ! the error message
470
471
471
472
! POP/PUSH CHARACTER [private variables]
472
473
integer :: char_count = 0
@@ -1030,9 +1031,14 @@ end subroutine get_char_vec_from_json_file
1030
1031
!
1031
1032
! SOURCE
1032
1033
1033
- subroutine json_initialize ()
1034
+ subroutine json_initialize (verbose )
1034
1035
1035
1036
implicit none
1037
+
1038
+ logical ,intent (in ),optional :: verbose ! mainly useful for debugging (default is false)
1039
+
1040
+ ! optional input (if not present, value remains unchanged):
1041
+ if (present (verbose)) is_verbose = verbose
1036
1042
1037
1043
! clear any errors from previous runs:
1038
1044
call json_clear_exceptions()
@@ -1095,7 +1101,13 @@ subroutine throw_exception(msg)
1095
1101
1096
1102
exception_thrown = .true.
1097
1103
err_message = trim (msg)
1098
-
1104
+
1105
+ if (is_verbose) then
1106
+ write (* ,' (A)' ) ' ***********************'
1107
+ write (* ,' (A)' ) ' JSON-FORTRAN EXCEPTION: ' // trim (msg)
1108
+ write (* ,' (A)' ) ' ***********************'
1109
+ end if
1110
+
1099
1111
end subroutine throw_exception
1100
1112
! *****************************************************************************************
1101
1113
@@ -4165,11 +4177,11 @@ end subroutine parse_object
4165
4177
! parse_array
4166
4178
!
4167
4179
! DESCRIPTION
4168
- !
4180
+ ! Core parsing routine.
4169
4181
!
4170
4182
! SOURCE
4171
4183
4172
- recursive subroutine parse_array (unit , array )
4184
+ subroutine parse_array (unit , array )
4173
4185
4174
4186
implicit none
4175
4187
@@ -4179,36 +4191,41 @@ recursive subroutine parse_array(unit, array)
4179
4191
type (json_value), pointer :: element
4180
4192
logical :: eof
4181
4193
character (len= 1 ) :: c
4194
+
4195
+ do
4182
4196
4183
- if (.not. exception_thrown) then
4184
- nullify(element)
4197
+ if (exception_thrown) exit
4185
4198
4186
4199
! try to parse an element value
4200
+ nullify(element)
4187
4201
call json_value_create(element)
4188
4202
call parse_value(unit, element)
4189
- if (exception_thrown) return
4203
+ if (exception_thrown) exit
4190
4204
4191
4205
! parse value will disassociate an empty array value
4192
- if (associated (element)) then
4193
- call json_value_add(array, element)
4194
- nullify(element) ! cleanup
4195
- end if
4206
+ if (associated (element)) call json_value_add(array, element)
4196
4207
4197
4208
! popped the next character
4198
4209
c = pop_char(unit, eof = eof, skip_ws = .true. )
4199
4210
4200
4211
if (eof) then
4201
- return
4212
+ ! The file ended before array was finished:
4213
+ call throw_exception(' Error in parse_array: ' // &
4214
+ ' End of file encountered when parsing an array.' )
4215
+ exit
4202
4216
else if (' ,' == c) then
4203
4217
! parse the next element
4204
- call parse_array(unit, array)
4205
- if (exception_thrown) return
4218
+ cycle
4206
4219
else if (' ]' == c) then
4207
4220
! end of array
4208
- return
4221
+ exit
4222
+ else
4223
+ call throw_exception(' Error in parse_array: ' // &
4224
+ ' Unexpected character encountered when parsing array.' )
4225
+ exit
4209
4226
end if
4210
-
4211
- end if
4227
+
4228
+ end do
4212
4229
4213
4230
end subroutine parse_array
4214
4231
! *****************************************************************************************
@@ -4320,7 +4337,7 @@ end subroutine parse_string
4320
4337
! parse_for_chars
4321
4338
!
4322
4339
! DESCRIPTION
4323
- !
4340
+ ! Core parsing routine.
4324
4341
!
4325
4342
! SOURCE
4326
4343
0 commit comments