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:

    * 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

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