json_get_string Subroutine

private subroutine json_get_string(json, me, value)

Get a character string from a json_value.

Arguments

Type IntentOptional AttributesName
class(json_core), intent(inout) :: json
type(json_value), intent(in), pointer:: me
character(kind=CK,len=:), intent(out), allocatable:: value

Calls

proc~~json_get_string~~CallsGraph proc~json_get_string json_get_string proc~escape_string escape_string proc~json_get_string->proc~escape_string proc~integer_to_string integer_to_string proc~json_get_string->proc~integer_to_string proc~real_to_string real_to_string proc~json_get_string->proc~real_to_string proc~valid_json_hex valid_json_hex proc~escape_string->proc~valid_json_hex proc~compact_real_string compact_real_string proc~real_to_string->proc~compact_real_string

Contents

Source Code


Source Code

    subroutine json_get_string(json, me, value)

    implicit none

    class(json_core),intent(inout)                   :: json
    type(json_value),pointer,intent(in)              :: me
    character(kind=CK,len=:),allocatable,intent(out) :: value

    value = CK_''
    if (.not. json%exception_thrown) then

        if (me%var_type == json_string) then

            if (allocated(me%str_value)) then
                if (json%unescaped_strings) then
                    ! default: it is stored already unescaped:
                    value = me%str_value
                else
                    ! return the escaped version:
                    call escape_string(me%str_value, value, json%escape_solidus)
                end if
            else
               call json%throw_exception('Error in json_get_string: '//&
                                         'me%str_value not allocated')
            end if

        else

            if (json%strict_type_checking) then
                if (allocated(me%name)) then
                    call json%throw_exception('Error in json_get_string:'//&
                                              ' Unable to resolve value to string: '//me%name)
                else
                    call json%throw_exception('Error in json_get_string:'//&
                                              ' Unable to resolve value to string')
                end if
            else

                select case (me%var_type)

                case (json_integer)

                    if (allocated(me%int_value)) then
                        value = repeat(space, max_integer_str_len)
                        call integer_to_string(me%int_value,int_fmt,value)
                        value = trim(value)
                    else
                        call json%throw_exception('Error in json_get_string: '//&
                                                  'me%int_value not allocated')
                    end if

                case (json_real)

                    if (allocated(me%dbl_value)) then
                        value = repeat(space, max_numeric_str_len)
                        call real_to_string(me%dbl_value,json%real_fmt,&
                                            json%non_normals_to_null,&
                                            json%compact_real,value)
                        value = trim(value)
                    else
                        call json%throw_exception('Error in json_get_string: '//&
                                                  'me%int_value not allocated')
                    end if

                case (json_logical)

                    if (allocated(me%log_value)) then
                        if (me%log_value) then
                            value = true_str
                        else
                            value = false_str
                        end if
                    else
                        call json%throw_exception('Error in json_get_string: '//&
                                                  'me%log_value not allocated')
                    end if

                case (json_null)

                    value = null_str

                case default
                    if (allocated(me%name)) then
                        call json%throw_exception('Error in json_get_string: '//&
                                                  'Unable to resolve value to characters: '//&
                                                  me%name)
                    else
                        call json%throw_exception('Error in json_get_string: '//&
                                                  'Unable to resolve value to characters')
                    end if
                end select

            end if
        end if

    end if

    end subroutine json_get_string