Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=IK), | intent(in) | :: | unit | file unit number (if parsing from a file) |
||
character(kind=CK,len=*), | intent(in) | :: | str | JSON string (if parsing from a string) |
||
type(json_value), | , | pointer | :: | array |
Core parsing routine.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
type(json_value), | public, | pointer | :: | element | |||
logical(kind=LK), | public | :: | eof | ||||
character(kind=CK,len=1), | public | :: | c |
recursive subroutine parse_array(unit, str, array)
implicit none
integer(IK),intent(in) :: unit !! file unit number (if parsing from a file)
character(kind=CK,len=*),intent(in) :: str !! JSON string (if parsing from a string)
type(json_value),pointer :: array
type(json_value),pointer :: element
logical(LK) :: eof
character(kind=CK,len=1) :: c
do
if (exception_thrown) exit
! try to parse an element value
nullify(element)
call json_value_create(element)
call parse_value(unit, str, element)
if (exception_thrown) then
if (associated(element)) call json_destroy(element)
exit
end if
! parse value will disassociate an empty array value
if (associated(element)) call json_add(array, element)
! popped the next character
c = pop_char(unit, str=str, eof = eof, skip_ws = .true.)
if (eof) then
! The file ended before array was finished:
call throw_exception('Error in parse_array: '//&
'End of file encountered when parsing an array.')
exit
else if (delimiter == c) then
! parse the next element
cycle
else if (end_array == c) then
! end of array
exit
else
call throw_exception('Error in parse_array: '//&
'Unexpected character encountered when parsing array.')
exit
end if
end do
end subroutine parse_array