@@ -48,6 +48,7 @@ module jf_test_9_mod
4848 use , intrinsic :: iso_fortran_env , only: error_unit, output_unit, wp = > real64
4949
5050 implicit none
51+
5152 ! small file - 0.0 sec : http://www.json-generator.com
5253 ! character(len=*),parameter :: filename = 'random1.json'
5354
@@ -57,9 +58,13 @@ module jf_test_9_mod
5758 ! 13 MB - 7.6 sec : http://mtgjson.com
5859 ! character(len=*),parameter :: filename = 'AllSets.json'
5960
61+ ! ....WARNING: this file is causing some error.... (bug in code?)
6062 ! 100 MB - takes forever... : https://github.com/seductiveapps/largeJSON
6163 ! character(len=*),parameter :: filename = '100mb.json'
6264
65+ ! small file that contains unicode characters:
66+ ! character(len=*),parameter :: filename = 'hello-world-ucs4.json' !!!! test !!!!
67+
6368 character (len=* ),parameter :: dir = ' ../files/inputs/' ! working directory
6469
6570contains
@@ -74,6 +79,7 @@ subroutine test_9(error_cnt)
7479
7580 type (json_file) :: f
7681 real :: tstart, tend
82+ character (len= :),allocatable :: str
7783
7884 error_cnt = 0
7985 call json_initialize()
@@ -84,7 +90,7 @@ subroutine test_9(error_cnt)
8490
8591 write (error_unit,' (A)' ) ' '
8692 write (error_unit,' (A)' ) ' ================================='
87- write (error_unit,' (A)' ) ' EXAMPLE 9 '
93+ write (error_unit,' (A)' ) ' EXAMPLE 9a '
8894 write (error_unit,' (A)' ) ' ================================='
8995
9096 write (error_unit,' (A)' ) ' '
@@ -108,8 +114,75 @@ subroutine test_9(error_cnt)
108114 ! cleanup:
109115 call f% destroy()
110116
117+ write (error_unit,' (A)' ) ' '
118+ write (error_unit,' (A)' ) ' ================================='
119+ write (error_unit,' (A)' ) ' EXAMPLE 9b '
120+ write (error_unit,' (A)' ) ' ================================='
121+
122+ write (error_unit,' (A)' ) ' '
123+ write (error_unit,' (A)' ) ' Load a file using json_file%load_from_string'
124+ write (error_unit,' (A)' ) ' '
125+ write (error_unit,' (A)' ) ' Loading file: ' // trim (filename)
126+
127+ call cpu_time(tstart)
128+ call read_file(dir// filename, str)
129+
130+ if (allocated (str)) then
131+ call f% load_from_string(str)
132+ call cpu_time(tend)
133+ write (error_unit,' (A,1X,F10.3,1X,A)' ) ' Elapsed time to parse: ' ,tend- tstart,' sec'
134+ if (json_failed()) then
135+ call json_print_error_message(error_unit)
136+ error_cnt = error_cnt + 1
137+ else
138+ write (error_unit,' (A)' ) ' File successfully read'
139+ end if
140+ write (error_unit,' (A)' ) ' '
141+ ! write(error_unit,'(A)') str !!!! test !!!!
142+ ! write(error_unit,'(A)') '' !!!! test !!!!
143+ else
144+ write (error_unit,' (A)' ) ' Error loading file'
145+ end if
146+
147+ ! cleanup:
148+ call f% destroy()
149+
111150 end subroutine test_9
112151
152+ subroutine read_file (filename ,str )
153+ !
154+ ! Reads the contents of the file into the allocatable string str.
155+ ! If there are any problems, str will be returned unallocated.
156+ !
157+
158+ ! Will this routine work if the file contains unicode characters??
159+
160+ implicit none
161+
162+ character (len=* ),intent (in ) :: filename
163+ character (len= :),allocatable ,intent (out ) :: str
164+
165+ integer :: iunit,istat,filesize
166+
167+ open ( newunit = iunit,&
168+ file = filename,&
169+ status = ' OLD' ,&
170+ form = ' UNFORMATTED' ,&
171+ access = ' STREAM' ,&
172+ iostat = istat )
173+
174+ if (istat== 0 ) then
175+ inquire (file= filename, size= filesize)
176+ if (filesize> 0 ) then
177+ allocate ( character (len= filesize) :: str )
178+ read (iunit,pos= 1 ,iostat= istat) str
179+ if (istat/= 0 ) deallocate (str)
180+ close (iunit, iostat= istat)
181+ end if
182+ end if
183+
184+ end subroutine read_file
185+
113186end module jf_test_9_mod
114187
115188program jf_test_9
0 commit comments