parse_single_json_attribute Subroutine

subroutine parse_single_json_attribute(line, attrs, n_attrs)

Parse a single JSON attribute object.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: line

Source line for one JSON attribute object

type(onnx_attribute_type), intent(inout), allocatable :: attrs(:)

Destination list of parsed attributes

integer, intent(inout) :: n_attrs

Number of valid attributes in attrs


Source Code

  subroutine parse_single_json_attribute(line, attrs, n_attrs)
    !! Parse a single JSON attribute object.
    implicit none

    ! Arguments
    character(*), intent(in) :: line
    !! Source line for one JSON attribute object
    type(onnx_attribute_type), allocatable, intent(inout) :: attrs(:)
    !! Destination list of parsed attributes
    integer, intent(inout) :: n_attrs
    !! Number of valid attributes in attrs

    ! Local variables
    type(onnx_attribute_type) :: attr
    !! Parsed attribute record
    character(64) :: attr_type_str
    !! Raw attribute type token
    character(256) :: val_str
    !! Temporary attribute value buffer

    attr%name = ''
    attr%type = ''
    allocate(character(0) :: attr%val)

    call extract_json_string(line, '"name"', val_str)
    attr%name = trim(val_str)

    call extract_json_string(line, '"type"', attr_type_str)
    attr%type = to_lower(trim(attr_type_str))

    select case(trim(attr%type))
    case('int')
       call extract_json_string(line, '"i"', val_str)
       attr%val = trim(val_str)
    case('float')
       call extract_json_string(line, '"f"', val_str)
       if(len_trim(val_str) .eq. 0)then
          block
            integer :: fp, fp2

            fp = index(line, '"f"')
            if(fp .gt. 0)then
               fp = fp + 3
               fp = fp + index(line(fp:), ':')
               val_str = trim(adjustl(line(fp:)))
               fp2 = scan(val_str, ',}')
               if(fp2 .gt. 0) val_str = val_str(1:fp2-1)
            end if
          end block
       end if
       attr%val = trim(val_str)
    case('ints')
       block
         integer :: ip, ip2
         character(256) :: ints_str

         ints_str = ''
         ip = index(line, '"ints"')
         if(ip .gt. 0)then
            ip = ip + 6
            ip = ip + index(line(ip:), '[') - 1
            ip2 = index(line(ip:), ']')
            if(ip2 .gt. 0)then
               ints_str = line(ip+1:ip+ip2-2)
               do ip2 = 1, len_trim(ints_str)
                  if(ints_str(ip2:ip2) .eq. ',' .or. &
                       ints_str(ip2:ip2) .eq. '"') ints_str(ip2:ip2) = ' '
               end do
            end if
         end if
         attr%val = trim(adjustl(ints_str))
       end block
    case('string')
       call extract_json_string(line, '"s"', val_str)
       attr%val = trim(val_str)
    case default
       attr%val = ''
    end select

    n_attrs = n_attrs + 1
    attrs = [attrs, attr]

  end subroutine parse_single_json_attribute