WARNING This documentation is a work in progress and does not reflect the full potential of the JSON plugin.
The JSON plugin enables users to export data in valid JSON format and allows for custom writing formats (The JSON plugin for custom formating).
This plugin allows writing multiple variables in the same file as soon as they are available.
It supports not only basic data types such as scalar, array, record, tuple, and pointer, but also complex and nested combinations of these types. This includes multi-level arrays, multi-level records, and any mixture of the supported basic types at arbitrary levels of nesting. However, not all combinations are fully tested.
It ensures a JSON valid format at all times, even if the simulation crashes and PDI is not finalized, due to its incremental writing. Warning: this may result in increased disk bandwidth and may not be ideal for a performance-sensitive context.
Conditionnal writing is supported.
The write to file is only triggered when a variable is shared to PDI with read permission given.
Note that is written to this file the variable whichever variables listed in the "write" section when PDI is granted read permission.
The JSON plugin doesn't yet support the function of waiting for multiple variables to be readable before writing them.
There are two syntax for the JSON configuration file: First one is var_name: file_path.json
, as for example :
The second one starts with - file: file_path.json
with arguments below
key | value |
---|---|
"file" (mandatory) | Name of the output file |
"write" (mandatory) | The variables to be writen |
"when" (optional) | Indicate some kind of condition |
For example,
Note : By default, if the key "when" is not specified, a true condition is set so that the variable is written every time PDI is granted read permission. Examples of those two syntax are given in example/json.yml
.
The JSON plugin relies on the nlohmann/json library to output data to JSON format. The json library is included in the PDI repo as a submodule. To get the source code of this external library, one needs to do, from the root of PDI:
Next, users can activate the json_plugin by adding BUILD_JSON_PLUGIN=ON
option to the PDI configuration commande.
Once the plugin installed, the easiest way to test the plugin is referring to the example section, which provides a full somewhat realistic C simulation code, where PDI is enabled and the JSON plugin is used. This one can be found in example
.
To use it, first compile using CMake with BUILD_TESTING
set to ON
, then execute :
Please refer to example/json.yml
For the sake of the example, let's imagine the following code, creating which performs a certain heat transfer simulation.
If we run this code as is, the following JSON file would get generated.
We can use this output json file to generate our custom format. For example, we can rely on mustache to do so.
Finaly, let's print it ! We execute the json to mustache converter ...
... so we can get the following result !