@@ -867,6 +867,10 @@ end subroutine array_callback_func
867867 ! for indenting (Note: jsonlint.com uses 4 spaces)
868868 integer (IK),parameter :: spaces_per_tab = 2
869869
870+ ! Variables for real string printing:
871+
872+ logical (LK) :: compact_real = .true. ! ! to use the "compact" form of real numbers for output
873+
870874 ! find out the precision of the floating point number system
871875 ! and set safety factors
872876 integer (IK),parameter :: rp_safety_factor = 1
@@ -881,25 +885,21 @@ end subroutine array_callback_func
881885 real (max (maxexp,abs (maxexp)),&
882886 kind= RK) ) )
883887
884- ! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
885- integer (IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
886- ! real format set by library initialization
887- character (kind= CDK,len=* ),parameter :: int_fmt = ' (ss,I0)' ! minimum width format for integers
888- character (kind= CK, len=* ),parameter :: star = ' *' ! for invalid numbers
889-
890- ! real string printing:
891- character (kind= CDK,len= :),allocatable :: real_fmt ! the format string to use for real numbers
892- ! [set in json_initialize]
893- logical (LK) :: compact_real = .true. ! to use the "compact" form of real numbers for output
888+ integer (IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
889+ ! ! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
890+ character (kind= CDK,len=* ),parameter :: int_fmt = ' (ss,I0)' ! ! minimum width format for integers
891+ character (kind= CK, len=* ),parameter :: star = ' *' ! ! for invalid numbers
892+ character (kind= CDK,len= :),allocatable :: real_fmt ! ! the format string to use for real numbers
893+ ! ! it is set in [[json_initialize]]
894894
895895 !
896896 ! Note: the following global variables make this module non thread safe.
897897 !
898898
899899 ! exception handling [private variables]
900- logical (LK) :: is_verbose = .false. ! if true, all exceptions are immediately printed to console
901- logical (LK) :: exception_thrown = .false . ! the error flag
902- character (kind= CK,len= :),allocatable :: err_message ! the error message
900+ logical (LK) :: is_verbose = .false. ! ! if true, all exceptions are immediately printed to console
901+ logical (LK) :: exception_thrown = .true . ! ! the error flag (by default, this is true to make sure that [[json_initialize]] is called.
902+ character (kind= CK,len= :),allocatable :: err_message ! ! the error message
903903
904904 ! temp vars used when parsing lines in file [private variables]
905905 integer (IK) :: char_count = 0 ! character position in the current line
@@ -1642,59 +1642,71 @@ subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
16421642
16431643 implicit none
16441644
1645- logical (LK),intent (in ),optional :: verbose ! ! mainly useful for debugging (default is false)
1646- logical (LK),intent (in ),optional :: compact_reals ! ! to compact the real number strings for output
1647- logical (LK),intent (in ),optional :: print_signs ! ! always print numeric sign (default is false)
1648- character (len=* ,kind= CDK),intent (in ),optional :: real_format
1649- ! ! exponential (default), scientific, engineering or general
1645+ logical (LK),intent (in ),optional :: verbose ! ! mainly useful for debugging (default is false)
1646+ logical (LK),intent (in ),optional :: compact_reals ! ! to compact the real number strings for output (default is true)
1647+ logical (LK),intent (in ),optional :: print_signs ! ! always print numeric sign (default is false)
1648+ character (len=* ,kind= CDK),intent (in ),optional :: real_format ! ! exponential (default), scientific, engineering or general
16501649
16511650 character (kind= CDK,len= 10 ) :: w,d,e
16521651 character (kind= CDK,len= 2 ) :: sgn, rl_edit_desc
16531652 integer (IK) :: istat
16541653 logical (LK) :: sgn_prnt
16551654
1656-
16571655 ! clear any errors from previous runs:
16581656 call json_clear_exceptions()
16591657
1660- ! set defaults
1661- sgn_prnt = .false.
1662- if ( present ( print_signs) ) sgn_prnt = print_signs
1663- if ( sgn_prnt ) then
1664- sgn = ' sp'
1665- else
1666- sgn = ' ss'
1667- end if
1658+ ! Ensure gfortran bug work around "parameters" are set properly
1659+ null_str = ' null'
1660+ true_str = ' true'
1661+ false_str = ' false'
16681662
1669- rl_edit_desc = ' E'
1670- if ( present ( real_format ) ) then
1671- select case ( real_format )
1672- case (' g' ,' G' ,' e' ,' E' ,' en' ,' EN' ,' es' ,' ES' )
1673- rl_edit_desc = real_format
1674- case default
1675- call throw_exception(' Invalid real format, "' // trim (real_format) // ' ", passed to json_initialize.' // &
1676- new_line(' a' ) // ' Acceptable formats are: "G", "E", "EN", and "ES".' )
1677- end select
1678- end if
1663+ ! Just in case, clear these global variables also:
1664+ pushed_index = 0
1665+ pushed_char = ' '
1666+ char_count = 0
1667+ line_count = 1
1668+ ipos = 1
16791669
16801670# ifdef USE_UCS4
16811671 ! reopen stdout and stderr with utf-8 encoding
16821672 open (output_unit,encoding= ' utf-8' )
16831673 open (error_unit, encoding= ' utf-8' )
16841674# endif
16851675
1686- ! Ensure gfortran bug work around "parameters" are set properly
1687- null_str = ' null'
1688- true_str = ' true'
1689- false_str = ' false'
1676+ ! verbose error printing:
1677+ if (present (verbose)) is_verbose = verbose
1678+
1679+ ! Set the format for real numbers:
1680+ ! [if not changing it, then it remains the same]
1681+
1682+ if ( (.not. allocated (real_fmt)) .or. & ! if this hasn't been done yet
1683+ present (compact_reals) .or. &
1684+ present (print_signs) .or. &
1685+ present (real_format) ) then
1686+
1687+ if (present (compact_reals)) compact_real = compact_reals
1688+
1689+ ! set defaults
1690+ sgn_prnt = .false.
1691+ if ( present ( print_signs) ) sgn_prnt = print_signs
1692+ if ( sgn_prnt ) then
1693+ sgn = ' sp'
1694+ else
1695+ sgn = ' ss'
1696+ end if
16901697
1691- ! optional inputs (if not present, values remains unchanged):
1692- if (present (verbose)) is_verbose = verbose
1693- if (present (compact_reals)) compact_real = compact_reals
1698+ rl_edit_desc = ' E'
1699+ if ( present ( real_format ) ) then
1700+ select case ( real_format )
1701+ case (' g' ,' G' ,' e' ,' E' ,' en' ,' EN' ,' es' ,' ES' )
1702+ rl_edit_desc = real_format
1703+ case default
1704+ call throw_exception(' Invalid real format, "' // trim (real_format) // ' ", passed to json_initialize.' // &
1705+ new_line(' a' ) // ' Acceptable formats are: "G", "E", "EN", and "ES".' )
1706+ end select
1707+ end if
16941708
1695- ! set the default output/input format for reals:
1696- ! [this only needs to be done once, since it can't change]
1697- if (.not. allocated (real_fmt)) then
1709+ ! set the default output/input format for reals:
16981710 write (w,' (ss,I0)' ,iostat= istat) max_numeric_str_len
16991711 if (istat== 0 ) write (d,' (ss,I0)' ,iostat= istat) real_precision
17001712 if (istat== 0 ) write (e,' (ss,I0)' ,iostat= istat) real_exponent_digits
@@ -1703,14 +1715,8 @@ subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
17031715 else
17041716 real_fmt = ' (' // sgn // ' ,' // trim (rl_edit_desc) // ' 30.16E3)' ! just use this one (should never happen)
17051717 end if
1706- end if
17071718
1708- ! Just in case, clear these global variables also:
1709- pushed_index = 0
1710- pushed_char = ' '
1711- char_count = 0
1712- line_count = 1
1713- ipos = 1
1719+ end if
17141720
17151721 end subroutine json_initialize
17161722! *****************************************************************************************
@@ -1815,7 +1821,7 @@ subroutine json_check_for_errors(status_ok, error_msg)
18151821 if (allocated (err_message)) then
18161822 error_msg = err_message
18171823 else
1818- error_msg = ' Unknown Error'
1824+ error_msg = ' Error: json_initialize() must be called first to initialize the module. '
18191825 end if
18201826 else
18211827 error_msg = ' '
0 commit comments