json_parse_file Subroutine

private subroutine json_parse_file(file, p, unit)

Arguments

Type IntentOptional AttributesName
character(kind=CDK,len=*), intent(in) :: file

JSON file name

type(json_value), , pointer:: p

output structure

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

file unit number (/= 0)

Description

Parse the JSON file and populate the json_value tree.

Inputs

The inputs can be:

  • file and unit : the specified unit is used to read JSON from file. [note if unit is already open, then the filename is ignored]
  • file : JSON is read from file using internal unit number

Example

    type(json_value),pointer :: p
    call json_parse(file='myfile.json', p=p)

History

  • Jacob Williams : 01/13/2015 : added read from string option.
  • Izaak Beekman : 03/08/2015 : moved read from string to separate subroutine, and error annotation to separate subroutine.

Calls

proc~~json_parse_file~~CallsGraph proc~json_parse_file json_parse_file proc~annotate_invalid_json annotate_invalid_json proc~json_parse_file->proc~annotate_invalid_json proc~json_initialize json_initialize proc~json_parse_file->proc~json_initialize interface~throw_exception throw_exception proc~json_parse_file->interface~throw_exception proc~parse_value parse_value proc~json_parse_file->proc~parse_value proc~json_value_create json_value_create proc~json_parse_file->proc~json_value_create proc~get_current_line_from_file_sequential get_current_line_from_file_sequential proc~annotate_invalid_json->proc~get_current_line_from_file_sequential proc~integer_to_string integer_to_string proc~annotate_invalid_json->proc~integer_to_string proc~get_current_line_from_file_stream get_current_line_from_file_stream proc~annotate_invalid_json->proc~get_current_line_from_file_stream proc~json_initialize->interface~throw_exception proc~json_clear_exceptions json_clear_exceptions proc~json_initialize->proc~json_clear_exceptions proc~json_throw_exception json_throw_exception interface~throw_exception->proc~json_throw_exception proc~parse_value->interface~throw_exception proc~to_string to_string proc~parse_value->proc~to_string proc~parse_array parse_array proc~parse_value->proc~parse_array proc~pop_char pop_char proc~parse_value->proc~pop_char proc~parse_number parse_number proc~parse_value->proc~parse_number proc~parse_string parse_string proc~parse_value->proc~parse_string proc~to_logical to_logical proc~parse_value->proc~to_logical proc~push_char push_char proc~parse_value->proc~push_char proc~to_object to_object proc~parse_value->proc~to_object proc~to_array to_array proc~parse_value->proc~to_array proc~parse_for_chars parse_for_chars proc~parse_value->proc~parse_for_chars proc~to_null to_null proc~parse_value->proc~to_null proc~parse_object parse_object proc~parse_value->proc~parse_object proc~destroy_json_data destroy_json_data proc~to_string->proc~destroy_json_data proc~parse_array->interface~throw_exception proc~parse_array->proc~parse_value proc~parse_array->proc~json_value_create proc~parse_array->proc~pop_char interface~json_destroy json_destroy proc~parse_array->interface~json_destroy interface~json_add json_add proc~parse_array->interface~json_add proc~parse_number->interface~throw_exception proc~parse_number->proc~pop_char proc~parse_number->proc~push_char proc~to_double to_double proc~parse_number->proc~to_double proc~to_integer to_integer proc~parse_number->proc~to_integer proc~string_to_integer string_to_integer proc~parse_number->proc~string_to_integer proc~string_to_double string_to_double proc~parse_number->proc~string_to_double proc~parse_string->interface~throw_exception proc~parse_string->proc~pop_char proc~valid_json_hex valid_json_hex proc~parse_string->proc~valid_json_hex proc~to_logical->proc~destroy_json_data proc~push_char->interface~throw_exception proc~push_char->proc~integer_to_string proc~to_object->proc~destroy_json_data proc~to_array->proc~destroy_json_data proc~parse_for_chars->interface~throw_exception proc~parse_for_chars->proc~pop_char proc~to_null->proc~destroy_json_data proc~parse_object->interface~throw_exception proc~parse_object->proc~parse_value proc~parse_object->proc~json_value_create proc~parse_object->proc~pop_char proc~parse_object->proc~parse_string proc~parse_object->proc~parse_object proc~parse_object->interface~json_destroy proc~parse_object->interface~json_add proc~json_value_destroy json_value_destroy interface~json_destroy->proc~json_value_destroy proc~json_value_add_logical json_value_add_logical interface~json_add->proc~json_value_add_logical proc~json_value_add_double json_value_add_double interface~json_add->proc~json_value_add_double proc~json_value_add_member json_value_add_member interface~json_add->proc~json_value_add_member proc~json_value_add_integer json_value_add_integer interface~json_add->proc~json_value_add_integer proc~json_value_add_double_vec json_value_add_double_vec interface~json_add->proc~json_value_add_double_vec proc~json_value_add_logical_vec json_value_add_logical_vec interface~json_add->proc~json_value_add_logical_vec proc~json_value_add_integer_vec json_value_add_integer_vec interface~json_add->proc~json_value_add_integer_vec proc~json_value_add_string_vec json_value_add_string_vec interface~json_add->proc~json_value_add_string_vec proc~json_value_add_string json_value_add_string interface~json_add->proc~json_value_add_string proc~json_value_destroy->proc~destroy_json_data proc~json_value_destroy->proc~json_value_destroy proc~json_value_add_logical->proc~json_value_create proc~json_value_add_logical->proc~to_logical proc~json_value_add_logical->interface~json_add proc~json_value_add_double->proc~json_value_create proc~json_value_add_double->interface~json_add proc~json_value_add_double->proc~to_double proc~json_value_add_integer->proc~json_value_create proc~json_value_add_integer->interface~json_add proc~json_value_add_integer->proc~to_integer proc~json_value_add_double_vec->proc~json_value_create proc~json_value_add_double_vec->proc~to_array proc~json_value_add_double_vec->interface~json_add proc~json_value_add_logical_vec->proc~json_value_create proc~json_value_add_logical_vec->proc~to_array proc~json_value_add_logical_vec->interface~json_add proc~json_value_add_integer_vec->proc~json_value_create proc~json_value_add_integer_vec->proc~to_array proc~json_value_add_integer_vec->interface~json_add proc~json_value_add_string_vec->proc~json_value_create proc~json_value_add_string_vec->proc~to_array proc~json_value_add_string_vec->interface~json_add proc~json_value_add_string->proc~json_value_create proc~json_value_add_string->proc~to_string proc~json_value_add_string->interface~json_add proc~escape_string escape_string proc~json_value_add_string->proc~escape_string proc~to_double->proc~destroy_json_data proc~to_integer->proc~destroy_json_data proc~string_to_integer->interface~throw_exception proc~string_to_double->interface~throw_exception
Help

