json_is_vector Function

private function json_is_vector(json, p) result(is_vector)

Returns true if all the children are the same type (and a scalar). Note that integers and reals are considered the same type for this purpose. This routine is used for the compress_vectors option.

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: json
type(json_value), pointer:: p

Return Value logical(kind=LK)

if all elements of a vector are scalars of the same type


Contents

Source Code


Source Code

    function json_is_vector(json, p) result(is_vector)

    implicit none

    class(json_core),intent(inout) :: json
    type(json_value),pointer       :: p
    logical(LK)                    :: is_vector  !! if all elements of a vector
                                                 !! are scalars of the same type

    integer(IK) :: var_type_prev !! for getting the variable type of children
    integer(IK) :: var_type !! for getting the variable type of children
    type(json_value),pointer :: element !! for getting children
    integer(IK) :: i !! counter
    integer(IK) :: count !! number of children

    integer(IK),parameter :: json_invalid = -1_IK  !! to initialize the flag. an invalid value
    integer(IK),parameter :: json_numeric = -2_IK  !! indicates `json_integer` or `json_real`

    if (json%compress_vectors) then
        ! check to see if every child is the same type,
        ! and a scalar:
        is_vector = .true.
        var_type_prev = json_invalid
        count = json%count(p)
        element => p%children
        do i = 1_IK, count
            if (.not. associated(element)) then
                call json%throw_exception('Error in json_is_vector: '//&
                                          'Malformed JSON linked list')
                return
            end if
            ! check variable type of all the children.
            ! They must all be the same, and a scalar.
            call json%info(element,var_type=var_type)
            ! special check for numeric values:
            if (var_type==json_integer .or. var_type==json_real) var_type = json_numeric
            if (var_type==json_object .or. &
                var_type==json_array .or. &
                (i>1_IK .and. var_type/=var_type_prev)) then
                is_vector = .false.
                exit
            end if
            var_type_prev = var_type
            ! get the next child the list:
            element => element%next
        end do
    else
        is_vector = .false.
    end if

    end function json_is_vector