Skip to content

Commit b33038a

Browse files
Merge pull request #258 from jacobwilliams/path_mode
added path_mode integer input
2 parents b710be8 + 7dd5f07 commit b33038a

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

src/json_file_module.F90

+4-4
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
275275
no_whitespace,&
276276
unescape_strings,&
277277
comment_char,&
278-
use_rfc6901_paths,&
278+
path_mode,&
279279
path_separator)
280280

281281
implicit none
@@ -291,7 +291,7 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
291291
no_whitespace,&
292292
unescape_strings,&
293293
comment_char,&
294-
use_rfc6901_paths,&
294+
path_mode,&
295295
path_separator)
296296

297297
end subroutine initialize_json_core_in_file
@@ -354,7 +354,7 @@ function initialize_json_file(p,verbose,compact_reals,&
354354
no_whitespace,&
355355
unescape_strings,&
356356
comment_char,&
357-
use_rfc6901_paths,&
357+
path_mode,&
358358
path_separator) result(file_object)
359359

360360
implicit none
@@ -372,7 +372,7 @@ function initialize_json_file(p,verbose,compact_reals,&
372372
no_whitespace,&
373373
unescape_strings,&
374374
comment_char,&
375-
use_rfc6901_paths,&
375+
path_mode,&
376376
path_separator)
377377

378378
if (present(p)) file_object%p => p

src/json_initialize_arguments.inc

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
!! to denote comments in the JSON file,
2323
!! which will be ignored if present.
2424
!! Example: `!` or `#`.
25-
logical(LK),intent(in),optional :: use_rfc6901_paths !! If true, then path values in the
26-
!! `get_by_path` routines are interpreted
27-
!! as RFC 6901 "JSON Pointer" paths.
28-
!! By default, this is False.
25+
integer(IK),intent(in),optional :: path_mode !! How the path strings are interpreted in the
26+
!! `get_by_path` routines:
27+
!!
28+
!! * 1 -- Default way (see [[json_get_by_path_default]])
29+
!! [Default]
30+
!! * 2 -- as RFC 6901 "JSON Pointer" paths
31+
!! (see [[json_get_by_path_rfc6901]])
2932
character(kind=CK,len=1),intent(in),optional :: path_separator !! The `path` separator to use
3033
!! in the "default" mode for
3134
!! the paths in the various
3235
!! `get_by_path` routines.
3336
!! Example: `.` [default] or `%`.
34-
!! Note: if `use_rfc6901_paths=true`
37+
!! Note: if `path_mode/=1`
3538
!! then this is ignored.

src/json_value_module.F90

+37-24
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,18 @@ module json_value_module
208208
!! `allow_comments` is true.
209209
!! Examples: '`!`' or '`#`'.
210210

211-
logical(LK) :: use_rfc6901_paths = .false. !! use the RFC 6901 standard for
212-
!! JSON paths. Otherwise, the original
213-
!! default method is used
211+
integer(IK) :: path_mode = 1_IK !! How the path strings are interpreted in the
212+
!! `get_by_path` routines:
213+
!!
214+
!! * 1 -- Default mode (see [[json_get_by_path_default]])
215+
!! * 2 -- as RFC 6901 "JSON Pointer" paths
216+
!! (see [[json_get_by_path_rfc6901]])
214217

215218
character(kind=CK,len=1) :: path_separator = dot !! The `path` separator to use
216219
!! in the "default" mode for
217220
!! the paths in the various
218221
!! `get_by_path` routines.
219-
!! Note: if `use_rfc6901_paths=true`
222+
!! Note: if `path_mode/=1`
220223
!! then this is ignored.
221224

222225
contains
@@ -660,7 +663,7 @@ function initialize_json_core(verbose,compact_reals,&
660663
no_whitespace,&
661664
unescape_strings,&
662665
comment_char,&
663-
use_rfc6901_paths,&
666+
path_mode,&
664667
path_separator) result(json_core_object)
665668

666669
implicit none
@@ -676,7 +679,7 @@ function initialize_json_core(verbose,compact_reals,&
676679
no_whitespace,&
677680
unescape_strings,&
678681
comment_char,&
679-
use_rfc6901_paths,&
682+
path_mode,&
680683
path_separator)
681684

682685
end function initialize_json_core
@@ -709,7 +712,7 @@ subroutine json_initialize(json,verbose,compact_reals,&
709712
no_whitespace,&
710713
unescape_strings,&
711714
comment_char,&
712-
use_rfc6901_paths,&
715+
path_mode,&
713716
path_separator)
714717

