Change how boost::property_tree reads translates strings to bool

混江龙づ霸主 提交于 2019-11-29 02:35:42

问题


I've gotten lost in the header files for the boost property_tree and given the lack of documentation around the lower layers, I've decided to ask what the easy way is to over-ride the stream translator to change how Boolean values are parsed.

The problem is that on the input side of a property tree, there are users, and they can modify the configuration files. A Boolean value might be specified in a number of ways, like:

dosomething.enabled=true
dosomething.enabled=trUE
dosomething.enabled=yes
dosomething.enabled=ON
dosomething.enabled=1

The default behaviour is to check for 0 or 1 and then use

std::ios_base::boolalpha 

to get the stream to try to parse the value in the appropriate manner for the current locale...which could be insane if we try to send a configuration file to international customers.

So what's the easiest way to override this behaviour or bool only? Not only easiest to implement, but easiest to use - so that the users of my class which derives from iptree don't need to do something special for Boolean values.

Thanks!


回答1:


You can specialize boost::property_tree::translator_between so that a property tree will use a custom translator for a bool value type. This specialization must be visible (i.e. #included) by clients wanting the customized behavior. Here's a working example:

#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"
}

Output:

i=42 b=1

Please note that this example assumes that the property tree is case insensitive and uses std::string. If you want BoolTranslator to be more general, you'll have to make BoolTranslator a template and provide specializations for wide strings and case sensitive comparisons.




回答2:


There is also a good example at theboostcpplibraries.com.

Based on that, I wrote for a custom parser (declaration omitted):

boost::optional<bool> string_to_bool_translator::get_value(const std::string &s) {
    auto tmp = boost::to_lower_copy(s);
    if (tmp == "true" || tmp == "1" || tmp == "y" || tmp == "on") {
       return boost::make_optional(true);
    } else if (tmp == "false" || tmp == "0" || tmp == "n" || tmp == "off") {
      return boost::make_optional(false);
    } else {
        return boost::none;
    }
} 

It's only for bool and std::string but easily extendable.

Then,

boost::property_tree::ptree pt;
...
string_to_bool_translator tr;
auto optional_value = pt.get_optional<bool>(key, tr);


来源:https://stackoverflow.com/questions/9745716/change-how-boostproperty-tree-reads-translates-strings-to-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!