In my previous post on reading XML input files, I discussed how input files can be made more human friendly with XML markup. In some situations, it may be preferable to have JSON format files instead.
The JSON input file
In JSON format, our Boundary input file can be expressed as:
Recall that the equivalent XML input file has the form
I prefer the XML version, but most new students will be familiar with JSON and may prefer that format, particularly as that format appears to be preferred in IoT applications.
A large list of JSON readers for C++ can be found in the JSON webpage. A reasonably good header-only library is JSON for Modern C++. I have chosen to use that library because its constructs are similar to those of ZenXml.
We add a new method, readJSON, to the class BoundaryReader and implement it as follows.
I’m not really a fan of try-catch blocks, but that’s the easiest way of dealing cleanly with read/parse failures when using JSON for Modern C++. For research codes, this approach will suffice. An alternative is to use a check of the form
Sometimes input/output files for engineering simulations can be quite large. In those situations I would suggest XML with binary data. In future post I’ll show you how to create a VTK XML format output file with binary data.
If you have questions/comments/corrections, please contact banerjee at parresianz dot com dot json (without the dot json).