Ответ 1
Вы можете специализировать boost::property_tree::translator_between
, чтобы дерево свойств использовало собственный переводчик для типа значения bool
. Эта специализация должна быть видимой (т.е. #includ
ed) клиентами, желающими настроить поведение. Вот рабочий пример:
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string/predicate.hpp>
// Custom translator for bool (only supports std::string)
struct BoolTranslator
{
typedef std::string internal_type;
typedef bool external_type;
// Converts a string to bool
boost::optional<external_type> get_value(const internal_type& str)
{
if (!str.empty())
{
using boost::algorithm::iequals;
if (iequals(str, "true") || iequals(str, "yes") || str == "1")
return boost::optional<external_type>(true);
else
return boost::optional<external_type>(false);
}
else
return boost::optional<external_type>(boost::none);
}
// Converts a bool to string
boost::optional<internal_type> put_value(const external_type& b)
{
return boost::optional<internal_type>(b ? "true" : "false");
}
};
/* Specialize translator_between so that it uses our custom translator for
bool value types. Specialization must be in boost::property_tree
namespace. */
namespace boost {
namespace property_tree {
template<typename Ch, typename Traits, typename Alloc>
struct translator_between<std::basic_string< Ch, Traits, Alloc >, bool>
{
typedef BoolTranslator type;
};
} // namespace property_tree
} // namespace boost
int main()
{
boost::property_tree::iptree pt;
read_json("test.json", pt);
int i = pt.get<int>("number");
int b = pt.get<bool>("enabled");
std::cout << "i=" << i << " b=" << b << "\n";
}
test.json:
{
"number" : 42,
"enabled" : "Yes"
}
Вывод:
i=42 b=1
Обратите внимание, что в этом примере предполагается, что дерево свойств нечувствительно к регистру и использует std::string
. Если вы хотите, чтобы BoolTranslator
был более общим, вам нужно сделать шаблон BoolTranslator
и предоставить специализации для широких строк и сравнения с регистром.