JSON-Fortran 5.0 is out. This release finally brings thread-safety to the library. Note that it does break backward compatibility with previous versions, but hopefully it isn’t too much trouble to modify your code to be compatible with the new release. I’ve provided a short guide describing what you need to do.
JSON-Fortran is a Fortran 2008 JSON API, based on an earlier project called FSON (which was written in Fortran 95). FSON was not thread-safe, and so neither was JSON-Fortran at first. This was mainly due to the use of various global settings, and global variables used during parsing and for keeping track of errors.
In the transition from FSON to JSON-Fortran, I added a high-level
json_file class that is used to do a lot of common operations (e.g. open a JSON file and read data from it). However, building a JSON structure from scratch is done using lower-level
json_value pointers. In the 5.0 release, there is a new factory class called
json_core that is now the interface for manipulating
json_value variables. Thus, each instance of this class can exist independently of any others (each with potentially different settings), and so provides thread-safe operation and error handling. The
json_file class simply contains an instance of
json_core, which contains all the variables and settings that were formerly global to the entire module.
A very simple example of the pre-5.0 usage would be:
program test use json_module implicit none type(json_file) :: json integer :: ival character(len=:),allocatable :: cval logical :: found call json_initialize() call json%load_file(filename='myfile.json') call json%print_file() !print to the console call json%get('var.i',ival,found) call json%get('var.c',cval,found) call json%destroy() end program test
For 5.0, all you have to do is change:
and you’re done. All global variables have been eliminated and the only entities that are user-accessible are three public types and their methods.
There are also a ton of other new features in JSON-Fortran 5.0, including new APIs, such as:
json_core%validate()— test the validity of a JSON structure (i.e., a json_value linked list).
json_core%is_child_of()— test if one json_value is a child of another.
json_core%swap()— swap two json_value elements in a JSON structure (this may be useful for sorting purposes).
json_core%rename()— rename a json_value variable in a JSON structure.
And new settings (set during the call to
initialize()) such as:
- Trailing spaces can now be significant for name comparisons.
- Name comparisons can now be case sensitive or case insensitive.
- Can enable strict type checking to avoid automatic conversion of numeric data (say, integer to double) when getting data from a JSON structure.
- Can set the number of spaces for indenting when writing JSON data to a file.