|
json2xml master
C++ header-only json to xml converter
|
C++ header-only converter from Json to XML, based in also header-only nlohmann json library.
The json layout is the one of python xmltodict, restricted to elements and attributes:
@ (you can change it at JsonSaxConsumer constructor) are the attributes, with string, number or boolean values (escaped as needed); the other keys are the child elements. Attributes are written first, wherever they are in the object.null for empty elements).null value, or an empty object, is an empty element (<a/>).Not supported (the conversion fails):
{"a": "text"}), an array of scalars, or the #text key used by xmltodict.null values or repeated, invalid XML names and characters not allowed in XML 1.0.In summary, this converter is intended to be used for simple xml configurations with predictable and controlled content:
turns into:
json2xml.hpp is the single required file in include/ert or released here. You need to add
You must also integrate nlohmann/json header-only file (json.hpp) in your project.
On failure, the xml is empty (before 1.1.0 it held the json parse error, and unsupported content gave a wrong xml). A consumer can be reused: every parse starts from scratch.
The full API is documented here.
To embed the library directly into an existing CMake project, place the entire source tree in a subdirectory and call add_subdirectory() in your CMakeLists.txt file:
Since CMake v3.11, FetchContent can be used to automatically download the repository as a dependency at configure type.
Example:
CMake 3.14 or newer is required (dependencies are fetched at configure time: nlohmann/json and, for the unit tests, googletest):
Built by default (option ERT_JSON2XML_BuildTests):
A Debug build is instrumented for coverage (lcov is needed for the report):
The CI (GitHub Actions) publishes the coverage report and the doxygen documentation (local generation: cd docs/doxygen && doxygen) on GitHub Pages.
The example converter is checked with pytest: the xml is parsed back with python xmltodict and compared to the json, and the unsupported cases must fail. On ubuntu you will need pytest & xmltodict modules:
Then, you shall change directory to ct and just execute pytest (the converter is examples/json2xml, or the one given by JSON2XML_BIN):
Don't forget to build the project first !
A converter example based in the library header is located at examples directory:
There are also some test files (examples/test*.json) to play with:
This will output the xml representation, or an error (on standard error, with exit code 1).
You could do the opposite (xml to json) using python xmltodict module:
You can also use this beautify perl script for json output, courtesy of yanOnGithub:
You may remove cmake cache from native workflow described above:
Or perhaps you could execute:
Now, you will build the compilation docker image, and then build:
Please, execute astyle formatting (using frankwolf image) before any pull request: