Json in Fortran
Our first program will demonstrate JSON encoding and decoding in Fortran. Here’s the full source code:
program json_example
use json_module
use iso_fortran_env, only: error_unit
implicit none
type :: response1
integer :: page
character(len=:), allocatable :: fruits(:)
end type response1
type :: response2
integer :: page
character(len=:), allocatable :: fruits(:)
end type response2
type(json_core) :: json
type(json_value), pointer :: p, p_tmp
type(response1) :: res1
type(response2) :: res2
character(len=:), allocatable :: json_string
integer :: i
! Encoding basic data types to JSON strings
call json%initialize()
call json%create_object(p, '')
call json%add(p, 'boolean', .true.)
call json%add(p, 'integer', 1)
call json%add(p, 'real', 2.34)
call json%add(p, 'string', 'gopher')
call json%print(p, json_string)
print *, trim(json_string)
call json%destroy(p)
! Encoding arrays and objects
call json%create_object(p, '')
call json%create_array(p_tmp, 'fruits')
call json%add(p_tmp, '', 'apple')
call json%add(p_tmp, '', 'peach')
call json%add(p_tmp, '', 'pear')
call json%add(p, p_tmp)
call json%print(p, json_string)
print *, trim(json_string)
call json%destroy(p)
! Encoding custom data types
res1%page = 1
allocate(res1%fruits(3))
res1%fruits = ['apple', 'peach', 'pear']
call json%create_object(p, '')
call json%add(p, 'Page', res1%page)
call json%create_array(p_tmp, 'Fruits')
do i = 1, size(res1%fruits)
call json%add(p_tmp, '', res1%fruits(i))
end do
call json%add(p, p_tmp)
call json%print(p, json_string)
print *, trim(json_string)
call json%destroy(p)
! Decoding JSON data
json_string = '{"num":6.13,"strs":["a","b"]}'
call json%initialize()
call json%parse(json_string, p)
call json%get(p, 'num', res2%page) ! Note: Fortran doesn't have a direct equivalent to Go's interface{}
print *, 'num:', res2%page
call json%get(p, 'strs[1]', json_string)
print *, 'First string:', trim(json_string)
call json%destroy(p)
call json%finalize()
end program json_example
This Fortran program demonstrates basic JSON encoding and decoding operations using the json_module
. Here’s a breakdown of what the program does:
We start by encoding basic data types (boolean, integer, real, and string) to JSON strings.
We then encode an array of strings as a JSON array.
We create a custom type
response1
and encode it to JSON.Finally, we demonstrate decoding a JSON string into Fortran variables.
To run this program, you would typically compile it with a Fortran compiler that supports the json_module
. For example:
$ gfortran -I/path/to/json_module json_example.f90 -o json_example
$ ./json_example
Note that Fortran doesn’t have built-in JSON support like Go does. We’re using an external module (json_module
) to handle JSON operations. The exact syntax and available operations may vary depending on the specific JSON library you’re using.
Also, Fortran doesn’t have a direct equivalent to Go’s interface{}
type, so type safety is generally stricter. When decoding JSON, you typically need to know the expected types in advance.
This example provides a basic introduction to JSON handling in Fortran. For more advanced usage, you should refer to the documentation of the specific JSON library you’re using.