Skip to content

Commit 6b6e126

Browse files
committed
Merge pull request #158 from jacobwilliams/real_fmt
Fixed a bug where some real numbers weren't being read in properly.
2 parents 426e2fa + d4545ee commit 6b6e126

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/json_module.F90

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,19 @@ module json_module
260260
! The types of JSON data.
261261
!
262262
integer(IK),parameter,public :: json_unknown = 0 !! Unknown JSON data type
263-
!! (see [[json_file_variable_info]] and [[json_info]])
263+
!! (see [[json_file_variable_info]] and [[json_info]])
264264
integer(IK),parameter,public :: json_null = 1 !! Null JSON data type
265-
!! (see [[json_file_variable_info]] and [[json_info]])
265+
!! (see [[json_file_variable_info]] and [[json_info]])
266266
integer(IK),parameter,public :: json_object = 2 !! Object JSON data type
267-
!! (see [[json_file_variable_info]] and [[json_info]])
267+
!! (see [[json_file_variable_info]] and [[json_info]])
268268
integer(IK),parameter,public :: json_array = 3 !! Array JSON data type
269-
!! (see [[json_file_variable_info]] and [[json_info]])
269+
!! (see [[json_file_variable_info]] and [[json_info]])
270270
integer(IK),parameter,public :: json_logical = 4 !! Logical JSON data type
271-
!! (see [[json_file_variable_info]] and [[json_info]])
271+
!! (see [[json_file_variable_info]] and [[json_info]])
272272
integer(IK),parameter,public :: json_integer = 5 !! Integer JSON data type
273-
!! (see [[json_file_variable_info]] and [[json_info]])
273+
!! (see [[json_file_variable_info]] and [[json_info]])
274274
integer(IK),parameter,public :: json_double = 6 !! Double JSON data type
275-
!! (see [[json_file_variable_info]] and [[json_info]])
275+
!! (see [[json_file_variable_info]] and [[json_info]])
276276
integer(IK),parameter,public :: json_string = 7 !! String JSON data type
277277
!*********************************************************
278278

@@ -914,7 +914,7 @@ end subroutine traverse_callback_func
914914
integer(IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
915915
!! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
916916
character(kind=CDK,len=*),parameter :: int_fmt = '(ss,I0)' !! minimum width format for integers
917-
character(kind=CK, len=*),parameter :: star = '*' !! for invalid numbers
917+
character(kind=CK, len=*),parameter :: star = '*' !! for invalid numbers and list-directed real output
918918
character(kind=CDK,len=:),allocatable :: real_fmt !! the format string to use for real numbers
919919
!! it is set in [[json_initialize]]
920920

@@ -925,7 +925,7 @@ end subroutine traverse_callback_func
925925
!exception handling [private variables]
926926
logical(LK) :: is_verbose = .false. !! if true, all exceptions are immediately printed to console
927927
logical(LK) :: exception_thrown = .true. !! the error flag (by default, this is true to
928-
!! make sure that [[json_initialize]] is called.
928+
!! make sure that [[json_initialize]] is called.
929929
character(kind=CK,len=:),allocatable :: err_message !! the error message
930930

931931
!temp vars used when parsing lines in file [private variables]
@@ -1717,6 +1717,16 @@ subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
17171717
present(compact_reals) .or. &
17181718
present(print_signs) .or. &
17191719
present(real_format) ) then
1720+
1721+
!allow the special case where real format is '*':
1722+
! [this overrides the other options]
1723+
if (present(real_format)) then
1724+
if (real_format==star) then
1725+
compact_real = .false.
1726+
real_fmt = star
1727+
return
1728+
end if
1729+
end if
17201730

17211731
if (present(compact_reals)) compact_real = compact_reals
17221732

@@ -3971,6 +3981,11 @@ end function string_to_integer
39713981
! date: 1/19/2014
39723982
!
39733983
! Convert a string into a double.
3984+
!
3985+
!# History
3986+
! * Jacob Williams, 10/27/2015 : Now using fmt=*, rather than
3987+
! fmt=real_fmt, since it doesn't work for some unusual cases
3988+
! (e.g., when str='1E-5').
39743989

39753990
function string_to_double(str) result(rval)
39763991

@@ -3982,15 +3997,16 @@ function string_to_double(str) result(rval)
39823997
integer(IK) :: ierr
39833998

39843999
if (.not. exception_thrown) then
3985-
3986-
read(str,fmt=real_fmt,iostat=ierr) rval !string to double
3987-
4000+
4001+
!string to double
4002+
read(str,fmt=*,iostat=ierr) rval
4003+
39884004
if (ierr/=0) then !if there was an error
39894005
rval = 0.0_RK
39904006
call throw_exception('Error in string_to_double:'//&
39914007
' string cannot be converted to a double: '//trim(str))
39924008
end if
3993-
4009+
39944010
end if
39954011

39964012
end function string_to_double
@@ -6652,6 +6668,7 @@ end subroutine integer_to_string
66526668
!
66536669
!# Modified
66546670
! * Izaak Beekman : 02/24/2015 : added the compact option.
6671+
! * Jacob Williams : 10/27/2015 : added the star option.
66556672

66566673
subroutine real_to_string(rval,str)
66576674

@@ -6662,8 +6679,11 @@ subroutine real_to_string(rval,str)
66626679

66636680
integer(IK) :: istat
66646681

6665-
!default format:
6666-
write(str,fmt=real_fmt,iostat=istat) rval
6682+
if (real_fmt==star) then
6683+
write(str,fmt=*,iostat=istat) rval
6684+
else
6685+
write(str,fmt=real_fmt,iostat=istat) rval
6686+
end if
66676687

66686688
if (istat==0) then
66696689

src/tests/jf_test_13.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ subroutine test_13(error_cnt)
2525
character(kind=CK,len=:),allocatable :: str
2626
integer :: i
2727

28-
character(len=2),dimension(4),parameter :: fmts=['g ','e ','en','es'] !! format statements to test
28+
character(len=2),dimension(5),parameter :: fmts=['g ','e ','en','es','* ']
29+
!! format statements to test
2930

3031
write(error_unit,'(A)') ''
3132
write(error_unit,'(A)') '================================='

0 commit comments

Comments
 (0)