json_initialize Subroutine

private subroutine json_initialize(me, verbose, compact_reals, print_signs, real_format, spaces_per_tab, strict_type_checking, trailing_spaces_significant, case_sensitive_keys, no_whitespace, unescape_strings, comment_char, path_mode, path_separator, compress_vectors)

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).

Modified

  • Izaak Beekman : 02/24/2015

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: me
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 get routines (default is false)

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.

character(kind=CK,len=1), intent(in), optional :: comment_char

If present, this character is used to denote comments in the JSON file, which will be ignored if present. Example: ! or #. Setting this to a blank string disables the ignoring of comments. (Default is !).

integer(kind=IK), intent(in), optional :: path_mode

How the path strings are interpreted in the get_by_path routines: * 1 – Default way (see json_get_by_path_default) [Default] * 2 – as RFC 6901 "JSON Pointer" paths (see json_get_by_path_rfc6901)

character(kind=CK,len=1), intent(in), optional :: path_separator

The path separator to use in the "default" mode for the paths in the various get_by_path routines. Example: . [default] or %. Note: if path_mode/=1 then this is ignored.

logical(kind=LK), intent(in), optional :: compress_vectors

If true, then arrays of integers, nulls, doubles, and logicals are printed all on one line. [Note: no_whitespace will override this option if necessary]. (Default is False).


Source Code


Source Code

    subroutine json_initialize(me,verbose,compact_reals,&
                               print_signs,real_format,spaces_per_tab,&
                               strict_type_checking,&
                               trailing_spaces_significant,&
                               case_sensitive_keys,&
                               no_whitespace,&
                               unescape_strings,&
                               comment_char,&
                               path_mode,&
                               path_separator,&
                               compress_vectors)

    implicit none

    class(json_core),intent(inout)  :: me
