fson_value_print Subroutine

public recursive subroutine fson_value_print(this, indent)

Arguments

Type IntentOptional AttributesName
type(fson_value), pointer:: this
integer, intent(in), optional :: indent

Contents

Source Code


Source Code

  recursive subroutine fson_value_print(this, indent)
    use fson_string_m, only: fson_string_copy
    implicit none

    type(fson_value), pointer :: this, element
    integer, optional, intent(in) :: indent
    character(len=1024) :: tmp_chars
    integer :: tab, i, count, spaces

    !+PJK
    if (.not.associated(this)) return
    !-PJK

    if (present(indent)) then
       tab = indent
    else
       tab = 0
    end if

    spaces = tab * 2

    select case (this % value_type)
    case(TYPE_OBJECT)
       print *, repeat(" ", spaces), "{"
       count = fson_value_count(this)
       do i = 1, count
          ! get the element
          element => fson_value_get(this, i)
          ! get the name
          call fson_string_copy(element % name, tmp_chars)
          ! print the name
          print *, repeat(" ", spaces), '"', trim(tmp_chars), '":'
          ! recursive print of the element
          call fson_value_print(element, tab + 1)
          ! print the separator if required
          if (i < count) then
             print *, repeat(" ", spaces), ","
          end if
       end do

       print *, repeat(" ", spaces), "}"
    case (TYPE_ARRAY)
       print *, repeat(" ", spaces), "["
       count = fson_value_count(this)
       do i = 1, count
          ! get the element
          element => fson_value_get(this, i)
          ! recursive print of the element
          call fson_value_print(element, tab + 1)
          ! print the separator if required
          if (i < count) then
             print *, ","
          end if
       end do
       print *, repeat(" ", spaces), "]"
    case (TYPE_NULL)
       print *, repeat(" ", spaces), "null"
    case (TYPE_STRING)
       call fson_string_copy(this % value_string, tmp_chars)
       print *, repeat(" ", spaces), '"', trim(tmp_chars), '"'
    case (TYPE_LOGICAL)
       if (this % value_logical) then
          print *, repeat(" ", spaces), "true"
       else
          print *, repeat(" ", spaces), "false"
       end if
    case (TYPE_INTEGER)
       print *, repeat(" ", spaces), this % value_integer
    case (TYPE_REAL)
       print *, repeat(" ", spaces), this % value_real  !  N.B. doubles will be shown as single precision
    end select
  end subroutine fson_value_print