Skip to content

Commit 1805d9a

Browse files
Merge pull request jacobwilliams#453 from jacobwilliams/291-recursive-json-get-array
made json_get_array recursive
2 parents 35b24de + a8058f6 commit 1805d9a

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

src/json_value_module.F90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9476,7 +9476,7 @@ end subroutine wrap_json_get_alloc_string_vec_by_path
94769476
! higher-level routines are provided (see `get` methods), so
94779477
! this routine does not have to be used for those cases.
94789478

9479-
subroutine json_get_array(json, me, array_callback)
9479+
recursive subroutine json_get_array(json, me, array_callback)
94809480

94819481
implicit none
94829482

@@ -9581,7 +9581,7 @@ end subroutine json_traverse
95819581
! This routine calls the user-supplied array_callback subroutine
95829582
! for each element in the array (specified by the path).
95839583

9584-
subroutine json_get_array_by_path(json, me, path, array_callback, found)
9584+
recursive subroutine json_get_array_by_path(json, me, path, array_callback, found)
95859585

95869586
implicit none
95879587

@@ -9626,7 +9626,7 @@ end subroutine json_get_array_by_path
96269626
!>
96279627
! Alternate version of [[json_get_array_by_path]], where "path" is kind=CDK
96289628

9629-
subroutine wrap_json_get_array_by_path(json, me, path, array_callback, found)
9629+
recursive subroutine wrap_json_get_array_by_path(json, me, path, array_callback, found)
96309630

96319631
implicit none
96329632

src/tests/jf_test_44.F90

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
!*****************************************************************************************
2+
!>
3+
! Module for the 44th unit test
4+
5+
module jf_test_44_mod
6+
7+
use json_module, CK => json_CK, IK => json_IK, RK => json_RK, LK => json_LK
8+
use, intrinsic :: iso_fortran_env , only: error_unit, output_unit
9+
10+
implicit none
11+
12+
private
13+
public :: test_44
14+
15+
contains
16+
17+
subroutine test_44(error_cnt)
18+
19+
!! testing recursive `json_get_array`
20+
21+
implicit none
22+
23+
integer,intent(out) :: error_cnt !! error counter
24+
25+
character(kind=CK,len=*),parameter :: str = CK_'{ "x": [0, [1,2,3], [4,5,6] ] }'
26+
27+
type(json_core) :: json
28+
type(json_value),pointer :: p, x
29+
logical :: found
30+
31+
write(error_unit,'(A)') ''
32+
write(error_unit,'(A)') '================================='
33+
write(error_unit,'(A)') ' TEST 44'
34+
write(error_unit,'(A)') '================================='
35+
write(error_unit,'(A)') ''
36+
37+
error_cnt = 0
38+
39+
call json%initialize(path_mode=1_IK)
40+
call json%deserialize(p,str)
41+
42+
if (json%failed()) then
43+
call json%print_error_message(error_unit)
44+
error_cnt = error_cnt + 1
45+
else
46+
call json%get(p,CK_'x',x)
47+
call json%get(x, callback)
48+
end if
49+
50+
if (json%failed()) then
51+
call json%print_error_message(error_unit)
52+
error_cnt = error_cnt + 1
53+
end if
54+
55+
if (error_cnt==0) then
56+
write(error_unit,'(A)') 'Success!'
57+
else
58+
write(error_unit,'(A)') 'Failed!'
59+
end if
60+
write(error_unit,'(A)') ''
61+
62+
end subroutine test_44
63+
64+
recursive subroutine callback(json, element, i, count)
65+
!! Array element callback function. Used by [[json_get_array]]
66+
67+
implicit none
68+
69+
class(json_core),intent(inout) :: json
70+
type(json_value),pointer,intent(in) :: element
71+
integer(IK),intent(in) :: i !! index
72+
integer(IK),intent(in) :: count !! size of array
73+
74+
integer(IK) :: var_type
75+
character(kind=CK,len=:),allocatable :: path
76+
integer(IK) :: ival
77+
78+
call json%get_path(element, path)
79+
80+
call json%info(element,var_type=var_type)
81+
82+
if (var_type == json_array) then
83+
write(output_unit,'(A)') 'array: '//trim(path)
84+
call json%get(element, callback)
85+
else if (var_type == json_integer) then
86+
call json%get(element,ival)
87+
!write(output_unit,'(A,1X,I2)') trim(path)//' = ', ival ! see Issue #452
88+
write(output_unit,'(I2,A,I2)') i,' : ', ival
89+
end if
90+
91+
end subroutine callback
92+
93+
end module jf_test_44_mod
94+
!*****************************************************************************************
95+
96+
#ifndef INTEGRATED_TESTS
97+
!*****************************************************************************************
98+
program jf_test_44
99+
100+
!! 44th unit test.
101+
102+
use jf_test_44_mod , only: test_44
103+
implicit none
104+
integer :: n_errors
105+
n_errors = 0
106+
call test_44(n_errors)
107+
if (n_errors /= 0) stop 1
108+
109+
end program jf_test_44
110+
!*****************************************************************************************
111+
#endif
112+
113+
114+
115+

visual_studio/jsonfortrantest/jsonfortrantest.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ program jsonfortrantest
9999
call test_41(n_errors); if (n_errors /= 0) stop 1
100100
call test_42(n_errors); if (n_errors /= 0) stop 1
101101
call test_43(n_errors); if (n_errors /= 0) stop 1
102+
call test_44(n_errors); if (n_errors /= 0) stop 1
102103

103104
end program jsonfortrantest
104105
!*****************************************************************************************

visual_studio/jsonfortrantest/jsonfortrantest.vfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@
8989
<File RelativePath="..\..\src\tests\jf_test_41.F90"/>
9090
<File RelativePath="..\..\src\tests\jf_test_42.F90"/>
9191
<File RelativePath="..\..\src\tests\jf_test_43.F90"/>
92+
<File RelativePath="..\..\src\tests\jf_test_44.F90"/>
9293
<File RelativePath=".\jsonfortrantest.f90"/></Filter></Files>
9394
<Globals/></VisualStudioProject>

0 commit comments

Comments
 (0)