Core parsing routine.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(json_core), | intent(inout) | :: | json | |||
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 |
recursive subroutine parse_array(json, unit, str, array)
implicit none
class(json_core),intent(inout) :: json
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 (json%exception_thrown) exit
! try to parse an element value
nullify(element)
call json_value_create(element)
call json%parse_value(unit, str, element)
if (json%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 = json%pop_char(unit, str=str, eof = eof, skip_ws = .true.)
if (eof) then
! The file ended before array was finished:
call json%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 json%throw_exception('Error in parse_array: '//&
'Unexpected character encountered when parsing array.')
exit
end if
end do
end subroutine parse_array