|
| 1 | +!***************************************************************************************** |
| 2 | +!> |
| 3 | +! Module for the 53rd unit test. |
| 4 | +! An example of iterating through a JSON object using get_child and get_next. |
| 5 | + |
| 6 | +module jf_test_53_mod |
| 7 | + |
| 8 | + use json_module, wp => json_RK, IK => json_IK, LK => json_LK, CK => json_CK |
| 9 | + use, intrinsic :: iso_fortran_env , only: error_unit, output_unit |
| 10 | + |
| 11 | + implicit none |
| 12 | + |
| 13 | + private |
| 14 | + public :: test_53 |
| 15 | + |
| 16 | +contains |
| 17 | + |
| 18 | + subroutine test_53(error_cnt) |
| 19 | + |
| 20 | + !! 53rd unit test. |
| 21 | + |
| 22 | + integer,intent(out) :: error_cnt |
| 23 | + |
| 24 | + type(json_core) :: json |
| 25 | + type(json_value),pointer :: p_root, p_child |
| 26 | + character(kind=CK,len=:),allocatable :: key |
| 27 | + integer(IK) :: count, i |
| 28 | + |
| 29 | + character(kind=CK,len=*),parameter :: str = CK_'{'//& |
| 30 | + ' "a": 1,'//& |
| 31 | + ' "b": 2,'//& |
| 32 | + ' "c": 3'//& |
| 33 | + '}' |
| 34 | + |
| 35 | + character(kind=CK,len=1),dimension(3),parameter :: expected_keys = [CK_'a', CK_'b', CK_'c'] |
| 36 | + |
| 37 | + error_cnt = 0 |
| 38 | + |
| 39 | + write(error_unit,'(A)') '' |
| 40 | + write(error_unit,'(A)') '=================================' |
| 41 | + write(error_unit,'(A)') ' EXAMPLE 53' |
| 42 | + write(error_unit,'(A)') '=================================' |
| 43 | + write(error_unit,'(A)') '' |
| 44 | + |
| 45 | + call json%initialize() |
| 46 | + call json%deserialize(p_root, str) |
| 47 | + if (json%failed()) then |
| 48 | + error_cnt = error_cnt + 1 |
| 49 | + write(error_unit,'(A)') 'Error reading JSON string: '//trim(str) |
| 50 | + else |
| 51 | + call json%info(p_root,n_children=count) ! there should be 3 children |
| 52 | + if (count /= 3) then |
| 53 | + error_cnt = error_cnt + 1 |
| 54 | + write(error_unit,'(A,I0)') 'Error: expected 3 children, got ', count |
| 55 | + else |
| 56 | + write(output_unit,'(A,I0)') 'Success: expected 3 children, got ', count |
| 57 | + call json%get_child(p_root, 1, p_child) ! get the first one |
| 58 | + do i = 1, count |
| 59 | + call json%info(p_child, name=key) ! get the key name |
| 60 | + write(output_unit,'(A,I0,A,A)') 'Key ', i, ': ', trim(key) |
| 61 | + if (key /= expected_keys(i)) then |
| 62 | + error_cnt = error_cnt + 1 |
| 63 | + write(error_unit,'(A,I0,A,A)') 'Error: expected key ', i, ' to be ', & |
| 64 | + trim(expected_keys(i)), ' but got ', trim(key) |
| 65 | + end if |
| 66 | + ! get the next one (more efficient than calling get_child again) |
| 67 | + if (i<count) call json%get_next(p_child, p_child) |
| 68 | + end do |
| 69 | + end if |
| 70 | + |
| 71 | + end if |
| 72 | + |
| 73 | + call json%destroy(p_root) |
| 74 | + |
| 75 | + end subroutine test_53 |
| 76 | + |
| 77 | +end module jf_test_53_mod |
| 78 | +!***************************************************************************************** |
| 79 | + |
| 80 | +!***************************************************************************************** |
| 81 | +#ifndef INTEGRATED_TESTS |
| 82 | +program jf_test_53 |
| 83 | + |
| 84 | + use jf_test_53_mod , only: test_53 |
| 85 | + |
| 86 | + implicit none |
| 87 | + integer :: n_errors |
| 88 | + |
| 89 | + call test_53(n_errors) |
| 90 | + if (n_errors /= 0) stop 1 |
| 91 | + |
| 92 | +end program jf_test_53 |
| 93 | +#endif |
| 94 | +!***************************************************************************************** |
| 95 | + |
0 commit comments