Hi,
I am trying to read a JSON file with some configuration options from ROOT (C++). It seems the nlohmann code is already in ROOT but I could not figure out how to use it in my macros/code.
I also tried to go through TPython using the json module in python, but again, I could not figure out how to transport this information back to C++ world.
I know of the TBufferJSON class too, but, again, I could not figure out how to use it.
Any hint or example code would be highly appreciated. Thanks,
As far as I know, TBufferJSON only works to serialize/deserialize C++ objects supported by ROOT. If you have JSON that does not comply to the expected format (configuration data, etc.), I think that you may be able to use something like jsoncpp.
Please search for json on this forum, as there are already posts from other people that can help in solving your particular issue. If you cannot find a solution, do not hesitate to reply back here.
I had a look in the forum before I posted my message and I did not find a good answer.
The funny thing for me is that the nlohmann json code for C++ was included in ROOT at least in some releases (here). But I don’t know if it can be used (and, if so, how) any more.
I don’t understand either how to use the TBufferJSON class myself (even if that involves creating a TObject derived class mimicking the json contents). Of course, given that my idea is to have simple json (actually a dictionary in the python sense) I could write a class and a reader, but I expected someone had already coded a generic reader given JSON is quite common.
Also, I failed with using TPython. I tried the following example:
TBufferJSON can only be used for C++ classes with dictionary.
And produced JSON code has special format, which not always can be mapped to arbitrary JSON file.
But for simple structures it also looks very simple. Here is simple example:
Internally ROOT uses nlohmann/json parser, but it will be made public only in next 6.24 release (current master branch). nlohmann/json parser can parse arbitrary JSON file, but I have my doubts if this highly templated C++ library can be used via PyROOT.
Probably, you can use json parser, available in python.
Hi Sergey,
Thanks for the more detailed answer! It may serve what I want.
Please note that I don’t want to read JSON (a flat one) through python (which is easy). I want to read it from ROOT/C++. I thought of the TPython trick as a (quite awful) hack.
You can also use yaml-cpp. Most of the time yaml and json are interchangeable, we have successfuly used yamlcpp and yaml in python in our analysis and the 2 were interchangable. In practice we have written the parser in c++ and linkdef it for python creating appropriate shared libraries for the module. I suppose one can do the same for c++ code compiled against jsoncpp libraries.