test_2 Subroutine

public subroutine test_2(error_cnt)

Arguments

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

Description

Populate a JSON structure and write it to a file.

Calls

proc~~test_2~~CallsGraph proc~test_2 test_2 interface~json_print json_print proc~test_2->interface~json_print proc~json_failed json_failed proc~test_2->proc~json_failed interface~json_create_array json_create_array proc~test_2->interface~json_create_array proc~json_get_next json_get_next proc~test_2->proc~json_get_next proc~json_get_parent json_get_parent proc~test_2->proc~json_get_parent interface~json_destroy json_destroy proc~test_2->interface~json_destroy proc~add_variables_to_input add_variables_to_input proc~test_2->proc~add_variables_to_input proc~json_print_error_message json_print_error_message proc~test_2->proc~json_print_error_message interface~json_update json_update proc~test_2->interface~json_update proc~json_initialize json_initialize proc~test_2->proc~json_initialize proc~json_get_tail json_get_tail proc~test_2->proc~json_get_tail proc~json_clone json_clone proc~test_2->proc~json_clone interface~json_add json_add proc~test_2->interface~json_add proc~json_info json_info proc~test_2->proc~json_info interface~json_create_object json_create_object proc~test_2->interface~json_create_object interface~json_get json_get proc~test_2->interface~json_get proc~json_get_previous json_get_previous proc~test_2->proc~json_get_previous proc~json_print_1 json_print_1 interface~json_print->proc~json_print_1 proc~json_print_2 json_print_2 interface~json_print->proc~json_print_2 proc~json_value_create_array json_value_create_array interface~json_create_array->proc~json_value_create_array proc~json_value_destroy json_value_destroy interface~json_destroy->proc~json_value_destroy proc~add_variables_to_input->proc~json_failed proc~add_variables_to_input->proc~json_print_error_message proc~add_variables_to_input->interface~json_add proc~add_variables_to_input->interface~json_create_object proc~json_check_for_errors json_check_for_errors proc~json_print_error_message->proc~json_check_for_errors proc~json_clear_exceptions json_clear_exceptions proc~json_print_error_message->proc~json_clear_exceptions 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 interface~throw_exception throw_exception proc~json_initialize->interface~throw_exception proc~json_initialize->proc~json_clear_exceptions proc~json_value_clone_func json_value_clone_func proc~json_clone->proc~json_value_clone_func 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_count json_count proc~json_info->proc~json_count proc~json_value_create_object json_value_create_object interface~json_create_object->proc~json_value_create_object 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_print json_value_print proc~json_print_1->proc~json_value_print proc~json_print_1->interface~throw_exception proc~json_print_2->interface~json_print proc~json_print_2->interface~throw_exception proc~json_value_print->proc~json_value_print proc~json_value_print->interface~throw_exception proc~real_to_string real_to_string proc~json_value_print->proc~real_to_string proc~json_value_print->proc~json_count proc~integer_to_string integer_to_string proc~json_value_print->proc~integer_to_string none~write_it write_it proc~json_value_print->none~write_it proc~json_throw_exception json_throw_exception interface~throw_exception->proc~json_throw_exception proc~compact_real_string compact_real_string proc~real_to_string->proc~compact_real_string proc~json_value_create json_value_create proc~json_value_create_array->proc~json_value_create proc~to_array to_array proc~json_value_create_array->proc~to_array proc~destroy_json_data destroy_json_data proc~to_array->proc~destroy_json_data proc~json_value_destroy->proc~destroy_json_data proc~json_value_destroy->proc~json_value_destroy proc~json_update_logical->interface~json_add proc~json_update_logical->proc~json_info proc~json_update_logical->interface~json_get proc~json_update_logical->interface~throw_exception proc~to_logical to_logical proc~json_update_logical->proc~to_logical proc~json_update_string->interface~json_add proc~json_update_string->proc~json_info proc~json_update_string->interface~json_get proc~json_update_string->interface~throw_exception proc~to_string to_string proc~json_update_string->proc~to_string proc~json_update_integer->interface~json_add proc~json_update_integer->proc~json_info proc~json_update_integer->interface~json_get proc~json_update_integer->interface~throw_exception proc~to_integer to_integer proc~json_update_integer->proc~to_integer proc~json_update_double->interface~json_add proc~json_update_double->proc~json_info proc~json_update_double->interface~json_get proc~json_update_double->interface~throw_exception proc~to_double to_double proc~json_update_double->proc~to_double proc~to_logical->proc~destroy_json_data proc~to_string->proc~destroy_json_data proc~to_integer->proc~destroy_json_data proc~to_double->proc~destroy_json_data proc~json_value_clone_func->proc~json_value_clone_func proc~json_value_add_logical->interface~json_add proc~json_value_add_logical->proc~json_value_create proc~json_value_add_logical->proc~to_logical 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~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~json_value_add_string->proc~to_string proc~escape_string escape_string proc~json_value_add_string->proc~escape_string proc~json_value_create_object->proc~json_value_create proc~to_object to_object proc~json_value_create_object->proc~to_object proc~to_object->proc~destroy_json_data 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->interface~throw_exception proc~json_get_by_path->proc~json_clear_exceptions 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->interface~throw_exception proc~json_get_double_with_path->proc~json_clear_exceptions 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->interface~throw_exception proc~json_get_logical_with_path->proc~json_clear_exceptions 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->interface~throw_exception proc~json_get_integer_with_path->proc~json_clear_exceptions 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->interface~throw_exception proc~json_get_string_with_path->proc~json_clear_exceptions 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->interface~throw_exception proc~json_get_array_with_path->proc~json_clear_exceptions 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
Help

