Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CDK,len=*), | intent(in) | :: | file | JSON file name |
||
type(json_value), | intent(inout), | pointer | :: | p | output structure |
|
integer(kind=IK), | intent(in), | optional | :: | unit | file unit number (/= 0) |
Parse the JSON file and populate the json_value tree.
The inputs can be:
type(json_value),pointer :: p call json_parse(file='myfile.json', p=p)
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer(kind=IK), | public | :: | iunit | ||||
integer(kind=IK), | public | :: | istat | ||||
logical(kind=LK), | public | :: | is_open |
subroutine json_parse_file(file, p, unit)
implicit none
character(kind=CDK,len=*),intent(in) :: file !! JSON file name
type(json_value),pointer :: p !! output structure
integer(IK),intent(in),optional :: unit !! file unit number (/= 0)
integer(IK) :: iunit, istat
logical(LK) :: is_open
!clear any exceptions and initialize:
call json_initialize()
if ( present(unit) ) then
if (unit==0) then
call throw_exception('Error in json_parse_file: unit number must not be 0.')
return
end if
iunit = unit
!check to see if the file is already open
! if it is, then use it, otherwise open the file with the name given.
inquire(unit=iunit, opened=is_open, iostat=istat)
if (istat==0 .and. .not. is_open) then
! open the file
open ( unit = iunit, &
file = file, &
status = 'OLD', &
action = 'READ', &
form = form_spec, &
access = access_spec, &
iostat = istat &
FILE_ENCODING )
else
!if the file is already open, then we need to make sure
! that it is open with the correct form/access/etc...
end if
else
! open the file with a new unit number:
open ( newunit = iunit, &
file = file, &
status = 'OLD', &
action = 'READ', &
form = form_spec, &
access = access_spec, &
iostat = istat &
FILE_ENCODING )
end if
if (istat==0) then
! create the value and associate the pointer
call json_value_create(p)
! Note: the name of the root json_value doesn't really matter,
! but we'll allocate something here just in case.
p%name = trim(file) !use the file name
! parse as a value
call parse_value(unit=iunit, str=CK_'', value=p)
if (exception_thrown) call annotate_invalid_json(iunit,CK_'')
! close the file if necessary
close(unit=iunit, iostat=istat)
else
call throw_exception('Error in json_parse_file: Error opening file: '//trim(file))
nullify(p)
end if
end subroutine json_parse_file