@@ -204,14 +204,21 @@ module json_value_module
204
204
logical (LK) :: allow_comments = .true. ! ! if true, any comments will be ignored when
205
205
! ! parsing a file. The comment token is defined
206
206
! ! 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 '`#`'.
210
210
211
211
logical (LK) :: use_rfc6901_paths = .false. ! ! use the RFC 6901 standard for
212
212
! ! JSON paths. Otherwise, the original
213
213
! ! default method is used
214
214
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
+
215
222
contains
216
223
217
224
private
@@ -653,7 +660,8 @@ function initialize_json_core(verbose,compact_reals,&
653
660
no_whitespace ,&
654
661
unescape_strings ,&
655
662
comment_char ,&
656
- use_rfc6901_paths ) result(json_core_object)
663
+ use_rfc6901_paths ,&
664
+ path_separator ) result(json_core_object)
657
665
658
666
implicit none
659
667
@@ -668,7 +676,8 @@ function initialize_json_core(verbose,compact_reals,&
668
676
no_whitespace,&
669
677
unescape_strings,&
670
678
comment_char,&
671
- use_rfc6901_paths)
679
+ use_rfc6901_paths,&
680
+ path_separator)
672
681
673
682
end function initialize_json_core
674
683
! *****************************************************************************************
@@ -700,7 +709,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
700
709
no_whitespace ,&
701
710
unescape_strings ,&
702
711
comment_char ,&
703
- use_rfc6901_paths )
712
+ use_rfc6901_paths ,&
713
+ path_separator )
704
714
705
715
implicit none
706
716
@@ -755,6 +765,11 @@ subroutine json_initialize(json,verbose,compact_reals,&
755
765
json% comment_char = comment_char
756
766
end if
757
767
768
+ ! path separator:
769
+ if (present (path_separator)) then
770
+ json% path_separator = path_separator
771
+ end if
772
+
758
773
! Set the format for real numbers:
759
774
! [if not changing it, then it remains the same]
760
775
@@ -3894,12 +3909,10 @@ end subroutine json_get_by_path
3894
3909
! ### Notes
3895
3910
! The following special characters are used to denote paths:
3896
3911
!
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
3903
3916
!
3904
3917
! Thus, if any of these characters are present in the name key,
3905
3918
! this routine cannot be used to get the value.
@@ -3964,27 +3977,6 @@ subroutine json_get_by_path_default(json, me, path, p, found)
3964
3977
p = > me
3965
3978
child_i = i + 1
3966
3979
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
-
3988
3980
case (start_array,start_array_alt)
3989
3981
3990
3982
! ....Modified to allow for 'var[3]' style syntax
@@ -4026,6 +4018,31 @@ subroutine json_get_by_path_default(json, me, path, p, found)
4026
4018
4027
4019
child_i= i + 1
4028
4020
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
+
4029
4046
end select
4030
4047
4031
4048
end do
@@ -4096,7 +4113,7 @@ end subroutine json_get_by_path_default
4096
4113
! ### Reference
4097
4114
! * [JavaScript Object Notation (JSON) Pointer](https://tools.ietf.org/html/rfc6901)
4098
4115
!
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.
4100
4117
! This is considered a normal error.
4101
4118
!
4102
4119
! @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)
4309
4326
logical (LK),intent (in ),optional :: use_alt_array_tokens ! ! if true, then '()' are used for array elements
4310
4327
! ! otherwise, '[]' are used [default]
4311
4328
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` )
4313
4330
4314
4331
type (json_value),pointer :: tmp ! ! for traversing the structure
4315
4332
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)
4445
4462
4446
4463
contains
4447
4464
4448
- subroutine add_to_path (str ,dot )
4465
+ subroutine add_to_path (str ,path_sep )
4449
4466
! ! prepend the string to the path
4450
4467
implicit none
4451
4468
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
4453
4470
! ! path separator (default is '.').
4454
4471
! ! (ignored if `json%use_rfc6901_paths=.true.`)
4455
4472
@@ -4465,10 +4482,12 @@ subroutine add_to_path(str,dot)
4465
4482
if (path== CK_' ' ) then
4466
4483
path = str
4467
4484
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
4470
4488
else
4471
- path = str// child// path
4489
+ ! use the default:
4490
+ path = str// json% path_separator// path
4472
4491
end if
4473
4492
end if
4474
4493
end if
0 commit comments