Skip to content

TOML / JSON / YAML / ... support for stdlib #671

Open
@awvwgk

Description

@awvwgk

It would be useful to have a stdlib module for handling common file format for data storage. If we can provide an interface in stdlib for this kind of operations we would make it much easier to integrate a new file format in a library which is already using stdlib.

This can be implemented in stdlib by pulling in common libraries used for this task, like JSON Fortran and TOML Fortran. Alternative, the interface could be declared in the specification and libraries like JSON Fortran and TOML Fortran can provide an implementation of stdlib_json or stdlib_toml, respectively.

Requirements (feel free to add to this list)

  • map data structure
  • list data structure
  • date time derived type (for TOML)
A possible API
!> Standard library support for reading TOML documents
module stdlib_toml
  interface toml_load
    !> Load TOML data structure from file
    module subroutine toml_load_from_file(filename, value, stat, message)
      !> File name to read from
      character(*), intent(in) :: filename
      !> Data structure, either a built-in type a [[stdlib_value::value_type]]
      class(*), allocatable, intent(out) :: value
      !> Error code, if not provided routine will halt in case of error
      integer, intent(out), optional :: stat
      !> Error message
      character(:), allocatable, intent(out), optional :: message
    end subroutine toml_load_from_file
  end interface toml_load

  interface toml_loads
    module subroutine toml_load_from_string(string, value, stat, message)
      !> File name to read from
      character(*), intent(in) :: string
      !> Data structure, either a built-in type or a [[stdlib_value::value_type]]
      class(*), allocatable, intent(out) :: value
      !> Error code, if not provided routine will halt in case of error
      integer, intent(out), optional :: stat
      !> Error message
      character(:), allocatable, intent(out), optional :: message
    end subroutine toml_load_from_string
  end interface toml_loads
end module stdlib_toml

A similar interface would be provided for reading JSON to allow a consistent interface for all common IO formats.

!> Standard library support for reading JSON documents
module stdlib_json
  interface json_load
    !> Load JSON data structure from file
    module subroutine json_load_from_file(filename, value, stat, message)
      !> File name to read from
      character(*), intent(in) :: filename
      !> Data structure, either a built-in type a [[stdlib_value::value_type]]
      class(*), allocatable, intent(out) :: value
      !> Error code, if not provided routine will halt in case of error
      integer, intent(out), optional :: stat
      !> Error message
      character(:), allocatable, intent(out), optional :: message
    end subroutine json_load_from_file
  end interface json_load

  interface json_load
    !> Load JSON data structure from string
    module subroutine json_loads_from_string(string, value, stat, message)
      !> File name to read from
      character(*), intent(in) :: string
      !> Data structure, either a built-in type or a [[stdlib_value::value_type]]
      class(*), allocatable, intent(out) :: value
      !> Error code, if not provided routine will halt in case of error
      integer, intent(out), optional :: stat
      !> Error message
      character(:), allocatable, intent(out), optional :: message
    end subroutine json_loads
  end interface json_loads_from_string
end module stdlib_json

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: IOCommon input/output related features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions