Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 3 tasks
awvwgk opened this issue Jul 30, 2022 · 3 comments
Open
1 of 3 tasks

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

awvwgk opened this issue Jul 30, 2022 · 3 comments
Labels
topic: IO Common input/output related features

Comments

@awvwgk
Copy link
Member

awvwgk commented Jul 30, 2022

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
@awvwgk awvwgk added the topic: IO Common input/output related features label Jul 30, 2022
@jvdp1
Copy link
Member

jvdp1 commented Aug 1, 2022

I think it is a great idea. Another file format mentioned in your list (maybe on purpose) is the CSV format.
Anyway, would this result in stdlib depending on other separate projects to support the different file formats? And so, avoid to rewrite everything from scrath?

@awvwgk
Copy link
Member Author

awvwgk commented Aug 1, 2022

Anyway, would this result in stdlib depending on other separate projects to support the different file formats? And so, avoid to rewrite everything from scrath?

Maintaining an own parser implementation for every format can be quite demanding, especially with respect to validation for the implementation, for small formats this might be possible to do within stdlib.

Instead of depending on other libraries it might be possible to just define the interface another library would have to implement. A user would still need to manually depend on the respective project implementing the stdlib interface:

[dependencies]
stdlib.git = "https://github.com/fortran-lang/stdlib.git"
stdlib.branch = "stdlib-fpm"
stdlib-toml.git = "https://github.com/toml-f/stdlib-toml.git"  # for stdlib_toml module

@jvdp1
Copy link
Member

jvdp1 commented Aug 4, 2022

I like this strategy. I guess that a similar strategy could be used for other types of procedures/algorithms.

@awvwgk awvwgk mentioned this issue Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: IO Common input/output related features
Projects
None yet
Development

No branches or pull requests

2 participants