Json in Co-array Fortran

program json_example
  use iso_fortran_env, only: real64
  use json_module
  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
  real(real64) :: num
  character(len=:), allocatable :: str1

  ! Encoding basic data types to JSON strings
  call json%initialize()
  call json%create_object(p, '')

  call json%add(p, 'bool', .true.)
  call json%add(p, 'int', 1)
  call json%add(p, 'float', 2.34_real64)
  call json%add(p, 'string', 'gopher')

  call json%print(p)
  call json%destroy(p)

  ! Encoding arrays and objects
  call json%create_object(p, '')
  call json%add(p, 'fruits', ['apple', 'peach', 'pear'])
  call json%print(p)
  call json%destroy(p)

  call json%create_object(p, '')
  call json%add(p, 'apple', 5)
  call json%add(p, 'lettuce', 7)
  call json%print(p)
  call json%destroy(p)

  ! Encoding custom data types
  res1%page = 1
  res1%fruits = ['apple', 'peach', 'pear']
  call json%create_object(p, '')
  call json%add(p, 'Page', res1%page)
  call json%add(p, 'Fruits', res1%fruits)
  call json%print(p)
  call json%destroy(p)

  res2%page = 1
  res2%fruits = ['apple', 'peach', 'pear']
  call json%create_object(p, '')
  call json%add(p, 'page', res2%page)
  call json%add(p, 'fruits', res2%fruits)
  call json%print(p)
  call json%destroy(p)

  ! Decoding JSON data
  json_string = '{"num":6.13,"strs":["a","b"]}'
  call json%parse(p, json_string)
  call json%get(p, 'num', num)
  print *, 'num:', num

  call json%get(p, 'strs[1]', str1)
  print *, 'First string:', str1

  call json%destroy(p)

  ! Decoding into custom data types
  json_string = '{"page": 1, "fruits": ["apple", "peach"]}'
  call json%parse(p, json_string)
  call json%get(p, 'page', res2%page)
  call json%get(p, 'fruits', res2%fruits)
  print *, 'Page:', res2%page
  print *, 'First fruit:', res2%fruits(1)

  call json%destroy(p)
  call json%finalize()
end program json_example

This Co-array Fortran example demonstrates JSON encoding and decoding using a hypothetical json_module. The program showcases:

  1. Encoding basic data types (boolean, integer, float, string) to JSON.
  2. Encoding arrays and objects to JSON.
  3. Encoding custom data types (structs) to JSON.
  4. Decoding JSON data into Fortran variables and custom types.

Note that Co-array Fortran doesn’t have built-in JSON support, so we’re using a hypothetical json_module that provides similar functionality to Go’s encoding/json package. In practice, you would need to use or implement a third-party JSON library for Fortran.

The program structure follows the Go example, adapting concepts where necessary. For instance, Fortran uses derived types instead of structs, and the JSON manipulation is done through subroutine calls rather than method calls.

To run this program, you would need to compile it with a Co-array Fortran compiler that includes the hypothetical json_module. The exact compilation and execution commands would depend on your specific Fortran environment and the JSON library you’re using.

This example provides a starting point for working with JSON in Co-array Fortran, but keep in mind that the actual implementation details may vary depending on the specific JSON library you choose to use.