test_4 Subroutine

public subroutine test_4(error_cnt)

Arguments

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

Description

Populate a JSON structure, write it to a file, then read it.

Also tests the json_value_to_string routine to write the file to a character string.

Calls

proc~~test_4~~CallsGraph proc~test_4 test_4 interface~json_print json_print proc~test_4->interface~json_print proc~json_failed json_failed proc~test_4->proc~json_failed proc~json_print_error_message json_print_error_message proc~test_4->proc~json_print_error_message interface~json_print_to_string json_print_to_string proc~test_4->interface~json_print_to_string proc~json_initialize json_initialize proc~test_4->proc~json_initialize interface~json_add json_add proc~test_4->interface~json_add interface~json_create_object json_create_object proc~test_4->interface~json_create_object interface~json_destroy json_destroy proc~test_4->interface~json_destroy 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_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_value_to_string json_value_to_string interface~json_print_to_string->proc~json_value_to_string interface~throw_exception throw_exception proc~json_initialize->interface~throw_exception proc~json_initialize->proc~json_clear_exceptions 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_create_object json_value_create_object interface~json_create_object->proc~json_value_create_object proc~json_value_destroy json_value_destroy interface~json_destroy->proc~json_value_destroy 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_count json_count 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_to_string->proc~json_value_print proc~json_value_add_logical->interface~json_add proc~json_value_create json_value_create proc~json_value_add_logical->proc~json_value_create proc~to_logical to_logical 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~to_double to_double 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~to_integer to_integer 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~to_string to_string proc~json_value_add_string->proc~to_string proc~destroy_json_data destroy_json_data proc~to_logical->proc~destroy_json_data 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 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_value_destroy->proc~destroy_json_data proc~json_value_destroy->proc~json_value_destroy
Help

Called By

proc~~test_4~~CalledByGraph proc~test_4 test_4 program~jf_test_4 jf_test_4 program~jf_test_4->proc~test_4
Help

Variables

TypeVisibility AttributesNameInitial
type(json_value), public, pointer:: p
type(json_value), public, pointer:: inp
type(json_file), public :: json
integer, public :: i
character(kind=CK,len=10), public :: istr
character(kind=CK,len=:), public, allocatable:: string

Source Code

    subroutine test_4(error_cnt)

    !! Populate a JSON structure, write it to a file,
    !! then read it.
    !! 
    !! Also tests the json_value_to_string routine to write
    !! the file to a character string.

    implicit none

    integer,intent(out) :: error_cnt
    type(json_value),pointer    :: p,inp
    type(json_file) :: json

    integer :: i
    character(kind=CK,len=10) :: istr
    character(kind=CK,len=:),allocatable :: string

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

    write(error_unit,'(A)') ''
    write(error_unit,'(A)') 'creating structure'

    call json_create_object(p,dir//filename4)     !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

    !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
    !add just integers:
    do i=1,100
        write(istr,fmt='(I10)') i
        istr = adjustl(istr)
        call json_add(inp, 'x'//trim(istr),i)
        if (json_failed()) then
            call json_print_error_message(error_unit)
            error_cnt = error_cnt + 1
        end if
    end do
    call json_add(p, inp)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    nullify(inp)

    write(error_unit,'(A)') ''
    write(error_unit,'(A)') 'write to file'

    !write the file:
    call json_print(p,trim(dir//filename4))
    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 to string'
    write(error_unit,'(A)') ''
    !write it to a string, and print to console:
    call json_print_to_string(p, string)
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if
    write(output_unit,'(A)') string
    deallocate(string)  !cleanup

    !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)') ''
    write(error_unit,'(A)') 'read file'

    call json%load_file(filename = dir//filename4)
    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)') 'cleanup'
    call json%destroy()
    if (json_failed()) then
        call json_print_error_message(error_unit)
        error_cnt = error_cnt + 1
    end if

    end subroutine test_4