RDKit: JSONHelpers.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
JSONHelpers.h
Go to the documentation of this file.
1#include <RDGeneral/BoostStartInclude.h>
2#include <boost/property_tree/ptree.hpp>
3#include <RDGeneral/BoostEndInclude.h>
4#include <array>
5
6// Convert a JSON object with boolean keys to a bitwise enum flag set.
7// The T enum must have been declared as a BETTER_ENUM.
8// The JSON object should have keys corresponding to the enum names, and boolean
9// values. Example JSON: { "FlagA": true, "FlagB": false, "FlagC": true,
10// "FlagD": false } This would set FlagA and FlagC in the resulting flag set,
11// then clear FlagB and FlagD. If keysFound is provided and non-null, it will be
12// set to true if any of the enum keys were found in the JSON object, false
13// otherwise.
14template <typename T>
15 typename T::_integral flagsFromJson(const boost::property_tree::ptree &pt,
16 bool *keysFound = nullptr) {
17 std::array<typename T::_integral, 2> flagsByType;
18 flagsByType.fill(static_cast<T::_integral>(0));
19 if (keysFound) {
20 *keysFound = false;
21 }
22 for (const auto *key : T::_names()) {
23 const auto it = pt.find(key);
24 if (it == pt.not_found()) {
25 continue;
26 }
27 if (keysFound) {
28 *keysFound = true;
29 }
30 auto value = T::_from_string(key)._to_integral();
31 auto i = static_cast<unsigned int>(it->second.template get_value<bool>());
32 flagsByType[i] |= value;
33 }
34 return (flagsByType[1] & ~flagsByType[0]);
35}
BoostEndInclude.h
BoostStartInclude.h
flagsFromJson
T::_integral flagsFromJson(const boost::property_tree::ptree &pt, bool *keysFound=nullptr)
Definition JSONHelpers.h:15