parse_number Subroutine

private subroutine parse_number(json, unit, str, value)

Read a numerical value from the file (or string). The routine will determine if it is an integer or a real, and allocate the type accordingly.

Type Bound

json_core

Arguments

Type IntentOptional Attributes Name
class(json_core), intent(inout) :: json
integer(kind=IK), intent(in) :: unit

file unit number (if parsing from a file)

character(kind=CK, len=*), intent(in) :: str

JSON string (if parsing from a string)

type(json_value), pointer :: value

Calls

proc~~parse_number~~CallsGraph proc~parse_number json_core%parse_number proc~json_clear_exceptions json_core%json_clear_exceptions proc~parse_number->proc~json_clear_exceptions proc~pop_char json_core%pop_char proc~parse_number->proc~pop_char proc~push_char json_core%push_char proc~parse_number->proc~push_char proc~string_to_dble json_core%string_to_dble proc~parse_number->proc~string_to_dble proc~string_to_int json_core%string_to_int proc~parse_number->proc~string_to_int proc~to_integer json_core%to_integer proc~parse_number->proc~to_integer proc~to_real json_core%to_real proc~parse_number->proc~to_real none~throw_exception json_core%throw_exception proc~push_char->none~throw_exception proc~integer_to_string integer_to_string proc~push_char->proc~integer_to_string proc~string_to_dble->none~throw_exception proc~string_to_int->none~throw_exception proc~string_to_integer string_to_integer proc~string_to_int->proc~string_to_integer proc~destroy_json_data destroy_json_data proc~to_integer->proc~destroy_json_data proc~to_real->proc~destroy_json_data proc~json_throw_exception json_core%json_throw_exception none~throw_exception->proc~json_throw_exception

Called by

proc~~parse_number~~CalledByGraph proc~parse_number json_core%parse_number proc~parse_value_nonrecursive parse_value_nonrecursive proc~parse_value_nonrecursive->proc~parse_number proc~parse_value_recursive parse_value_recursive proc~parse_value_recursive->proc~parse_number

Source Code

    subroutine parse_number(json, unit, str, value)

    implicit none

    class(json_core),intent(inout)      :: json
    integer(IK),intent(in)              :: unit   !! file unit number (if parsing from a file)
    character(kind=CK,len=*),intent(in) :: str    !! JSON string (if parsing from a string)
    type(json_value),pointer            :: value

    character(kind=CK,len=:),allocatable :: tmp !! temp string
    character(kind=CK,len=:),allocatable :: saved_err_message !! temp error message for
                                                              !! string to int conversion
    character(kind=CK,len=1) :: c           !! character returned by [[pop_char]]
    logical(LK)              :: eof         !! end of file flag
    real(RK)                 :: rval        !! real value
    integer(IK)              :: ival        !! integer value
    logical(LK)              :: first       !! first character
    logical(LK)              :: is_integer  !! it is an integer
    integer(IK)              :: ip          !! index to put next character
                                            !! [to speed up by reducing the number
                                            !! of character string reallocations]
    integer(IK)              :: ltmp        !! length of `tmp`

    if (.not. json%exception_thrown) then

        ! can use the max number size here (it will be expanded if necessary)
        tmp = repeat(space,max_numeric_str_len)
        ltmp = max_numeric_str_len
        ip = 1
        first = .true.
        is_integer = .true.  !assume it may be an integer, unless otherwise determined

        !read one character at a time and accumulate the string:
        do

            !get the next character:
            call json%pop_char(unit, str=str, eof=eof, skip_ws=.true., c=c)

            select case (c)

            case(CK_'0':CK_'9')    !valid characters for numbers

                call add_to_tmp(c)  !add it to the string

            case(CK_'.',CK_'E',CK_'e',CK_'D',CK_'d')    !can be present in real numbers

                if (is_integer) is_integer = .false.
                call add_to_tmp(c)  !add it to the string

            case(CK_'-',CK_'+')    !note: allowing a '+' as the first character here.

                if (is_integer .and. (.not. first)) is_integer = .false.
                call add_to_tmp(c)  !add it to the string

            case default

                !push back the last character read:
                call json%push_char(c)

                !string to value:
                if (is_integer) then
                    ! it is an integer:
                    ival = json%string_to_int(tmp)

                    if (json%exception_thrown .and. .not. json%strict_integer_type_checking) then
                        ! if it couldn't be converted to an integer,
                        ! then try to convert it to a real value and see if that works

                        saved_err_message = json%err_message  ! keep the original error message
                        call json%clear_exceptions()          ! clear exceptions
                        rval = json%string_to_dble(tmp)
                        if (json%exception_thrown) then
                            ! restore original error message and continue
                            json%err_message = saved_err_message
                            call json%to_integer(value,ival) ! just so we have something
                        else
                            ! in this case, we return a real
                            call json%to_real(value,rval)
                        end if

                    else
                        call json%to_integer(value,ival)
                    end if

                else
                    ! it is a real:
                    rval = json%string_to_dble(tmp)
                    call json%to_real(value,rval)
                end if

                exit    !finished

            end select

            if (first) first = .false.

        end do

    end if

    contains
        subroutine add_to_tmp(c)
            !! add character `c` to `tmp`, expanding if necessary
            character(kind=CK,len=1),intent(in) :: c
            if (ip>ltmp) then
                tmp = tmp // blank_chunk
                ltmp = len(tmp)
            end if
            tmp(ip:ip) = c
            ip = ip + 1
        end subroutine add_to_tmp

    end subroutine parse_number