fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.09k stars 167 forks source link

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

Open awvwgk opened 2 years ago

awvwgk commented 2 years ago

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)

A possible API ```f90 !> 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. ```f90 !> 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 ```
jvdp1 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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