Skip to content

Commit 737010d

Browse files
committed
When strict_type_checking=False, added ability to get integer/double/logical variables as strings. Fixes #331
1 parent 52fab8d commit 737010d

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/json_value_module.F90

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7275,14 +7275,16 @@ subroutine json_get_integer(json, me, value)
72757275
type(json_value),pointer,intent(in) :: me
72767276
integer(IK),intent(out) :: value
72777277

7278-
value = 0
7278+
logical(LK) :: status_ok !! for [[string_to_integer]]
7279+
7280+
value = 0_IK
72797281
if ( json%exception_thrown ) return
72807282

72817283
if (me%var_type == json_integer) then
72827284
value = me%int_value
72837285
else
72847286
if (json%strict_type_checking) then
7285-
call json%throw_exception('Error in get_integer:'//&
7287+
call json%throw_exception('Error in json_get_integer:'//&
72867288
' Unable to resolve value to integer: '//me%name)
72877289
else
72887290
!type conversions
@@ -7295,8 +7297,16 @@ subroutine json_get_integer(json, me, value)
72957297
else
72967298
value = 0
72977299
end if
7300+
case (json_string)
7301+
call string_to_integer(me%str_value,value,status_ok)
7302+
if (.not. status_ok) then
7303+
value = 0_IK
7304+
call json%throw_exception('Error in json_get_integer:'//&
7305+
' Unable to convert string value to integer: me.'//&
7306+
me%name//' = '//trim(me%str_value))
7307+
end if
72987308
case default
7299-
call json%throw_exception('Error in get_integer:'//&
7309+
call json%throw_exception('Error in json_get_integer:'//&
73007310
' Unable to resolve value to integer: '//me%name)
73017311
end select
73027312
end if
@@ -7491,6 +7501,8 @@ subroutine json_get_double(json, me, value)
74917501
type(json_value),pointer :: me
74927502
real(RK),intent(out) :: value
74937503

7504+
logical(LK) :: status_ok !! for [[string_to_real]]
7505+
74947506
value = 0.0_RK
74957507
if ( json%exception_thrown ) return
74967508

@@ -7511,6 +7523,14 @@ subroutine json_get_double(json, me, value)
75117523
else
75127524
value = 0.0_RK
75137525
end if
7526+
case (json_string)
7527+
call string_to_real(me%str_value,value,status_ok)
7528+
if (.not. status_ok) then
7529+
value = 0.0_RK
7530+
call json%throw_exception('Error in json_get_double:'//&
7531+
' Unable to convert string value to double: me.'//&
7532+
me%name//' = '//trim(me%str_value))
7533+
end if
75147534
case default
75157535
call json%throw_exception('Error in json_get_double:'//&
75167536
' Unable to resolve value to double: '//me%name)
@@ -7703,6 +7723,13 @@ end subroutine wrap_json_get_double_vec_by_path
77037723
!*****************************************************************************************
77047724
!>
77057725
! Get a logical value from a [[json_value]].
7726+
!
7727+
!### Note
7728+
! If `strict_type_checking` is False, then the following assumptions are made:
7729+
!
7730+
! * For integers: a value > 0 is True
7731+
! * For doubles: a value > 0 is True
7732+
! * For strings: 'true' is True, and everything else is false. [case sensitive match]
77067733

77077734
subroutine json_get_logical(json, me, value)
77087735

@@ -7726,7 +7753,11 @@ subroutine json_get_logical(json, me, value)
77267753
!type conversions
77277754
select case (me%var_type)
77287755
case (json_integer)
7729-
value = (me%int_value > 0)
7756+
value = (me%int_value > 0_IK)
7757+
case (json_double)
7758+
value = (me%int_value > 0.0_RK)
7759+
case (json_string)
7760+
value = (me%str_value == true_str)
77307761
case default
77317762
call json%throw_exception('Error in json_get_logical: '//&
77327763
'Unable to resolve value to logical: '//&

0 commit comments

Comments
 (0)