subroutine test_8(error_cnt)
!! read a JSON structure from a string
implicit none
integer,intent(out) :: error_cnt
type(json_value),pointer :: p
character(len=*),parameter :: newline = achar(10)
character(len=*),parameter :: str = '{ "label": "foo",'//newline//' "value": "bar" }'
character(len=*),parameter :: str2 = '{ "label": "foo",'//newline//&
' "value": "bar",'//newline//&
' "empty_array": [],'//newline//&
' "empty_object": {}' //newline//&
'}'
character(len=*),parameter :: str_invalid = '{ "label": "foo",'//newline//' "value : "bar" }'
error_cnt = 0
call json_initialize()
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
write(error_unit,'(A)') ''
write(error_unit,'(A)') '================================='
write(error_unit,'(A)') ' EXAMPLE 8 : read JSON from string'
write(error_unit,'(A)') '================================='
write(error_unit,'(A)') ''
write(error_unit,'(A)') '**************'
write(error_unit,'(A)') ' Valid test 1:'
write(error_unit,'(A)') '**************'
write(error_unit,'(A)') ''
call json_parse(str=str, p=p) ! read it from str
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
write(output_unit,'(A)') '{ "part a" : '
call json_print(p,output_unit) ! print to console
write(output_unit,'(A)') ','
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
call json_destroy(p) ! cleanup
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
write(error_unit,'(A)') ''
write(error_unit,'(A)') '**************'
write(error_unit,'(A)') ' Valid test 2:'
write(error_unit,'(A)') '**************'
write(error_unit,'(A)') ''
call json_parse(str=str2, p=p) ! read it from str
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
write(output_unit,'(A)') '"part b" : '
call json_print(p,output_unit) ! print to console
write(output_unit,'(A)') ','
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
call json_destroy(p) ! cleanup
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
write(error_unit,'(A)') ''
write(error_unit,'(A)') '**************'
write(error_unit,'(A)') ' Invalid test:'
write(error_unit,'(A)') '**************'
write(error_unit,'(A)') ''
call json_parse(str=str_invalid, p=p) ! read it from str
if (json_failed()) then
call json_print_error_message(error_unit)
else
write(error_unit,'(A)') 'This should have failed!'
error_cnt = error_cnt + 1
end if
write(output_unit,'(A)') '"part c" : '
call json_print(p,output_unit) ! print to console
write(output_unit,'(A)') '}'
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
call json_destroy(p) ! cleanup
if (json_failed()) then
call json_print_error_message(error_unit)
error_cnt = error_cnt + 1
end if
write(error_unit,'(A)') ''
end subroutine test_8