Skip to content

Commit 37a832c

Browse files
committed
Method to extract ConfigurableParam as JSON string
1 parent 16b6488 commit 37a832c

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

Common/Utils/include/CommonUtils/ConfigurableParam.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ class ConfigurableParam
407407
// writes a human readable INI or JSON file depending on the extension
408408
static void write(std::string const& filename, std::string const& keyOnly = "");
409409

410+
static std::string asJSON(std::string const& keyOnly = "");
411+
410412
// can be used instead of using API on concrete child classes
411413
template <typename T>
412414
static T getValueAs(std::string key)

Common/Utils/src/ConfigurableParam.cxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,29 @@ void ConfigurableParam::writeJSON(std::string const& filename, std::string const
754754

755755
// ------------------------------------------------------------------
756756

757+
std::string ConfigurableParam::asJSON(std::string const& keyOnly)
758+
{
759+
initPropertyTree(); // update the boost tree before writing
760+
std::ostringstream os;
761+
if (!keyOnly.empty()) { // write ini for selected key only
762+
try {
763+
boost::property_tree::ptree kTree;
764+
auto keys = o2::utils::Str::tokenize(keyOnly, " ,;", true, true);
765+
for (const auto& k : keys) {
766+
kTree.add_child(k, sPtree->get_child(k));
767+
}
768+
boost::property_tree::write_json(os, kTree);
769+
} catch (const boost::property_tree::ptree_bad_path& err) {
770+
LOG(fatal) << "non-existing key " << keyOnly << " provided to writeJSON";
771+
}
772+
} else {
773+
boost::property_tree::write_json(os, *sPtree);
774+
}
775+
return os.str();
776+
}
777+
778+
// ------------------------------------------------------------------
779+
757780
void ConfigurableParam::initPropertyTree()
758781
{
759782
sPtree->clear();

0 commit comments

Comments
 (0)