convert Subroutine

private subroutine convert(json, p, var_type)

Convert an existing JSON variable p to a different variable type. The existing variable (and its children) is destroyed. It is replaced in the structure by a new variable of type var_type (which can be a json_null, json_object or json_array).

Arguments

TypeIntentOptionalAttributesName
class(json_core), intent(inout) :: json
type(json_value), pointer:: p

the variable to convert

integer(kind=IK), intent(in) :: var_type

the variable type to convert p to


Contents

Source Code


Source Code

    subroutine convert(json,p,var_type)

    implicit none

    class(json_core),intent(inout) :: json
    type(json_value),pointer :: p  !! the variable to convert
    integer(IK),intent(in) :: var_type !! the variable type to convert `p` to

    type(json_value),pointer :: tmp  !! temporary variable
    character(kind=CK,len=:),allocatable :: name !! the name of a JSON variable

    logical :: convert_it  !! if `p` needs to be converted

    convert_it = p%var_type /= var_type

    if (convert_it) then

        call json%info(p,name=name) ! get existing name

        select case (var_type)
        case(json_object)
            call json%create_object(tmp,name)
        case(json_array)
            call json%create_array(tmp,name)
        case(json_null)
            call json%create_null(tmp,name)
        case default
            call json%throw_exception('Error in convert: invalid var_type value.')
            return
        end select

        call json%replace(p,tmp,destroy=.true.)
        p => tmp
        nullify(tmp)

    end if

    end subroutine convert