@@ -204,14 +204,21 @@ module json_value_module
204204 logical (LK) :: allow_comments = .true. ! ! if true, any comments will be ignored when
205205 ! ! parsing a file. The comment token is defined
206206 ! ! by the `comment_char` string.
207- character (kind= CK,len= 1 ) :: comment_char = ' !' ! ! comment token when
208- ! ! `allow_comments` is true.
209- ! ! Examples: '`!`' or '`#`'.
207+ character (kind= CK,len= 1 ) :: comment_char = CK_ ' !' ! ! comment token when
208+ ! ! `allow_comments` is true.
209+ ! ! Examples: '`!`' or '`#`'.
210210
211211 logical (LK) :: use_rfc6901_paths = .false. ! ! use the RFC 6901 standard for
212212 ! ! JSON paths. Otherwise, the original
213213 ! ! default method is used
214214
215+ character (kind= CK,len= 1 ) :: path_separator = dot ! ! The `path` separator to use
216+ ! ! in the "default" mode for
217+ ! ! the paths in the various
218+ ! ! `get_by_path` routines.
219+ ! ! Note: if `use_rfc6901_paths=true`
220+ ! ! then this is ignored.
221+
215222 contains
216223
217224 private
@@ -661,7 +668,8 @@ function initialize_json_core(verbose,compact_reals,&
661668 no_whitespace ,&
662669 unescape_strings ,&
663670 comment_char ,&
664- use_rfc6901_paths ) result(json_core_object)
671+ use_rfc6901_paths ,&
672+ path_separator ) result(json_core_object)
665673
666674 implicit none
667675
@@ -676,7 +684,8 @@ function initialize_json_core(verbose,compact_reals,&
676684 no_whitespace,&
677685 unescape_strings,&
678686 comment_char,&
679- use_rfc6901_paths)
687+ use_rfc6901_paths,&
688+ path_separator)
680689
681690 end function initialize_json_core
682691! *****************************************************************************************
@@ -708,7 +717,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
708717 no_whitespace ,&
709718 unescape_strings ,&
710719 comment_char ,&
711- use_rfc6901_paths )
720+ use_rfc6901_paths ,&
721+ path_separator )
712722
713723 implicit none
714724
@@ -763,6 +773,11 @@ subroutine json_initialize(json,verbose,compact_reals,&
763773 json% comment_char = comment_char
764774 end if
765775
776+ ! path separator:
777+ if (present (path_separator)) then
778+ json% path_separator = path_separator
779+ end if
780+
766781 ! Set the format for real numbers:
767782 ! [if not changing it, then it remains the same]
768783
@@ -4058,12 +4073,10 @@ end subroutine json_get_by_path
40584073! ### Notes
40594074! The following special characters are used to denote paths:
40604075!
4061- ! ````
4062- ! $ - root
4063- ! @ - this
4064- ! . - child object member
4065- ! [] or () - child array element
4066- ! ````
4076+ ! * `$` - root
4077+ ! * `@` - this
4078+ ! * `.` - child object member (note this can be changed using `json%path_separator`)
4079+ ! * `[]` or `()` - child array element
40674080!
40684081! Thus, if any of these characters are present in the name key,
40694082! this routine cannot be used to get the value.
@@ -4128,27 +4141,6 @@ subroutine json_get_by_path_default(json, me, path, p, found)
41284141 p = > me
41294142 child_i = i + 1
41304143
4131- case (child)
4132-
4133- ! get child member from p
4134- if (child_i < i) then
4135- nullify(tmp)
4136- call json% get_child(p, path(child_i:i-1 ), tmp)
4137- p = > tmp
4138- nullify(tmp)
4139- else
4140- child_i = i + 1
4141- cycle
4142- end if
4143-
4144- if (.not. associated (p)) then
4145- call json% throw_exception(' Error in json_get_by_path:' // &
4146- ' Error getting child member.' )
4147- exit
4148- end if
4149-
4150- child_i = i+1
4151-
41524144 case (start_array,start_array_alt)
41534145
41544146 ! ....Modified to allow for 'var[3]' style syntax
@@ -4190,6 +4182,31 @@ subroutine json_get_by_path_default(json, me, path, p, found)
41904182
41914183 child_i= i + 1
41924184
4185+ case default
4186+
4187+ if (c== json% path_separator) then
4188+
4189+ ! get child member from p
4190+ if (child_i < i) then
4191+ nullify(tmp)
4192+ call json% get_child(p, path(child_i:i-1 ), tmp)
4193+ p = > tmp
4194+ nullify(tmp)
4195+ else
4196+ child_i = i + 1
4197+ cycle
4198+ end if
4199+
4200+ if (.not. associated (p)) then
4201+ call json% throw_exception(' Error in json_get_by_path:' // &
4202+ ' Error getting child member.' )
4203+ exit
4204+ end if
4205+
4206+ child_i = i+1
4207+
4208+ end if
4209+
41934210 end select
41944211
41954212 end do
@@ -4260,7 +4277,7 @@ end subroutine json_get_by_path_default
42604277! ### Reference
42614278! * [JavaScript Object Notation (JSON) Pointer](https://tools.ietf.org/html/rfc6901)
42624279!
4263- ! @note Not doing anything special about the "-" character to index an array.
4280+ ! @note Not doing anything special about the `-` character to index an array.
42644281! This is considered a normal error.
42654282!
42664283! @warning Not checking if the member that is referenced is unique.
@@ -4473,7 +4490,7 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
44734490 logical (LK),intent (in ),optional :: use_alt_array_tokens ! ! if true, then '()' are used for array elements
44744491 ! ! otherwise, '[]' are used [default]
44754492 character (kind= CK,len= 1 ),intent (in ),optional :: path_sep ! ! character to use for path separator
4476- ! ! (default is '.' )
4493+ ! ! (otherwise use `json%path_separator` )
44774494
44784495 type (json_value),pointer :: tmp ! ! for traversing the structure
44794496 type (json_value),pointer :: element ! ! for traversing the structure
@@ -4609,11 +4626,11 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
46094626
46104627 contains
46114628
4612- subroutine add_to_path (str ,dot )
4629+ subroutine add_to_path (str ,path_sep )
46134630 ! ! prepend the string to the path
46144631 implicit none
46154632 character (kind= CK,len=* ),intent (in ) :: str ! ! string to prepend to `path`
4616- character (kind= CK,len= 1 ),intent (in ),optional :: dot
4633+ character (kind= CK,len= 1 ),intent (in ),optional :: path_sep
46174634 ! ! path separator (default is '.').
46184635 ! ! (ignored if `json%use_rfc6901_paths=.true.`)
46194636
@@ -4629,10 +4646,12 @@ subroutine add_to_path(str,dot)
46294646 if (path== CK_' ' ) then
46304647 path = str
46314648 else
4632- if (present (dot)) then
4633- path = str// dot// path
4649+ if (present (path_sep)) then
4650+ ! use user specified:
4651+ path = str// path_sep// path
46344652 else
4635- path = str// child// path
4653+ ! use the default:
4654+ path = str// json% path_separator// path
46364655 end if
46374656 end if
46384657 end if
0 commit comments