715718
implicit none
@@ -756,8 +759,14 @@ subroutine json_initialize(json,verbose,compact_reals,&
756759
json%no_whitespace = no_whitespace
757760
if (present(unescape_strings)) &
758761
json%unescaped_strings = unescape_strings
759-
if (present(use_rfc6901_paths)) &
760-
json%use_rfc6901_paths = use_rfc6901_paths
762+
if (present(path_mode)) then
763+
if (path_mode==1_IK .or. path_mode==2_IK) then
764+
json%path_mode = path_mode
765+
else
766+
json%path_mode = 1_IK ! just to have a valid value
767+
call json%throw_exception('Invalid path_mode.')
768+
end if
769+
end if
761770

762771
! if we are allowing comments in the file:
763772
if (present(comment_char)) then
@@ -3884,11 +3893,13 @@ subroutine json_get_by_path(json, me, path, p, found)
38843893
!! specify by `path`
38853894
logical(LK),intent(out),optional :: found !! true if it was found
38863895

3887-
if (json%use_rfc6901_paths) then
3888-
call json%json_get_by_path_rfc6901(me, path, p, found)
3889-
else
3896+
! note: it can only be 1 or 2 (which was checked in initialize)
3897+
select case (json%path_mode)
3898+
case(1_IK)
38903899
call json%json_get_by_path_default(me, path, p, found)
3891-
end if
3900+
case(2_IK)
3901+
call json%json_get_by_path_rfc6901(me, path, p, found)
3902+
end select
38923903

38933904
end subroutine json_get_by_path
38943905
!*****************************************************************************************
@@ -3912,7 +3923,7 @@ end subroutine json_get_by_path
39123923
! * `$` - root
39133924
! * `@` - this
39143925
! * `.` - child object member (note this can be changed using `json%path_separator`)
3915-
! * `[]` or `()` - child array element
3926+
! * `[]` or `()` - child array element (note that indices are 1-based)
39163927
!
39173928
! Thus, if any of these characters are present in the name key,
39183929
! this routine cannot be used to get the value.
@@ -4312,8 +4323,8 @@ end subroutine wrap_json_get_by_path
43124323
! `found` is present, which will be set to `false`. `path`
43134324
! will be a blank string.
43144325
!
4315-
!@note If `json%use_rfc6901_paths=.true.`, then the
4316-
! `use_alt_array_tokens` and `path_sep` inputs are ignored.
4326+
!@note If `json%path_mode/=1`, then the `use_alt_array_tokens`
4327+
! and `path_sep` inputs are ignored if present.
43174328

43184329
subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
43194330

@@ -4389,10 +4400,11 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
43894400
exit
43904401
end if
43914402
end do
4392-
if (json%use_rfc6901_paths) then
4403+
select case(json%path_mode)
4404+
case(2)
43934405
call integer_to_string(i-1,int_fmt,istr) ! 0-based index
43944406
call add_to_path(parent_name//slash//trim(adjustl(istr)))
4395-
else
4407+
case(1)
43964408
call integer_to_string(i,int_fmt,istr)
43974409
if (use_brackets) then
43984410
call add_to_path(parent_name//start_array//&
@@ -4401,7 +4413,7 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
44014413
call add_to_path(parent_name//start_array_alt//&
44024414
trim(adjustl(istr))//end_array_alt,path_sep)
44034415
end if
4404-
end if
4416+
end select
44054417
tmp => tmp%parent ! already added parent name
44064418

44074419
case (json_object)
@@ -4444,7 +4456,7 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
44444456
if (json%exception_thrown) then
44454457
path = CK_''
44464458
else
4447-
if (json%use_rfc6901_paths) then
4459+
if (json%path_mode==2) then
44484460
! add the root slash:
44494461
path = slash//path
44504462
end if
@@ -4468,16 +4480,17 @@ subroutine add_to_path(str,path_sep)
44684480
character(kind=CK,len=*),intent(in) :: str !! string to prepend to `path`
44694481
character(kind=CK,len=1),intent(in),optional :: path_sep
44704482
!! path separator (default is '.').
4471-
!! (ignored if `json%use_rfc6901_paths=.true.`)
4483+
!! (ignored if `json%path_mode/=1`)
44724484

4473-
if (json%use_rfc6901_paths) then
4485+
select case (json%path_mode)
4486+
case(2)
44744487
! in this case, the options are ignored
44754488
if (path==CK_'') then
44764489
path = str
44774490
else
44784491
path = str//slash//path
44794492
end if
4480-
else
4493+
case(1)
44814494
! default path format
44824495
if (path==CK_'') then
44834496
path = str
@@ -4490,7 +4503,7 @@ subroutine add_to_path(str,path_sep)
44904503
path = str//json%path_separator//path
44914504
end if
44924505
end if
4493-
end if
4506+
end select
44944507

44954508
end subroutine add_to_path
44964509

src/tests/jf_test_23.f90

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ subroutine test_23(error_cnt)
3636
error_cnt = 0
3737
call json%initialize( trailing_spaces_significant=.true.,&
3838
case_sensitive_keys=.true.,&
39-
use_rfc6901_paths=.true.)
39+
path_mode=2) ! RFC6901 paths
4040
if (json%failed()) then
4141
call json%print_error_message(error_unit)
4242
error_cnt = error_cnt + 1
@@ -217,7 +217,7 @@ subroutine test_23(error_cnt)
217217

218218
call core%initialize( trailing_spaces_significant=.true.,&
219219
case_sensitive_keys=.true.,&
220-
use_rfc6901_paths=.true.)
220+
path_mode=2) ! RFC6901 paths
221221

222222
write(error_unit,'(A)') ''
223223
key = '/data/1/real'

0 commit comments

Comments
 (0)