Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(json_value), | intent(in), | pointer | :: | me | ||
character(kind=CK,len=:), | intent(out), | allocatable | :: | value |
Get a character string from a json_value.
subroutine json_get_string(me, value)
implicit none
type(json_value),pointer,intent(in) :: me
character(kind=CK,len=:),allocatable,intent(out) :: value
character(kind=CK ,len=:),allocatable :: s,pre,post
integer(IK) :: j,jprev,n
character(kind=CK,len=1) :: c
value = ''
if ( exception_thrown) return
select case (me%var_type)
case (json_string)
if (allocated(me%str_value)) then
!get the value as is:
s = me%str_value
! Now, have to remove the escape characters:
!
! '\"' quotation mark
! '\\' reverse solidus
! '\/' solidus
! '\b' backspace
! '\f' formfeed
! '\n' newline (LF)
! '\r' carriage return (CR)
! '\t' horizontal tab
! '\uXXXX' 4 hexadecimal digits
!
!initialize:
n = len(s)
j = 1
do
jprev = j !initialize
j = index(s(j:n),backslash) !look for an escape character
if (j>0) then !an escape character was found
!index in full string of the escape character:
j = j + (jprev-1)
if (j<n) then
!save the bit before the escape character:
if (j>1) then
pre = s( 1 : j-1 )
else
pre = ''
end if
!character after the escape character:
c = s( j+1 : j+1 )
if (any(c == [quotation_mark,backslash,slash, &
to_unicode(['b','f','n','r','t'])])) then
!save the bit after the escape characters:
if (j+2<n) then
post = s(j+2:n)
else
post = ''
end if
select case(c)
case (quotation_mark,backslash,slash)
!use c as is
case (CK_'b')
c = bspace
case (CK_'f')
c = formfeed
case (CK_'n')
c = newline
case (CK_'r')
c = carriage_return
case (CK_'t')
c = horizontal_tab
end select
s = pre//c//post
n = n-1 !backslash character has been
! removed from the string
else if (c == 'u') then !expecting 4 hexadecimal digits after
!the escape character [\uXXXX]
!for now, we are just printing them as is
![not checking to see if it is a valid hex value]
if (j+5<=n) then
j=j+4
else
call throw_exception('Error in json_get_string:'//&
' Invalid hexadecimal sequence'//&
' in string: '//trim(c))
exit
end if
else
!unknown escape character
call throw_exception('Error in json_get_string:'//&
' unknown escape sequence in string "'//&
trim(s)//'" ['//backslash//c//']')
exit
end if
j=j+1 !go to the next character
if (j>=n) exit !finished
else
!an escape character is the last character in
! the string [this may not be valid syntax,
! but just keep it]
exit
end if
else
exit !no more escape characters in the string
end if
end do
if (exception_thrown) then
if (allocated(value)) deallocate(value)
else
value = s
end if
else
call throw_exception('Error in json_get_string:'//&
' me%value not allocated')
end if
case default
call throw_exception('Error in json_get_string:'//&
' Unable to resolve value to characters: '//me%name)
! Note: for the other cases, we could do val to string conversions.
end select
!cleanup:
if (allocated(s)) deallocate(s)
if (allocated(pre)) deallocate(pre)
if (allocated(post)) deallocate(post)
end subroutine json_get_string