JSON/jf_test_5 [ Unittest ]

[ Top ] [ Unittest ]

NAME

jf_test_5

DESCRIPTION

Fifth 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:

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_5_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 :: filename5 = 'test5.json'
 58 
 59 contains
 60 
 61     subroutine test_5(error_cnt)
 62 
 63 !    Github issue example: https://github.com/josephalevin/fson/issues/12
 64 !
 65 !    Read an existing file and extract some variables.
 66 
 67     implicit none
 68 
 69     integer,intent(out) :: error_cnt
 70     integer :: vv
 71     integer,dimension(:),allocatable :: vvv
 72     real(wp) :: do
 73     type(json_file) :: json
 74     logical :: found
 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 5'
 86     write(error_unit,'(A)') '================================='
 87     write(error_unit,'(A)') ''
 88 
 89     ! parse the json file:
 90     write(error_unit,'(A)') 'load file...'
 91     call json%load_file(filename = dir//filename5)
 92     if (json_failed()) then
 93 
 94         call json_print_error_message(error_unit)
 95         error_cnt = error_cnt + 1
 96 
 97     else
 98 
 99         ! print the parsed data to the console:
100         write(error_unit,'(A)') 'print file...'
101         call json%print_file()
102         if (json_failed()) then
103             call json_print_error_message(error_unit)
104             error_cnt = error_cnt + 1
105         end if
106 
107         ! extract data from the parsed value:
108         write(error_unit,'(A)') ''
109         write(error_unit,'(A)') 'extract data...'
110 
111         write(error_unit,'(A)') '--------------------------'
112         call json%get('Correl.ID2', vv, found)
113         if (json_failed()) then
114             call json_print_error_message(error_unit)
115             error_cnt = error_cnt + 1
116         end if
117         if (found) write(error_unit,'(A,I5)') 'vv = ',vv
118 
119         call json%get('Correl.ID1', vvv, found)
120         if (json_failed()) then
121             call json_print_error_message(error_unit)
122             error_cnt = error_cnt + 1
123         end if
124         if (found) write(error_unit,'(A,*(I5,1X))') 'vvv= ',vvv
125 
126         call json%get('Prior[3].mode', do, found)
127         if (json_failed()) then
128             call json_print_error_message(error_unit)
129             error_cnt = error_cnt + 1
130         end if
131         if (found) write(error_unit,'(A,E30.16)') 'd  = ',do
132 
133         write(error_unit,'(A)') ''
134 
135     end if
136 
137     ! clean up
138     call json%destroy()
139     if (json_failed()) then
140         call json_print_error_message(error_unit)
141         error_cnt = error_cnt + 1
142     end if
143 
144     end subroutine test_5
145 
146 end module jf_test_5_mod
147 
148 program jf_test_5
149     use jf_test_5_mod , only: test_5
150     implicit none
151     integer :: n_errors
152     n_errors = 0
153     call test_5(n_errors)
154     if (n_errors /= 0) stop 1
155 end program jf_test_5