@@ -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
@@ -653,7 +660,8 @@ function initialize_json_core(verbose,compact_reals,&
653660 no_whitespace ,&
654661 unescape_strings ,&
655662 comment_char ,&
656- use_rfc6901_paths ) result(json_core_object)
663+ use_rfc6901_paths ,&
664+ path_separator ) result(json_core_object)
657665
658666 implicit none
659667
@@ -668,7 +676,8 @@ function initialize_json_core(verbose,compact_reals,&
668676 no_whitespace,&
669677 unescape_strings,&
670678 comment_char,&
671- use_rfc6901_paths)
679+ use_rfc6901_paths,&
680+ path_separator)
672681
673682 end function initialize_json_core
674683! *****************************************************************************************
@@ -700,7 +709,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
700709 no_whitespace ,&
701710 unescape_strings ,&
702711 comment_char ,&
703- use_rfc6901_paths )
712+ use_rfc6901_paths ,&
713+ path_separator )
704714
705715 implicit none
706716
@@ -755,6 +765,11 @@ subroutine json_initialize(json,verbose,compact_reals,&
755765 json% comment_char = comment_char
756766 end if
757767
768+ ! path separator:
769+ if (present (path_separator)) then
770+ json% path_separator = path_separator
771+ end if
772+
758773 ! Set the format for real numbers:
759774 ! [if not changing it, then it remains the same]
760775
@@ -3894,12 +3909,10 @@ end subroutine json_get_by_path
38943909! ### Notes
38953910! The following special characters are used to denote paths:
38963911!
3897- ! ````
3898- ! $ - root
3899- ! @ - this
3900- ! . - child object member
3901- ! [] or () - child array element
3902- ! ````
3912+ ! * `$` - root
3913+ ! * `@` - this
3914+ ! * `.` - child object member (note this can be changed using `json%path_separator`)
3915+ ! * `[]` or `()` - child array element
39033916!
39043917! Thus, if any of these characters are present in the name key,
39053918! this routine cannot be used to get the value.
@@ -3964,27 +3977,6 @@ subroutine json_get_by_path_default(json, me, path, p, found)
39643977 p = > me
39653978 child_i = i + 1
39663979
3967- case (child)
3968-
3969- ! get child member from p
3970- if (child_i < i) then
3971- nullify(tmp)
3972- call json% get_child(p, path(child_i:i-1 ), tmp)
3973- p = > tmp
3974- nullify(tmp)
3975- else
3976- child_i = i + 1
3977- cycle
3978- end if
3979-
3980- if (.not. associated (p)) then
3981- call json% throw_exception(' Error in json_get_by_path:' // &
3982- ' Error getting child member.' )
3983- exit
3984- end if
3985-
3986- child_i = i+1
3987-
39883980 case (start_array,start_array_alt)
39893981
39903982 ! ....Modified to allow for 'var[3]' style syntax
@@ -4026,6 +4018,31 @@ subroutine json_get_by_path_default(json, me, path, p, found)
40264018
40274019 child_i= i + 1
40284020
4021+ case default
4022+
4023+ if (c== json% path_separator) then
4024+
4025+ ! get child member from p
4026+ if (child_i < i) then
4027+ nullify(tmp)
4028+ call json% get_child(p, path(child_i:i-1 ), tmp)
4029+ p = > tmp
4030+ nullify(tmp)
4031+ else
4032+ child_i = i + 1
4033+ cycle
4034+ end if
4035+
4036+ if (.not. associated (p)) then
4037+ call json% throw_exception(' Error in json_get_by_path:' // &
4038+ ' Error getting child member.' )
4039+ exit
4040+ end if
4041+
4042+ child_i = i+1
4043+
4044+ end if
4045+
40294046 end select
40304047
40314048 end do
@@ -4096,7 +4113,7 @@ end subroutine json_get_by_path_default
40964113! ### Reference
40974114! * [JavaScript Object Notation (JSON) Pointer](https://tools.ietf.org/html/rfc6901)
40984115!
4099- ! @note Not doing anything special about the "-" character to index an array.
4116+ ! @note Not doing anything special about the `-` character to index an array.
41004117! This is considered a normal error.
41014118!
41024119! @warning Not checking if the member that is referenced is unique.
@@ -4309,7 +4326,7 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
43094326 logical (LK),intent (in ),optional :: use_alt_array_tokens ! ! if true, then '()' are used for array elements
43104327 ! ! otherwise, '[]' are used [default]
43114328 character (kind= CK,len= 1 ),intent (in ),optional :: path_sep ! ! character to use for path separator
4312- ! ! (default is '.' )
4329+ ! ! (otherwise use `json%path_separator` )
43134330
43144331 type (json_value),pointer :: tmp ! ! for traversing the structure
43154332 type (json_value),pointer :: element ! ! for traversing the structure
@@ -4445,11 +4462,11 @@ subroutine json_get_path(json, p, path, found, use_alt_array_tokens, path_sep)
44454462
44464463 contains
44474464
4448- subroutine add_to_path (str ,dot )
4465+ subroutine add_to_path (str ,path_sep )
44494466 ! ! prepend the string to the path
44504467 implicit none
44514468 character (kind= CK,len=* ),intent (in ) :: str ! ! string to prepend to `path`
4452- character (kind= CK,len= 1 ),intent (in ),optional :: dot
4469+ character (kind= CK,len= 1 ),intent (in ),optional :: path_sep
44534470 ! ! path separator (default is '.').
44544471 ! ! (ignored if `json%use_rfc6901_paths=.true.`)
44554472
@@ -4465,10 +4482,12 @@ subroutine add_to_path(str,dot)
44654482 if (path== CK_' ' ) then
44664483 path = str
44674484 else
4468- if (present (dot)) then
4469- path = str// dot// path
4485+ if (present (path_sep)) then
4486+ ! use user specified:
4487+ path = str// path_sep// path
44704488 else
4471- path = str// child// path
4489+ ! use the default:
4490+ path = str// json% path_separator// path
44724491 end if
44734492 end if
44744493 end if
0 commit comments