Skip to content

Commit eed53d3

Browse files
committed
Fixed a bug in wrap_json_get_path (wrapper to json_get_path). Fixes #333
Added a unit test for this routine for all the options.
1 parent 25fd7ff commit eed53d3

File tree

2 files changed

+137
-11
lines changed

2 files changed

+137
-11
lines changed

src/json_value_module.F90

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7176,19 +7176,20 @@ subroutine wrap_json_get_path(json, p, path, found, use_alt_array_tokens, path_s
71767176
type(json_value),pointer,intent(in) :: p !! a JSON linked list object
71777177
character(kind=CDK,len=:),allocatable,intent(out) :: path !! path to the variable
71787178
logical(LK),intent(out),optional :: found !! true if there were no problems
7179-
logical(LK),intent(in),optional :: use_alt_array_tokens !! if true, then '()' are used for array elements
7180-
!! otherwise, '[]' are used [default]
7181-
character(kind=CDK,len=1),intent(in),optional :: path_sep !! character to use for path separator
7182-
!! (default is '.')
7179+
logical(LK),intent(in),optional :: use_alt_array_tokens !! if true, then '()' are used
7180+
!! for array elements otherwise,
7181+
!! '[]' are used [default]
7182+
character(kind=CDK,len=1),intent(in),optional :: path_sep !! character to use for path
7183+
!! separator (default is '.')
71837184

71847185
character(kind=CK,len=:),allocatable :: ck_path !! path to the variable
7185-
character(kind=CK,len=1) :: sep
7186-
7187-
! from unicode:
7188-
sep = path_sep
71897186

71907187
! call the main routine:
7191-
call json_get_path(json,p,ck_path,found,use_alt_array_tokens,sep)
7188+
if (present(path_sep)) then
7189+
call json%get_path(p,ck_path,found,use_alt_array_tokens,to_unicode(path_sep))
7190+
else
7191+
call json%get_path(p,ck_path,found,use_alt_array_tokens)
7192+
end if
71927193

71937194
! from unicode:
71947195
path = ck_path
@@ -7210,7 +7211,7 @@ function string_to_int(json,str) result(ival)
72107211
character(kind=CK,len=*),intent(in) :: str
72117212
integer(IK) :: ival
72127213

7213-
logical(LK) :: status_ok !! error flag
7214+
logical(LK) :: status_ok !! error flag for [[string_to_integer]]
72147215

72157216
if (.not. json%exception_thrown) then
72167217

@@ -7243,7 +7244,7 @@ function string_to_dble(json,str) result(rval)
72437244
character(kind=CK,len=*),intent(in) :: str !! a string
72447245
real(RK) :: rval !! `str` converted to a double
72457246

7246-
logical(LK) :: status_ok !! error flag
7247+
logical(LK) :: status_ok !! error flag for [[string_to_real]]
72477248

72487249
if (.not. json%exception_thrown) then
72497250

src/tests/jf_test_32.F90

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
!*****************************************************************************************
2+
!>
3+
! Module for the 32nd unit test.
4+
5+
module jf_test_32_mod
6+
7+
use json_module, rk => json_rk, lk => json_lk, ik => json_ik, ck => json_ck, cdk => json_cdk
8+
use, intrinsic :: iso_fortran_env , only: error_unit, output_unit
9+
10+
implicit none
11+
12+
contains
13+
14+
subroutine test_32(error_cnt)
15+
16+
!! Test all the options and wrappers for [[json_get_path]].
17+
18+
implicit none
19+
20+
integer,intent(out) :: error_cnt
21+
integer :: i !! counter
22+
integer :: j !! counter
23+
integer :: k !! counter
24+
type(json_core) :: json
25+
type(json_value),pointer :: p, p_var
26+
character(kind=CK,len=:),allocatable :: path_ck
27+
character(kind=CDK,len=:),allocatable :: path_cdk
28+
logical(LK) :: use_alt_array_tokens
29+
logical(LK) :: found
30+
31+
character(kind=CK,len=1),parameter :: path_sep_ck = CK_'.'
32+
character(kind=CDK,len=1),parameter :: path_sep_cdk = CDK_'.'
33+
character(kind=CK,len=*),parameter :: json_string = CK_'{ "struct": {"a": [1,2]} }'
34+
35+
error_cnt = 0
36+
37+
write(error_unit,'(A)') ''
38+
write(error_unit,'(A)') '================================='
39+
write(error_unit,'(A)') ' TEST 32'
40+
write(error_unit,'(A)') '================================='
41+
write(error_unit,'(A)') ''
42+
43+
call json%initialize()
44+
45+
call json%parse(p,json_string)
46+
if (json%failed()) then
47+
call json%print_error_message(error_unit)
48+
error_cnt = error_cnt + 1
49+
else
50+
51+
call json%get(p,CK_'struct.a(1)',p_var)
52+
53+
if (json%failed()) then
54+
call json%print_error_message(error_unit)
55+
error_cnt = error_cnt + 1
56+
else
57+
58+
! CK version:
59+
do i=1,2
60+
use_alt_array_tokens = i==1
61+
do j=1,2
62+
if (j==1) then
63+
call json%get_path(p_var, path_ck, found, use_alt_array_tokens, path_sep_ck)
64+
else
65+
call json%get_path(p_var, path_ck, found, use_alt_array_tokens)
66+
end if
67+
if (json%failed()) then
68+
call json%print_error_message(error_unit)
69+
error_cnt = error_cnt + 1
70+
else
71+
write(error_unit,'(A)') path_ck
72+
end if
73+
end do
74+
end do
75+
76+
! CDK version:
77+
do i=1,2
78+
use_alt_array_tokens = i==1
79+
do j=1,2
80+
if (j==1) then
81+
call json%get_path(p_var, path_cdk, found, use_alt_array_tokens, path_sep_cdk)
82+
else
83+
call json%get_path(p_var, path_cdk, found, use_alt_array_tokens)
84+
end if
85+
if (json%failed()) then
86+
call json%print_error_message(error_unit)
87+
error_cnt = error_cnt + 1
88+
else
89+
write(error_unit,'(A)') path_cdk
90+
end if
91+
end do
92+
end do
93+
94+
end if
95+
96+
end if
97+
98+
if (.not. json%failed()) then
99+
write(error_unit,*) ''
100+
write(error_unit,*) 'Success!'
101+
end if
102+
103+
call json%destroy(p) ! free memory
104+
105+
end subroutine test_32
106+
107+
end module jf_test_32_mod
108+
!*****************************************************************************************
109+
110+
#ifndef INTERGATED_TESTS
111+
!*****************************************************************************************
112+
program jf_test_32
113+
114+
!! 32nd unit test.
115+
116+
use jf_test_32_mod , only: test_32
117+
implicit none
118+
integer :: n_errors
119+
n_errors = 0
120+
call test_32(n_errors)
121+
if (n_errors /= 0) stop 1
122+
123+
end program jf_test_32
124+
!*****************************************************************************************
125+
#endif

0 commit comments

Comments
 (0)