#include "json_initialize_arguments.inc"

    character(kind=CDK,len=10) :: w            !! max string length
    character(kind=CDK,len=10) :: d            !! real precision digits
    character(kind=CDK,len=10) :: e            !! real exponent digits
    character(kind=CDK,len=2)  :: sgn          !! sign flag: `ss` or `sp`
    character(kind=CDK,len=2)  :: rl_edit_desc !! `G`, `E`, `EN`, or `ES`
    integer(IK)                :: istat        !! `iostat` flag for write statements
    logical(LK)                :: sgn_prnt     !! print sign flag

    !reset exception to false:
    call me%clear_exceptions()

    !Just in case, clear these global variables also:
    me%pushed_index = 0
    me%pushed_char  = CK_''
    me%char_count   = 0
    me%line_count   = 1
    me%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)) &
        me%spaces_per_tab = spaces_per_tab
    if (present(verbose)) &
        me%is_verbose = verbose
    if (present(strict_type_checking)) &
        me%strict_type_checking = strict_type_checking
    if (present(trailing_spaces_significant)) &
        me%trailing_spaces_significant = trailing_spaces_significant
    if (present(case_sensitive_keys)) &
        me%case_sensitive_keys = case_sensitive_keys
    if (present(no_whitespace)) &
        me%no_whitespace = no_whitespace
    if (present(unescape_strings)) &
        me%unescaped_strings = unescape_strings
    if (present(path_mode)) then
        if (path_mode==1_IK .or. path_mode==2_IK) then
            me%path_mode = path_mode
        else
            me%path_mode = 1_IK  ! just to have a valid value
            call me%throw_exception('Invalid path_mode.')
        end if
    end if

    ! if we are allowing comments in the file:
    ! [an empty string disables comments]
    if (present(comment_char)) then
        me%allow_comments = comment_char/=CK_''
        me%comment_char = comment_char
    end if

    ! path separator:
    if (present(path_separator)) then
        me%path_separator = path_separator
    end if

    ! printing vectors in compressed form:
    if (present(compress_vectors)) then
        me%compress_vectors = compress_vectors
    end if

    !Set the format for real numbers:
    ! [if not changing it, then it remains the same]

    if ( (.not. allocated(me%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
                if (present(compact_reals)) then
                    ! we will also allow for compact reals with
                    ! '*' format, if both arguments are present.
                    me%compact_real = compact_reals
                else
                    me%compact_real = .false.
                end if
                me%real_fmt = star
                return
            end if
        end if

        if (present(compact_reals)) me%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 me%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
            me%real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) //&
                            trim(w) // '.' // trim(d) // 'E' // trim(e) // ')'
        else
            me%real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) // &
                            '27.17E4)'  !just use this one (should never happen)
        end if

    end if

    end subroutine json_initialize


annotate_invalid_json compact_real_string decode_rfc6901 default_comp_ucs4 default_join_ucs4 default_neq_ucs4 destroy_json_core destroy_json_data encode_rfc6901 escape_string get_current_line_from_file_sequential get_current_line_from_file_stream get_json_core_in_file initialize_json_core initialize_json_core_in_file initialize_json_file initialize_json_file_v2 integer_to_string json_add_double_by_path json_add_double_vec_by_path json_add_integer_by_path json_add_integer_vec_by_path json_add_logical_by_path json_add_logical_vec_by_path json_add_member_by_path json_add_string_by_path json_add_string_by_path_path_ascii json_add_string_by_path_value_ascii json_add_string_vec_by_path json_add_string_vec_by_path_path_ascii json_add_string_vec_by_path_value_ascii json_check_for_errors json_clear_exceptions json_clone json_core json_count json_create_by_path json_failed json_file json_file_add_double json_file_add_double_vec json_file_add_integer json_file_add_integer_vec json_file_add_logical json_file_add_logical_vec json_file_add_object json_file_add_string json_file_add_string_path_ascii json_file_add_string_value_ascii json_file_add_string_vec json_file_add_string_vec_path_ascii json_file_add_string_vec_vec_ascii json_file_check_for_errors json_file_clear_exceptions json_file_destroy json_file_failed json_file_get_alloc_string_vec json_file_get_double json_file_get_double_vec json_file_get_integer json_file_get_integer_vec json_file_get_logical json_file_get_logical_vec json_file_get_object json_file_get_root json_file_get_string json_file_get_string_vec json_file_load json_file_load_from_string json_file_move_pointer json_file_print_1 json_file_print_2 json_file_print_error_message json_file_print_to_console json_file_print_to_string json_file_traverse json_file_update_integer json_file_update_logical json_file_update_real json_file_update_string json_file_update_string_name_ascii json_file_update_string_val_ascii json_file_variable_info json_file_variable_matrix_info json_get_alloc_string_vec json_get_alloc_string_vec_by_path json_get_array json_get_array_by_path json_get_by_path json_get_by_path_default json_get_by_path_rfc6901 json_get_double json_get_double_by_path json_get_double_vec json_get_double_vec_by_path json_get_integer json_get_integer_by_path json_get_integer_vec json_get_integer_vec_by_path json_get_logical json_get_logical_by_path json_get_logical_vec json_get_logical_vec_by_path json_get_next json_get_parent json_get_path json_get_previous json_get_string json_get_string_by_path json_get_string_vec json_get_string_vec_by_path json_get_tail json_info json_info_by_path json_initialize json_matrix_info json_matrix_info_by_path json_parse_file json_parse_string json_print_1 json_print_2 json_print_error_message json_string_info json_throw_exception json_traverse json_update_double json_update_integer json_update_logical json_update_string json_update_string_name_ascii json_update_string_val_ascii json_value_add_double json_value_add_double_vec json_value_add_integer json_value_add_integer_vec json_value_add_logical json_value_add_logical_vec json_value_add_member json_value_add_null json_value_add_string json_value_add_string_name_ascii json_value_add_string_val_ascii json_value_add_string_vec json_value_add_string_vec_name_ascii json_value_add_string_vec_val_ascii json_value_clone_func json_value_create json_value_create_array json_value_create_double json_value_create_integer json_value_create_logical json_value_create_null json_value_create_object json_value_create_string json_value_destroy json_value_get_child json_value_get_child_by_index json_value_get_child_by_name json_value_insert_after json_value_insert_after_child_by_index json_value_is_child_of json_value_print json_value_remove json_value_remove_if_present json_value_rename json_value_replace json_value_swap json_value_to_string json_value_validate lowercase_character lowercase_string name_equal parse_array parse_for_chars parse_number parse_object parse_string parse_value pop_char push_char real_to_string replace_string set_json_core_in_file string_to_dble string_to_int string_to_integer string_to_real to_array to_double to_integer to_logical to_null to_object to_string to_uni to_uni_vec to_unicode ucs4_comp_default ucs4_join_default ucs4_neq_default unescape_string valid_json_hex wrap_json_add_double_by_path wrap_json_add_double_vec_by_path wrap_json_add_integer_by_path wrap_json_add_integer_vec_by_path wrap_json_add_logical_by_path wrap_json_add_logical_vec_by_path wrap_json_add_member_by_path wrap_json_add_string_by_path wrap_json_add_string_vec_by_path wrap_json_create_by_path wrap_json_file_add_double wrap_json_file_add_double_vec wrap_json_file_add_integer wrap_json_file_add_integer_vec wrap_json_file_add_logical wrap_json_file_add_logical_vec wrap_json_file_add_object wrap_json_file_add_string wrap_json_file_add_string_vec wrap_json_file_get_alloc_string_vec wrap_json_file_get_double wrap_json_file_get_double_vec wrap_json_file_get_integer wrap_json_file_get_integer_vec wrap_json_file_get_logical wrap_json_file_get_logical_vec wrap_json_file_get_object wrap_json_file_get_string wrap_json_file_get_string_vec wrap_json_file_load_from_string wrap_json_file_update_integer wrap_json_file_update_logical wrap_json_file_update_real wrap_json_file_update_string wrap_json_file_variable_info wrap_json_file_variable_matrix_info wrap_json_get_alloc_string_vec_by_path wrap_json_get_array_by_path wrap_json_get_by_path wrap_json_get_double_by_path wrap_json_get_double_vec_by_path wrap_json_get_integer_by_path wrap_json_get_integer_vec_by_path wrap_json_get_logical_by_path wrap_json_get_logical_vec_by_path wrap_json_get_path wrap_json_get_string_by_path wrap_json_get_string_vec_by_path wrap_json_info_by_path wrap_json_matrix_info_by_path wrap_json_parse_string wrap_json_throw_exception wrap_json_update_double wrap_json_update_integer wrap_json_update_logical wrap_json_update_string wrap_json_value_add_double wrap_json_value_add_double_vec wrap_json_value_add_integer wrap_json_value_add_integer_vec wrap_json_value_add_logical wrap_json_value_add_logical_vec wrap_json_value_add_null wrap_json_value_add_string wrap_json_value_add_string_vec wrap_json_value_create_array wrap_json_value_create_double wrap_json_value_create_integer wrap_json_value_create_logical wrap_json_value_create_null wrap_json_value_create_object wrap_json_value_create_string wrap_json_value_get_child_by_name wrap_json_value_remove_if_present wrap_json_value_rename