test_1 Subroutine

public subroutine test_1(error_cnt)

Arguments

Type IntentOptional AttributesName
integer, intent(out) :: error_cnt

Description

Read a sample JSON file and retrieve some data from it

Calls

proc~~test_1~~CallsGraph proc~test_1 test_1 interface~json_remove json_remove proc~test_1->interface~json_remove proc~json_initialize json_initialize proc~test_1->proc~json_initialize interface~json_update json_update proc~test_1->interface~json_update proc~json_failed json_failed proc~test_1->proc~json_failed proc~json_print_error_message json_print_error_message proc~test_1->proc~json_print_error_message proc~json_value_remove json_value_remove interface~json_remove->proc~json_value_remove proc~json_clear_exceptions json_clear_exceptions proc~json_initialize->proc~json_clear_exceptions interface~throw_exception throw_exception proc~json_initialize->interface~throw_exception proc~json_update_logical json_update_logical interface~json_update->proc~json_update_logical proc~json_update_string json_update_string interface~json_update->proc~json_update_string proc~json_update_integer json_update_integer interface~json_update->proc~json_update_integer proc~json_update_double json_update_double interface~json_update->proc~json_update_double proc~json_print_error_message->proc~json_clear_exceptions proc~json_check_for_errors json_check_for_errors proc~json_print_error_message->proc~json_check_for_errors proc~json_value_destroy json_value_destroy proc~json_value_remove->proc~json_value_destroy proc~json_value_destroy->proc~json_value_destroy proc~destroy_json_data destroy_json_data proc~json_value_destroy->proc~destroy_json_data proc~json_throw_exception json_throw_exception interface~throw_exception->proc~json_throw_exception proc~json_update_logical->interface~throw_exception interface~json_get json_get proc~json_update_logical->interface~json_get interface~json_add json_add proc~json_update_logical->interface~json_add proc~to_logical to_logical proc~json_update_logical->proc~to_logical proc~json_info json_info proc~json_update_logical->proc~json_info proc~json_update_string->interface~throw_exception proc~json_update_string->interface~json_get proc~json_update_string->interface~json_add proc~json_update_string->proc~json_info proc~to_string to_string proc~json_update_string->proc~to_string proc~json_update_integer->interface~throw_exception proc~json_update_integer->interface~json_get proc~json_update_integer->interface~json_add proc~json_update_integer->proc~json_info proc~to_integer to_integer proc~json_update_integer->proc~to_integer proc~json_update_double->interface~throw_exception proc~json_update_double->interface~json_get proc~json_update_double->interface~json_add proc~json_update_double->proc~json_info proc~to_double to_double proc~json_update_double->proc~to_double proc~json_get_string json_get_string interface~json_get->proc~json_get_string proc~json_get_double_vec_with_path json_get_double_vec_with_path interface~json_get->proc~json_get_double_vec_with_path proc~json_get_logical json_get_logical interface~json_get->proc~json_get_logical proc~json_get_integer_vec_with_path json_get_integer_vec_with_path interface~json_get->proc~json_get_integer_vec_with_path proc~json_get_logical_vec_with_path json_get_logical_vec_with_path interface~json_get->proc~json_get_logical_vec_with_path proc~json_get_array json_get_array interface~json_get->proc~json_get_array proc~json_get_double json_get_double interface~json_get->proc~json_get_double proc~json_get_string_vec_with_path json_get_string_vec_with_path interface~json_get->proc~json_get_string_vec_with_path proc~json_get_by_path json_get_by_path interface~json_get->proc~json_get_by_path proc~json_get_integer json_get_integer interface~json_get->proc~json_get_integer proc~json_get_double_with_path json_get_double_with_path interface~json_get->proc~json_get_double_with_path proc~json_get_logical_with_path json_get_logical_with_path interface~json_get->proc~json_get_logical_with_path proc~json_get_integer_with_path json_get_integer_with_path interface~json_get->proc~json_get_integer_with_path proc~json_get_logical_vec json_get_logical_vec interface~json_get->proc~json_get_logical_vec proc~json_get_string_with_path json_get_string_with_path interface~json_get->proc~json_get_string_with_path proc~json_get_string_vec json_get_string_vec interface~json_get->proc~json_get_string_vec proc~json_get_integer_vec json_get_integer_vec interface~json_get->proc~json_get_integer_vec proc~json_get_array_with_path json_get_array_with_path interface~json_get->proc~json_get_array_with_path proc~json_get_double_vec json_get_double_vec interface~json_get->proc~json_get_double_vec 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~to_logical->proc~destroy_json_data proc~json_count json_count proc~json_info->proc~json_count proc~json_get_string->interface~throw_exception proc~unescape_string unescape_string proc~json_get_string->proc~unescape_string proc~json_get_double_vec_with_path->interface~json_get proc~json_get_logical->interface~throw_exception proc~json_get_integer_vec_with_path->interface~json_get proc~json_get_logical_vec_with_path->interface~json_get proc~json_get_array->interface~throw_exception proc~json_get_array->proc~json_count proc~json_get_double->interface~throw_exception proc~json_get_string_vec_with_path->interface~json_get proc~json_get_by_path->proc~json_clear_exceptions proc~json_get_by_path->interface~throw_exception interface~json_get_child json_get_child proc~json_get_by_path->interface~json_get_child proc~string_to_integer string_to_integer proc~json_get_by_path->proc~string_to_integer proc~json_get_integer->interface~throw_exception proc~json_get_double_with_path->proc~json_clear_exceptions proc~json_get_double_with_path->interface~throw_exception proc~json_get_double_with_path->proc~json_get_double proc~json_get_double_with_path->proc~json_get_by_path proc~json_get_logical_with_path->proc~json_clear_exceptions proc~json_get_logical_with_path->interface~throw_exception proc~json_get_logical_with_path->proc~json_get_logical proc~json_get_logical_with_path->proc~json_get_by_path proc~json_get_integer_with_path->proc~json_clear_exceptions proc~json_get_integer_with_path->interface~throw_exception proc~json_get_integer_with_path->proc~json_get_by_path proc~json_get_integer_with_path->proc~json_get_integer proc~json_get_logical_vec->interface~json_get proc~json_get_string_with_path->proc~json_clear_exceptions proc~json_get_string_with_path->interface~throw_exception proc~json_get_string_with_path->proc~json_get_string proc~json_get_string_with_path->proc~json_get_by_path proc~json_get_string_vec->interface~json_get proc~json_get_integer_vec->interface~json_get proc~json_get_array_with_path->proc~json_clear_exceptions proc~json_get_array_with_path->interface~throw_exception proc~json_get_array_with_path->proc~json_get_array proc~json_get_array_with_path->proc~json_get_by_path proc~json_get_double_vec->interface~json_get proc~unescape_string->interface~throw_exception interface~to_unicode to_unicode proc~unescape_string->interface~to_unicode proc~to_uni to_uni interface~to_unicode->proc~to_uni proc~to_uni_vec to_uni_vec interface~to_unicode->proc~to_uni_vec proc~json_value_get_by_index json_value_get_by_index interface~json_get_child->proc~json_value_get_by_index proc~json_value_get_by_name_chars json_value_get_by_name_chars interface~json_get_child->proc~json_value_get_by_name_chars proc~string_to_integer->interface~throw_exception proc~json_value_get_by_index->interface~throw_exception proc~json_value_get_by_name_chars->interface~throw_exception proc~json_value_get_by_name_chars->proc~json_count proc~json_value_add_logical->interface~json_add proc~json_value_add_logical->proc~to_logical proc~json_value_create json_value_create proc~json_value_add_logical->proc~json_value_create proc~json_value_add_double->interface~json_add proc~json_value_add_double->proc~json_value_create proc~json_value_add_double->proc~to_double proc~json_value_add_integer->interface~json_add proc~json_value_add_integer->proc~json_value_create proc~json_value_add_integer->proc~to_integer proc~json_value_add_double_vec->interface~json_add proc~json_value_add_double_vec->proc~json_value_create proc~to_array to_array proc~json_value_add_double_vec->proc~to_array proc~json_value_add_logical_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_integer_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_string_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->interface~json_add proc~json_value_add_string->proc~json_value_create proc~escape_string escape_string proc~json_value_add_string->proc~escape_string proc~json_value_add_string->proc~to_string proc~to_double->proc~destroy_json_data proc~to_integer->proc~destroy_json_data proc~to_array->proc~destroy_json_data proc~to_string->proc~destroy_json_data
Help

Called By

proc~~test_1~~CalledByGraph proc~test_1 test_1 program~jf_test_1 jf_test_1 program~jf_test_1->proc~test_1
Help

Variables

TypeVisibility AttributesNameInitial
type(json_file), public :: json
integer, public :: ival
character(kind=CK,len=:), public, allocatable:: cval
real(kind=wp), public :: rval
logical, public :: found
type(json_value), public, pointer:: p

Source Code

    subroutine test_1(error_cnt)

    !! Read a sample JSON file and retrieve some data from it

    implicit none

    integer,intent(out) :: error_cnt
    type(json_file) :: json    !the JSON structure read from the file:
    integer :: ival
    character(kind=CK,len=:),allocatable :: cval
    real(wp) :: rval
    logical :: found
    type(json_value),pointer :: p

    error_cnt = 0
    call json_initialize()
    if (json_failed()) then
      call json_print_error_message(error_unit)
      error_cnt = error_cnt + 1
    end if

    write(error_unit,'(A)') ''
    write(error_unit,'(A)') '================================='
    write(error_unit,'(A)') '   TEST 1'
    write(error_unit,'(A)') '================================='
    write(error_unit,'(A)') ''

    ! parse the json file:
    write(error_unit,'(A)') ''
    write(error_unit,'(A)') 'parsing file '//dir//filename1

    call json%load_file(filename = dir//filename1)

    if (json_failed()) then    !if there was an error reading the file

      call json_print_error_message(error_unit)
      error_cnt = error_cnt + 1

    else

      ! print the parsed data to the console
      write(error_unit,'(A)') ''
      write(error_unit,'(A)') 'printing the file...'
      write(output_unit,'(A)') '{ "part a" :' !Wrap 3 outputs to make stdout valid json
      call json%print_file()
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      ! extract data from the parsed value
      write(error_unit,'(A)') ''
      write(error_unit,'(A)') 'get some data from the file...'

      write(error_unit,'(A)') ''
      call json%get('version.svn', ival)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A,I5)') 'version.svn = ',ival
      end if

      write(error_unit,'(A)') ''
      call json%get('data(1).array(2)', cval)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A)') 'data(1).array(2) = '//trim(cval)
      end if

      write(error_unit,'(A)') ''
      call json%get('files(1)', cval)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A)') 'files(1) = '//trim(cval)
      end if

      write(error_unit,'(A)') ''
      call json%get('files(2)', cval)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A)') 'files(2) = '//trim(cval)
      end if

      write(error_unit,'(A)') ''
      call json%get('files(3)', cval)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A)') 'files(3) = '//trim(cval)
      end if

      write(error_unit,'(A)') ''
      call json%get('data(2).real', rval)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A,E30.16)') 'data(2).real = ',rval
      end if

      write(error_unit,'(A)') ''
      call json%get('files[4]', cval)      !has hex characters
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A)') 'files[4] = '//trim(cval)
      end if

      write(error_unit,'(A)') ''
      call json%get('files[5]', cval)      !string with spaces and no escape characters
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      else
        write(error_unit,'(A)') 'files[5] = '//trim(cval)
      end if

      !
      ! Test of values that aren't there:
      ! Note: when using the "found" output, the exceptions are cleared automatically.
      !

      write(error_unit,'(A)') ''
      call json%get('files[10]', cval, found)      !value that isn't there
      if (.not. found) then
        write(error_unit,'(A)') 'files[10] not in file.'
      else
        write(error_unit,'(1x,A)') 'files[10] = '//trim(cval)
        error_cnt = error_cnt + 1
      end if

      write(error_unit,'(A)') ''
      call json%get('version.blah', ival, found)      !value that isn't there
      if (.not. found) then
        write(error_unit,'(A)') 'version.blah not in file.'
      else
        write(error_unit,'(A)') 'version.blah = ',ival
        error_cnt = error_cnt + 1
      end if

      write(error_unit,'(A)') ''
      write(error_unit,'(A)') ' Test removing data from the json structure:'

      call json%get('files', p)         !in the middle of a list
      call json_remove(p)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      call json%get('data(1).array', p)   !at the end of a list
      call json_remove(p)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      call json%get('data(2).number', p)  !at the beginning of a list
      call json_remove(p)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      write(error_unit,'(A)') ''
      write(error_unit,'(A)') 'printing the modified structure...'
      write(output_unit,'(A)') ', "part b" : '
      call json%print_file()
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      write(error_unit,'(A)') ''
      write(error_unit,'(A)') ' Test replacing data from the json structure:'

      call json%get('data(1)', p)
      call json_update(p,'name','Cuthbert',found)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      !call json%get('data(2)', p)
      !call json_update(p,'real',[1.0_wp, 2.0_wp, 3.0_wp],found)   !don't have one like this yet...

      !use the json_file procedure to update a variable:
      call json%update('version.svn',999,found)
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

      write(error_unit,'(A)') ''
      write(error_unit,'(A)') 'printing the modified structure...'
      write(output_unit,'(A)') ', "part c" : '
      call json%print_file()
      write(output_unit,'(A)') '}'
      if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
      end if

    end if

    ! clean up
    write(error_unit,'(A)') ''
    write(error_unit,'(A)') 'destroy...'
    call json%destroy()
    if (json_failed()) then
      call json_print_error_message(error_unit)
      error_cnt = error_cnt + 1
    end if

    end subroutine test_1