Destroy a json_value linked-list structure.
The original FSON version of this routine was not properly freeing the memory. It was rewritten.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(json_core), | intent(inout) | :: | json | |||
type(json_value), | pointer | :: | p | variable to destroy |
||
logical(kind=LK), | intent(in), | optional | :: | destroy_next | if true, then |
recursive subroutine json_value_destroy(json,p,destroy_next)
implicit none
class(json_core),intent(inout) :: json
type(json_value),pointer :: p !! variable to destroy
logical(LK),intent(in),optional :: destroy_next !! if true, then `p%next`
!! is also destroyed (default is true)
logical(LK) :: des_next
type(json_value), pointer :: child
if (associated(p)) then
if (present(destroy_next)) then
des_next = destroy_next
else
des_next = .true.
end if
if (allocated(p%name)) deallocate(p%name)
call destroy_json_data(p)
if (associated(p%children)) then
do while (p%n_children > 0)
child => p%children
if (associated(child)) then
p%children => p%children%next
p%n_children = p%n_children - 1
call json_value_destroy(json,child,.false.)
else
call json%throw_exception('Error in json_value_destroy: '//&
'Malformed JSON linked list')
exit
end if
end do
nullify(p%children)
nullify(child)
end if
if (associated(p%next) .and. des_next) call json_value_destroy(json,p%next)
if (associated(p%previous)) nullify(p%previous)
if (associated(p%parent)) nullify(p%parent)
if (associated(p%tail)) nullify(p%tail)
deallocate(p)
nullify(p)
end if
end subroutine json_value_destroy