Called By

proc~~json_parse_file~~CalledByGraph proc~json_parse_file json_parse_file interface~json_parse json_parse interface~json_parse->proc~json_parse_file proc~test_14 test_14 proc~test_14->interface~json_parse proc~test_8 test_8 proc~test_8->interface~json_parse proc~json_file_load json_file_load proc~json_file_load->interface~json_parse proc~json_file_load_from_string json_file_load_from_string proc~json_file_load_from_string->interface~json_parse program~jf_test_14 jf_test_14 program~jf_test_14->proc~test_14 program~jf_test_8 jf_test_8 program~jf_test_8->proc~test_8 proc~wrap_json_file_load_from_string wrap_json_file_load_from_string proc~wrap_json_file_load_from_string->proc~json_file_load_from_string
Help

Variables

TypeVisibility AttributesNameInitial
integer(kind=IK), public :: iunit
integer(kind=IK), public :: istat
logical(kind=LK), public :: is_open

Source Code

    subroutine json_parse_file(file, p, unit)

    implicit none

    character(kind=CDK,len=*),intent(in) :: file  !! JSON file name
    type(json_value),pointer             :: p     !! output structure
    integer(IK),intent(in),optional      :: unit  !! file unit number (/= 0)

    integer(IK) :: iunit, istat
    logical(LK) :: is_open

    !clear any exceptions and initialize:
    call json_initialize()

    if ( present(unit) ) then

        if (unit==0) then
            call throw_exception('Error in json_parse_file: unit number must not be 0.')
            return
        end if

        iunit = unit

        !check to see if the file is already open
        ! if it is, then use it, otherwise open the file with the name given.
        inquire(unit=iunit, opened=is_open, iostat=istat)
        if (istat==0 .and. .not. is_open) then
           ! open the file
            open (  unit        = iunit, &
                    file        = file, &
                    status      = 'OLD', &
                    action      = 'READ', &
                    form        = form_spec, &
                    access      = access_spec, &
                    iostat      = istat &
                    FILE_ENCODING )
        else
            !if the file is already open, then we need to make sure
            ! that it is open with the correct form/access/etc...
        end if

    else

        ! open the file with a new unit number:
        open (  newunit     = iunit, &
                file        = file, &
                status      = 'OLD', &
                action      = 'READ', &
                form        = form_spec, &
                access      = access_spec, &
                iostat      = istat &
                FILE_ENCODING )

    end if

    if (istat==0) then

        ! create the value and associate the pointer
        call json_value_create(p)

        ! Note: the name of the root json_value doesn't really matter,
        !  but we'll allocate something here just in case.
        p%name = trim(file)  !use the file name

        ! parse as a value
        call parse_value(unit=iunit, str=CK_'', value=p)
        if (exception_thrown) call annotate_invalid_json(iunit,CK_'')

        ! close the file if necessary
        close(unit=iunit, iostat=istat)

    else

        call throw_exception('Error in json_parse_file: Error opening file: '//trim(file))
        nullify(p)

    end if

    end subroutine json_parse_file