@@ -278,6 +278,14 @@ module json_value_module
278278 ! ! `ieee_quiet_nan` for NaN values. If false,
279279 ! ! `ieee_signaling_nan` will be used.
280280
281+ logical (LK) :: strict_integer_type_checking = .true.
282+ ! ! * If false, when parsing JSON, if an integer numeric value
283+ ! ! cannot be converted to an integer (`integer(IK)`),
284+ ! ! then an attempt is then make to convert it
285+ ! ! to a real (`real(RK)`).
286+ ! ! * If true [default], an exception will be raised if an integer
287+ ! ! value cannot be read when parsing JSON.
288+
281289 integer :: ichunk = 0 ! ! index in `chunk` for [[pop_char]]
282290 ! ! when `use_unformatted_stream=True`
283291 integer :: filesize = 0 ! ! the file size when when `use_unformatted_stream=True`
@@ -1125,6 +1133,10 @@ subroutine json_initialize(me,&
11251133 me% use_quiet_nan = use_quiet_nan
11261134 end if
11271135
1136+ if (present (strict_integer_type_checking)) then
1137+ me% strict_integer_type_checking = strict_integer_type_checking
1138+ end if
1139+
11281140 ! Set the format for real numbers:
11291141 ! [if not changing it, then it remains the same]
11301142
@@ -6772,6 +6784,7 @@ subroutine json_get_by_path_default(json,me,path,p,found,create_it,was_created)
67726784 logical (LK) :: created ! ! if `create` is true, then this will be
67736785 ! ! true if the leaf object had to be created
67746786 integer (IK) :: j ! ! counter of children when creating object
6787+ logical (LK) :: status_ok ! ! integer to string conversion flag
67756788
67766789 nullify(p)
67776790
@@ -6877,7 +6890,13 @@ subroutine json_get_by_path_default(json,me,path,p,found,create_it,was_created)
68776890 exit
68786891 end if
68796892 array = .false.
6880- child_i = json% string_to_int(path(child_i:i-1 ))
6893+ call string_to_integer(path(child_i:i-1 ),child_i,status_ok)
6894+ if (.not. status_ok) then
6895+ call json% throw_exception(' Error in json_get_by_path_default:' // &
6896+ ' Could not convert array index to integer: ' // &
6897+ trim (path(child_i:i-1 )),found)
6898+ exit
6899+ end if
68816900
68826901 nullify(tmp)
68836902 if (create) then
@@ -7991,8 +8010,8 @@ function string_to_int(json,str) result(ival)
79918010 if (.not. status_ok) then
79928011 ival = 0
79938012 call json% throw_exception(' Error in string_to_int: ' // &
7994- ' string cannot be converted to an integer: ' // &
7995- trim (str))
8013+ ' string cannot be converted to an integer: ' // &
8014+ trim (str))
79968015 end if
79978016
79988017 else
@@ -11128,6 +11147,8 @@ subroutine parse_number(json, unit, str, value)
1112811147 type (json_value),pointer :: value
1112911148
1113011149 character (kind= CK,len= :),allocatable :: tmp ! ! temp string
11150+ character (kind= CK,len= :),allocatable :: saved_err_message ! ! temp error message for
11151+ ! ! string to int conversion
1113111152 character (kind= CK,len= 1 ) :: c ! ! character returned by [[pop_char]]
1113211153 logical (LK) :: eof ! ! end of file flag
1113311154 real (RK) :: rval ! ! real value
@@ -11187,9 +11208,31 @@ subroutine parse_number(json, unit, str, value)
1118711208
1118811209 ! string to value:
1118911210 if (is_integer) then
11211+ ! it is an integer:
1119011212 ival = json% string_to_int(tmp)
11191- call json% to_integer(value,ival)
11213+
11214+ if (json% exception_thrown .and. .not. json% strict_integer_type_checking) then
11215+ ! if it couldn't be converted to an integer,
11216+ ! then try to convert it to a real value and see if that works
11217+
11218+ saved_err_message = json% err_message ! keep the original error message
11219+ call json% clear_exceptions() ! clear exceptions
11220+ rval = json% string_to_dble(tmp)
11221+ if (json% exception_thrown) then
11222+ ! restore original error message and continue
11223+ json% err_message = saved_err_message
11224+ call json% to_integer(value,ival) ! just so we have something
11225+ else
11226+ ! in this case, we return a real
11227+ call json% to_real(value,rval)
11228+ end if
11229+
11230+ else
11231+ call json% to_integer(value,ival)
11232+ end if
11233+
1119211234 else
11235+ ! it is a real:
1119311236 rval = json% string_to_dble(tmp)
1119411237 call json% to_real(value,rval)
1119511238 end if
0 commit comments