Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=IK), | intent(in) | :: | unit | file unit number |
||
character(kind=CK,len=*), | intent(in) | :: | str | string containing JSON data (only used if unit=0) |
||
type(json_value), | intent(inout), | pointer | :: | value | JSON data that is extracted |
Core parsing routine.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
logical(kind=LK), | public | :: | eof | ||||
character(kind=CK,len=1), | public | :: | c | ||||
character(kind=CK,len=:), | public, | allocatable | :: | tmp |
recursive subroutine parse_value(unit, str, value)
implicit none
integer(IK),intent(in) :: unit !! file unit number
character(kind=CK,len=*),intent(in) :: str !! string containing JSON data (only used if unit=0)
type(json_value),pointer :: value !! JSON data that is extracted
logical(LK) :: eof
character(kind=CK,len=1) :: c
character(kind=CK,len=:),allocatable :: tmp !this is a work-around for a bug
! in the gfortran 4.9 compiler.
if (.not. exception_thrown) then
!the routine is being called incorrectly.
if (.not. associated(value)) then
call throw_exception('Error in parse_value: value pointer not associated.')
end if
! pop the next non whitespace character off the file
c = pop_char(unit, str=str, eof = eof, skip_ws = .true.)
if (eof) then
return
else
select case (c)
case (start_object)
! start object
call to_object(value) !allocate class
call parse_object(unit, str, value)
case (start_array)
! start array
call to_array(value) !allocate class
call parse_array(unit, str, value)
case (end_array)
! end an empty array
call push_char(c)
nullify(value)
case (quotation_mark)
! string
call to_string(value) !allocate class
select case (value%var_type)
case (json_string)
call parse_string(unit, str, tmp) !write to a tmp variable because of
value%str_value = tmp ! a bug in 4.9 gfortran compiler.
deallocate(tmp) !
end select
case (CK_'t') !true_str(1:1) gfortran bug work around
!true
call parse_for_chars(unit, str, true_str(2:))
!allocate class and set value:
if (.not. exception_thrown) call to_logical(value,.true.)
case (CK_'f') !false_str(1:1) gfortran bug work around
!false
call parse_for_chars(unit, str, false_str(2:))
!allocate class and set value:
if (.not. exception_thrown) call to_logical(value,.false.)
case (CK_'n') !null_str(1:1) gfortran bug work around
!null
call parse_for_chars(unit, str, null_str(2:))
if (.not. exception_thrown) call to_null(value) !allocate class
case(CK_'-', CK_'0': CK_'9')
call push_char(c)
call parse_number(unit, str, value)
case default
call throw_exception('Error in parse_value:'//&
' Unexpected character while parsing value. "'//&
c//'"')
end select
end if
end if
end subroutine parse_value