JSON/jf_test_3 [ Unittest ]
NAME
jf_test_3
DESCRIPTION
Third unit test
USES
json_module iso_fortran_env (intrinsic)
HISTORY
Izaak Beekman : 2/18/2015 : Created (refactoried original json_example.f90 file)
LICENSE
JSON-FORTRAN: A Fortran 2008 JSON API
https://github.com/jacobwilliams/json-fortran
Copyright (c) 2014, Jacob Williams
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SOURCE
49 module jf_test_3_mod 50 51 use json_module 52 use, intrinsic :: iso_fortran_env , only: error_unit, output_unit, wp => real64 53 54 implicit none 55 56 character(len=*),parameter :: dir = '../files/inputs/' !working directory 57 character(len=*),parameter :: filename2 = 'test2.json' 58 59 contains 60 61 subroutine test_3(error_cnt) 62 63 ! Read the file generated in jf_test_2, and extract some data from it. 64 65 implicit none 66 67 integer,intent(out) :: error_cnt 68 integer :: ival 69 character(kind=CK,len=:),allocatable :: cval 70 real(wp) :: rval 71 type(json_file) :: json !the JSON structure read from the file: 72 integer :: i 73 character(kind=CK,len=10) :: str 74 real(wp),dimension(:),allocatable :: rvec 75 76 error_cnt = 0 77 call json_initialize() 78 if (json_failed()) then 79 call json_print_error_message(error_unit) 80 error_cnt = error_cnt + 1 81 end if 82 83 write(error_unit,'(A)') '' 84 write(error_unit,'(A)') '=================================' 85 write(error_unit,'(A)') ' EXAMPLE 3' 86 write(error_unit,'(A)') '=================================' 87 write(error_unit,'(A)') '' 88 89 ! parse the json file: 90 write(error_unit,'(A)') '' 91 write(error_unit,'(A)') 'parsing file: '//dir//filename2 92 93 call json%load_file(filename = dir//filename2) 94 95 if (json_failed()) then !if there was an error reading the file 96 97 call json_print_error_message(error_unit) 98 error_cnt = error_cnt + 1 99 100 else 101 102 write(error_unit,'(A)') '' 103 write(error_unit,'(A)') 'reading data from file...' 104 !get scalars: 105 write(error_unit,'(A)') '' 106 call json%get('inputs.integer_scalar', ival) 107 if (json_failed()) then 108 call json_print_error_message(error_unit) 109 error_cnt = error_cnt + 1 110 else 111 write(error_unit,'(A,1X,I5)') 'inputs.integer_scalar = ',ival 112 end if 113 !get one element from a vector: 114 write(error_unit,'(A)') '' 115 call json%get('trajectory(1).DATA(2)', rval) 116 if (json_failed()) then 117 call json_print_error_message(error_unit) 118 error_cnt = error_cnt + 1 119 else 120 write(error_unit,'(A,1X,F30.16)') 'trajectory(1).DATA(2) = ',rval 121 end if 122 !get vectors: 123 do i=1,4 124 125 write(str,fmt='(I10)') i 126 str = adjustl(str) 127 128 write(error_unit,'(A)') '' 129 call json%get('trajectory('//trim(str)//').VARIABLE', cval) 130 if (json_failed()) then 131 132 call json_print_error_message(error_unit) 133 error_cnt = error_cnt + 1 134 135 else 136 137 write(error_unit,'(A)') 'trajectory('//trim(str)//').VARIABLE = '//trim(cval) 138 139 !...get the vector using the callback method: 140 call json%get('trajectory('//trim(str)//').DATA', rvec) 141 if (json_failed()) then 142 call json_print_error_message(error_unit) 143 error_cnt = error_cnt + 1 144 else 145 write(error_unit,'(A,1X,*(F30.16,1X))') 'trajectory('//trim(str)//').DATA = ',rvec 146 end if 147 148 end if 149 150 end do 151 152 end if 153 154 ! clean up 155 write(error_unit,'(A)') '' 156 write(error_unit,'(A)') 'destroy...' 157 call json%destroy() 158 if (json_failed()) then 159 call json_print_error_message(error_unit) 160 error_cnt = error_cnt + 1 161 end if 162 163 end subroutine test_3 164 165 end module jf_test_3_mod 166 167 program jf_test_3 168 use jf_test_3_mod , only: test_3 169 implicit none 170 integer :: n_errors 171 n_errors = 0 172 call test_3(n_errors) 173 if (n_errors /= 0) stop 1 174 end program jf_test_3