jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
333 stars 82 forks source link

Check if keyword is valid #534

Closed jphaupt closed 3 months ago

jphaupt commented 1 year ago

We are using json-fortran as an input file reader for one of our libraries. Perhaps this is outside the scope of your project, but is it possible to check if a keyword is invalid/unrecognised, or would this have to be done separately? E.g. check if data present in the json was not read by the program.

I guess this would involve reading each keyword present in the json and comparing it to all possible keywords (which would be done outside json-fortran, or given as input), and failing if not.

I just want some error checking, e.g. say x0 has a default value of 0 if it is not found, but the user erroneously inputs "z0": 5. At the moment, the code would simply ignore z0 (and set x0=0 which is not what the user actually wanted), but I would like it to stop.

jacobwilliams commented 1 year ago

There are various ways to check if a variable is in the file. The json_file type has a .in operator, so you can do stuff like this:

if (.not. ('x0' .in. f)) error stop 'x0 not found'

Some of the other routines have a found optional argument, so you could check if a variable was found, etc.

To check if a variable is there but not recognized, I think you'd just have to iterate over all the keys and compare them to the list and flag the ones not recognized.