Called By

proc~~test_2~~CalledByGraph proc~test_2 test_2 program~jf_test_2 jf_test_2 program~jf_test_2->proc~test_2
Help

Variables

TypeVisibility AttributesNameInitial
type(json_value), public, pointer:: p
type(json_value), public, pointer:: inp
type(json_value), public, pointer:: traj
type(json_value), public, pointer:: p_tmp
type(json_value), public, pointer:: p_integer_array
type(json_value), public, pointer:: p_clone
integer, public :: iunit
character(kind=CK,len=:), public, allocatable:: name
integer, public :: ival
integer, public :: ival_clone
logical, public :: found

Source Code

    subroutine test_2(error_cnt)

    !! Populate a JSON structure and write it to a file.

    implicit none

    integer,intent(out) :: error_cnt

    type(json_value),pointer :: p, inp, traj, p_tmp, p_integer_array, p_clone

    integer :: iunit
    character(kind=CK,len=:),allocatable :: name
    integer :: ival,ival_clone
    logical :: found

    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)') '   EXAMPLE 2'
    write(error_unit,'(A)') '================================='
    write(error_unit,'(A)') ''

    !root:
    call json_create_object(p,dir//filename2)    ! create the value and associate the pointer
                                                 ! add the file name as the name of the overall structure
    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)') 'initialize the structure...'

    !config structure:
    call json_create_object(inp,'inputs')   !an object
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(p, inp)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if

    !trajectory structure:
    call json_create_array(traj,'trajectory')    !an array
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(p, traj)
    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)') 'adding some data to structure...'

    !add some variables:

    !input variables:
    call json_add(inp, 't0', 0.1_wp)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'tf', 1.1_wp)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'x0', 9999.000_wp)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'integer_scalar', 1)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'integer_array', [2,4,99])
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'names', ['aaa','bbb','ccc'])
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'logical_scalar', .true.)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    call json_add(inp, 'logical_vector', [.true., .false., .true.])
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    nullify(inp)

    !trajectory variables:
    call add_variables_to_input(traj, 'Rx', 'km', 'J2000', 'EARTH', [1.0_wp, 2.0_wp, 3.0_wp], error_cnt )
    call add_variables_to_input(traj, 'Ry', 'km', 'J2000', 'EARTH', [10.0_wp, 20.0_wp, 30.0_wp], error_cnt )
    call add_variables_to_input(traj, 'Rz', 'km', 'J2000', 'EARTH', [100.0_wp, 200.0d0, 300.0_wp], error_cnt )
    call add_variables_to_input(traj, 'Vx', 'km/s', 'J2000', 'EARTH', [1.0e-3_wp, 2.0e-3_wp, 3.0e-3_wp], error_cnt )
    call add_variables_to_input(traj, 'Vy', 'km/s', 'J2000', 'EARTH', [2.0e-3_wp, 20.0e-3_wp, 3.0e-3_wp], error_cnt )
    call add_variables_to_input(traj, 'Vz', 'km/s', 'J2000', 'EARTH', [3.0e-3_wp, 30.0e-3_wp, 40.0e-3_wp], error_cnt )
    nullify(traj)

    write(error_unit,'(A)') ''
    write(error_unit,'(A)') 'writing file '//trim(dir//filename2)//'...'

    open(newunit=iunit, file=dir//filename2, status='REPLACE')
    call json_print(p,iunit)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    close(iunit)
    
    !test the deep copy routine:
    
    write(error_unit,'(A)') 'json_clone test'
    call json_clone(p,p_clone)
    
    write(error_unit,'(A)') ''
    write(error_unit,'(A)') '============='
    write(error_unit,'(A)') ' p_clone'
    write(error_unit,'(A)') '============='
    call json_print(p_clone,error_unit)
    write(error_unit,'(A)') '============='
    write(error_unit,'(A)') ''
    
    if (.not. associated(p)) write(error_unit,'(A)') 'ERROR: p has become unassociated'
    if (.not. associated(p_clone)) write(error_unit,'(A)') 'ERROR: p_clone is not associated'
    
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    else        
        !now, change one and verify that they are independent:
        call json_update(p_clone,'inputs.integer_scalar',100,found)
        if (json_failed()) write(error_unit,'(A)') 'json_update Error for p_clone'
        call json_get(p,'inputs.integer_scalar',ival)
        if (json_failed()) write(error_unit,'(A)') 'json_get Error for p'
        call json_get(p_clone,'inputs.integer_scalar',ival_clone)
        if (json_failed()) write(error_unit,'(A)') 'json_get Error for p_clone'
        if (json_failed()) then
            call json_print_error_message(error_unit)
            error_cnt = error_cnt + 1
        else
            if (ival==1 .and. ival_clone==100) then
                write(error_unit,'(A)') 'json_clone ... passed'
            else
                write(error_unit,'(A)') 'Error: ival /= ival_clone'
                error_cnt = error_cnt + 1
            end if
        end if
    end if
    
    !test some of the pointer routines:
    write(error_unit,'(A)') 'Pointer routine tests'
    call json_get(p,'inputs.integer_array',p_integer_array)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    else
    
        !get parent test:
        call json_get_parent(p_integer_array,p_tmp)  !should be "inputs"
        call json_info(p_tmp,name=name)
        if (json_failed()) then
            call json_print_error_message(error_unit)
            error_cnt = error_cnt + 1
        else
            if (name=='inputs') then
                write(error_unit,'(A)') 'json_get_parent ... passed'
            else
                write(error_unit,'(A)') 'Error: parent should be "inputs", is actually: '//trim(name)
                error_cnt = error_cnt + 1
            end if
        end if
        
        !get next test:
        call json_get_next(p_integer_array,p_tmp)  !should be "names"
        call json_info(p_tmp,name=name)
        if (json_failed()) then
            call json_print_error_message(error_unit)
            error_cnt = error_cnt + 1
        else
            if (name=='names') then
                write(error_unit,'(A)') 'json_get_next ... passed'
            else
                write(error_unit,'(A)') 'Error: next should be "names", is actually: '//trim(name)
                error_cnt = error_cnt + 1
            end if
        end if
        
        !get previous test:
        call json_get_previous(p_integer_array,p_tmp)  !should be "integer_scalar"
        call json_info(p_tmp,name=name)
        if (json_failed()) then
            call json_print_error_message(error_unit)
            error_cnt = error_cnt + 1
        else
            if (name=='integer_scalar') then
                write(error_unit,'(A)') 'json_get_previous ... passed'
            else
                write(error_unit,'(A)') 'Error: next should be "integer_scalar", is actually: '//trim(name)
                error_cnt = error_cnt + 1
            end if
        end if

        !get tail test:
        call json_get_tail(p_integer_array,p_tmp)  !should be 99, the last element in the array
        call json_get(p_tmp,ival)
        if (json_failed()) then
            call json_print_error_message(error_unit)
            error_cnt = error_cnt + 1
        else
            if (ival==99) then
                write(error_unit,'(A)') 'json_get_tail ... passed'
            else
                write(error_unit,'(A,1X,I5)') 'Error: tail value should be 99, is actually: ',ival
                error_cnt = error_cnt + 1
            end if
        end if

    end if

    !cleanup:
    call json_destroy(p)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if

    write(error_unit,'(A)') ''

    end subroutine test_2