Returns the json_value pointer given the path string.
type(json_value),pointer :: dat,p logical :: found !... call json%get(dat,'data(2).version',p,found)
The following special characters are used to denote paths:
$ - root @ - this . - child object member [] or () - child array element
Thus, if any of these characters are present in the name key,
this routine cannot be used to get the value.
In that case, the get_child
methods would need to be used.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(json_core), | intent(inout) | :: | json | |||
type(json_value), | intent(in), | pointer | :: | me | a JSON linked list |
|
character(kind=CK,len=*), | intent(in) | :: | path | path to the variable |
||
type(json_value), | intent(out), | pointer | :: | p | pointer to the variable specify by |
|
logical(kind=LK), | intent(out), | optional | :: | found | true if it was found |
subroutine json_get_by_path(json, me, path, p, found)
implicit none
class(json_core),intent(inout) :: json
type(json_value),pointer,intent(in) :: me !! a JSON linked list
character(kind=CK,len=*),intent(in) :: path !! path to the variable
type(json_value),pointer,intent(out) :: p !! pointer to the variable specify by `path`
logical(LK),intent(out),optional :: found !! true if it was found
integer(IK) :: i
integer(IK) :: length
integer(IK) :: child_i
character(kind=CK,len=1) :: c
logical(LK) :: array
type(json_value),pointer :: tmp
nullify(p)
if (.not. json%exception_thrown) then
! default to assuming relative to this
p => me
child_i = 1
array = .false.
!keep trailing space or not:
if (json%trailing_spaces_significant) then
length = len(path)
else
length = len_trim(path)
end if
do i=1, length
c = path(i:i)
select case (c)
case (root)
! root
do while (associated (p%parent))
p => p%parent
end do
child_i = i + 1
case (this)
! this
p => me
child_i = i + 1
case (child)
! get child member from p
if (child_i < i) then
nullify(tmp)
call json%get_child(p, path(child_i:i-1), tmp)
p => tmp
nullify(tmp)
else
child_i = i + 1
cycle
end if
if (.not. associated(p)) then
call json%throw_exception('Error in json_get_by_path:'//&
' Error getting child member.')
exit
end if
child_i = i+1
case (start_array,start_array_alt)
!....Modified to allow for 'var[3]' style syntax
!Note: jmozmoz/fson has a slightly different version of this...
! start looking for the array element index
array = .true.
! get child member from p
if (child_i < i) then
nullify(tmp)
call json%get_child(p, path(child_i:i-1), tmp)
p => tmp
nullify(tmp)
else
child_i = i + 1
cycle
end if
if (.not. associated(p)) then
call json%throw_exception('Error in json_get_by_path:'//&
' Error getting array element')
exit
end if
child_i = i + 1
case (end_array,end_array_alt)
if (.not.array) then
call json%throw_exception('Error in json_get_by_path: Unexpected ]')
exit
end if
array = .false.
child_i = json%string_to_integer(path(child_i:i-1))
nullify(tmp)
call json%get_child(p, child_i, tmp)
p => tmp
nullify(tmp)
child_i= i + 1
end select
end do
if (json%exception_thrown) then
if (present(found)) then
found = .false.
call json%clear_exceptions()
end if
else
! grab the last child if present in the path
if (child_i <= length) then
nullify(tmp)
call json%get_child(p, path(child_i:i-1), tmp)
p => tmp
nullify(tmp)
end if
if (associated(p)) then
if (present(found)) found = .true. !everything seems to be ok
else
call json%throw_exception('Error in json_get_by_path:'//&
' variable not found: '//trim(path))
if (present(found)) then
found = .false.
call json%clear_exceptions()
end if
end if
end if
else
if (present(found)) found = .false.
end if
end subroutine json_get_by_path