Skip to content

Commit 609987d

Browse files
committed
Fixed bug in json_get_path for RFC 6091 path mode: the special characters weren’t being encoded properly. Fixes #262
Fixed another bug in json_get_path for RFC 6091 path mode: it didn’t work if the final key was all whitespace.
1 parent 6839148 commit 609987d

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/json_value_module.F90

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5305,9 +5305,6 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
53055305
logical(LK) :: use_brackets !! to use '[]' characters for arrays
53065306
logical(LK) :: parent_is_root !! if the parent is the root
53075307

5308-
!initialize:
5309-
path = CK_''
5310-
53115308
!optional input:
53125309
if (present(use_alt_array_tokens)) then
53135310
use_brackets = .not. use_alt_array_tokens
@@ -5325,13 +5322,19 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
53255322

53265323
!get info about the current variable:
53275324
call json%info(tmp,name=name)
5325+
if (json%path_mode==2) then
5326+
name = encode_rfc6901(name)
5327+
end if
53285328

53295329
! if tmp a child of an object, or an element of an array
53305330
if (associated(tmp%parent)) then
53315331

53325332
!get info about the parent:
53335333
call json%info(tmp%parent,var_type=var_type,&
53345334
n_children=n_children,name=parent_name)
5335+
if (json%path_mode==2) then
5336+
parent_name = encode_rfc6901(parent_name)
5337+
end if
53355338

53365339
select case (var_type)
53375340
case (json_array)
@@ -5408,7 +5411,7 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
54085411
end if
54095412

54105413
!for errors, return blank string:
5411-
if (json%exception_thrown) then
5414+
if (json%exception_thrown .or. .not. allocated(path)) then
54125415
path = CK_''
54135416
else
54145417
if (json%path_mode==2) then
@@ -5440,14 +5443,14 @@ subroutine add_to_path(str,path_sep)
54405443
select case (json%path_mode)
54415444
case(2)
54425445
! in this case, the options are ignored
5443-
if (path==CK_'') then
5446+
if (.not. allocated(path)) then
54445447
path = str
54455448
else
54465449
path = str//slash//path
54475450
end if
54485451
case(1)
54495452
! default path format
5450-
if (path==CK_'') then
5453+
if (.not. allocated(path)) then
54515454
path = str
54525455
else
54535456
if (present(path_sep)) then

src/tests/jf_test_23.f90

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ subroutine test_23(error_cnt)
3232
real(wp) :: rval
3333
logical :: found
3434
character(kind=json_CK,len=10),dimension(:),allocatable :: cval_array
35+
integer :: i !! counter
3536

3637
error_cnt = 0
3738
call json%initialize( trailing_spaces_significant=.true.,&
@@ -220,20 +221,27 @@ subroutine test_23(error_cnt)
220221
path_mode=2) ! RFC6901 paths
221222

222223
write(error_unit,'(A)') ''
223-
key = '/data/1/real'
224-
call json%get(key,p)
225-
call core%get_path(p, path, found)
226-
if (found) then
227-
if (key==path) then
228-
write(error_unit,'(A)') 'get_path test passed: '//path
224+
do i = 1, 4
225+
select case (i)
226+
case(1); key = '/data/1/real'
227+
case(2); key = '/rfc6901 tests/ '
228+
case(3); key = '/rfc6901 tests/m~0n'
229+
case(4); key = '/rfc6901 tests/a~1b'
230+
end select
231+
call json%get(key,p)
232+
call core%get_path(p, path, found)
233+
if (found) then
234+
if (key==path) then
235+
write(error_unit,'(A)') 'get_path test passed: '//path
236+
else
237+
write(error_unit,'(A)') 'Error: path does not match: '//path//' '//key
238+
error_cnt = error_cnt + 1
239+
end if
229240
else
230-
write(error_unit,'(A)') 'Error: path does not match: '//path//' '//key
241+
write(error_unit,'(A)') 'Error: could not find '//key
231242
error_cnt = error_cnt + 1
232243
end if
233-
else
234-
write(error_unit,'(A)') 'Error: could not find '//key
235-
error_cnt = error_cnt + 1
236-
end if
244+
end do
237245

238246
! clean up
239247
write(error_unit,'(A)') ''

0 commit comments

Comments
 (0)