WARNING This documentation is a work in progress and does not reflect the full potential of the JSON plugin.
The JSON plugin exports data in valid JSON, which can then be turned into custom formats (The JSON plugin for custom formatting).
It can write several variables to the same file, each as soon as it is available.
It supports not only the basic datatypes (scalars, arrays, records, tuples and pointers), but also their nested combinations: multi-level arrays, multi-level records, and any mixture of the supported types at any level of nesting. Not all combinations are fully tested, however.
Thanks to its incremental writing, the file is valid JSON at all times, even if the simulation crashes before PDI is finalized. Warning: this increases the disk bandwidth used, and may not suit a performance-sensitive context.
Conditionnal writing is supported.
A variable is only written when it is shared with PDI with read access.
Each variable listed in the write section is written to the file whenever PDI is granted read access to it.
The JSON plugin cannot yet wait for several variables to be readable before writing them together.
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 is installed, the easiest way to test it is the example of example/, a full, somewhat realistic C simulation code that uses PDI with the JSON plugin.
To use it, first build PDI with CMake, with BUILD_TESTING set to ON, then run:
See example/json.yml.
For the sake of the example, let's imagine the following code, which runs a 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.
Finally, let's print it! We run the JSON to mustache converter...
... and get the following result!