Initialize the json_core instance.
The routine may be called before any of the json_core methods are used in order to specify certain parameters. If it is not called, then the defaults are used. This routine is also called internally by various routines. It can also be called to clear exceptions, or to reset some of the variables (note that only the arguments present are changed).
initialize_json_core, json_initialize, initialize_json_core_in_file, and initialize_json_file all have a similar interface.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(json_core), | intent(inout) | :: | json | |||
logical(kind=LK), | intent(in), | optional | :: | verbose | mainly useful for debugging (default is false) |
|
logical(kind=LK), | intent(in), | optional | :: | compact_reals | to compact the real number strings for output (default is true) |
|
logical(kind=LK), | intent(in), | optional | :: | print_signs | always print numeric sign (default is false) |
|
character(kind=CDK,len=*), | intent(in), | optional | :: | real_format | Real number format: 'E' [default], '*', 'G', 'EN', or 'ES' |
|
integer(kind=IK), | intent(in), | optional | :: | spaces_per_tab | number of spaces per tab for indenting (default is 2) |
|
logical(kind=LK), | intent(in), | optional | :: | strict_type_checking | if true, no integer, double, or logical type
conversions are done for the |
|
logical(kind=LK), | intent(in), | optional | :: | trailing_spaces_significant | for name and path comparisons, is trailing space to be considered significant. (default is false) |
|
logical(kind=LK), | intent(in), | optional | :: | case_sensitive_keys | for name and path comparisons, are they case sensitive. (default is true) |
|
logical(kind=LK), | intent(in), | optional | :: | no_whitespace | if true, printing the JSON structure is done without adding any non-significant spaces or linebreaks (default is false) |
|
logical(kind=LK), | intent(in), | optional | :: | unescape_strings | If false, then the raw escaped string is returned from json_get_string and similar routines. If true [default], then the string is returned unescaped. |
subroutine json_initialize(json,verbose,compact_reals,&
print_signs,real_format,spaces_per_tab,&
strict_type_checking,&
trailing_spaces_significant,&
case_sensitive_keys,&
no_whitespace,&
unescape_strings)
implicit none
class(json_core),intent(inout) :: json
logical(LK),intent(in),optional :: verbose !! mainly useful for debugging (default is false)
logical(LK),intent(in),optional :: compact_reals !! to compact the real number strings for output (default is true)
logical(LK),intent(in),optional :: print_signs !! always print numeric sign (default is false)
character(kind=CDK,len=*),intent(in),optional :: real_format !! Real number format: 'E' [default], '*', 'G', 'EN', or 'ES'
integer(IK),intent(in),optional :: spaces_per_tab !! number of spaces per tab for indenting (default is 2)
logical(LK),intent(in),optional :: strict_type_checking !! if true, no integer, double, or logical type
!! conversions are done for the `get` routines
!! (default is false)
logical(LK),intent(in),optional :: trailing_spaces_significant !! for name and path comparisons, is trailing
!! space to be considered significant.
!! (default is false)
logical(LK),intent(in),optional :: case_sensitive_keys !! for name and path comparisons, are they
!! case sensitive. (default is true)
logical(LK),intent(in),optional :: no_whitespace !! if true, printing the JSON structure is
!! done without adding any non-significant
!! spaces or linebreaks (default is false)
logical(LK),intent(in),optional :: unescape_strings !! If false, then the raw escaped
!! string is returned from [[json_get_string]]
!! and similar routines. If true [default],
!! then the string is returned unescaped.
character(kind=CDK,len=10) :: w,d,e
character(kind=CDK,len=2) :: sgn, rl_edit_desc
integer(IK) :: istat
logical(LK) :: sgn_prnt
!reset exception to false:
call json%clear_exceptions()
!Just in case, clear these global variables also:
json%pushed_index = 0
json%pushed_char = ''
json%char_count = 0
json%line_count = 1
json%ipos = 1
#ifdef USE_UCS4
! reopen stdout and stderr with utf-8 encoding
open(output_unit,encoding='utf-8')
open(error_unit, encoding='utf-8')
#endif
!various optional inputs:
if (present(spaces_per_tab)) &
json%spaces_per_tab = spaces_per_tab
if (present(verbose)) &
json%is_verbose = verbose
if (present(strict_type_checking)) &
json%strict_type_checking = strict_type_checking
if (present(trailing_spaces_significant)) &
json%trailing_spaces_significant = trailing_spaces_significant
if (present(case_sensitive_keys)) &
json%case_sensitive_keys = case_sensitive_keys
if (present(no_whitespace)) &
json%no_whitespace = no_whitespace
if (present(unescape_strings)) &
json%unescaped_strings = unescape_strings
!Set the format for real numbers:
! [if not changing it, then it remains the same]
if ( (.not. allocated(json%real_fmt)) .or. & ! if this hasn't been done yet
present(compact_reals) .or. &
present(print_signs) .or. &
present(real_format) ) then
!allow the special case where real format is '*':
! [this overrides the other options]
if (present(real_format)) then
if (real_format==star) then
json%compact_real = .false.
json%real_fmt = star
return
end if
end if
if (present(compact_reals)) json%compact_real = compact_reals
!set defaults
sgn_prnt = .false.
if ( present( print_signs) ) sgn_prnt = print_signs
if ( sgn_prnt ) then
sgn = 'sp'
else
sgn = 'ss'
end if
rl_edit_desc = 'E'
if ( present( real_format ) ) then
select case ( real_format )
case ('g','G','e','E','en','EN','es','ES')
rl_edit_desc = real_format
case default
call json%throw_exception('Invalid real format, "' // &
trim(real_format) // '", passed to json_initialize.'// &
new_line('a') // 'Acceptable formats are: "G", "E", "EN", and "ES".' )
end select
end if
! set the default output/input format for reals:
write(w,'(ss,I0)',iostat=istat) max_numeric_str_len
if (istat==0) write(d,'(ss,I0)',iostat=istat) real_precision
if (istat==0) write(e,'(ss,I0)',iostat=istat) real_exponent_digits
if (istat==0) then
json%real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) //&
trim(w) // '.' // trim(d) // 'E' // trim(e) // ')'
else
json%real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) // &
'30.16E3)' !just use this one (should never happen)
end if
end if
end subroutine json_initialize