【问题标题】:C++ Boost program options with json file带有 json 文件的 C++ Boost 程序选项
【发布时间】:2017-05-29 09:57:47
【问题描述】:

可以使用 boost 程序选项库:http://www.boost.org/doc/libs/1_64_0/doc/html/program_options.html

在这里读取json格式的文件作为输入文件?

或者如果我在 json 之类的文件中有一些配置,我需要自己解析它,例如:http://www.boost.org/doc/libs/1_64_0/doc/html/property_tree.html

【问题讨论】:

    标签: c++ json boost boost-program-options


    【解决方案1】:

    我遇到了同样的问题。这是我基于 property_tree 对 program_options 库的 JSON 解析器的实现:

    template <class charT> void parseChildren(std::string prefix, boost::property_tree::ptree& tree, boost::program_options::parsed_options& options)
    {
        if (tree.size() == 0)
        {
            //cut first dot
            std::basic_string<charT> name = prefix.substr(1);
            std::basic_string<charT> value = tree.data();
    
            boost::program_options::basic_option<charT> opt;
            opt.string_key = name;
            opt.value.push_back(value);
            opt.unregistered = (options.description->find_nothrow(name, false) == nullptr);
            opt.position_key = -1;
            options.options.push_back(opt);
        }
        else
        {
            for (auto it = tree.begin(); it != tree.end(); ++it)
            {
                parseChildren<charT>(prefix + "." + it->first, it->second, options);
            }
        }
    }
    
    template <class charT>
    void parseJsonOptions(std::basic_istream<charT>& is, boost::program_options::parsed_options& options)
    {
            boost::property_tree::basic_ptree<std::basic_string<charT>, std::basic_string<charT>> pt;
            boost::property_tree::read_json(is, pt);
    
            parseChildren<charT>(std::basic_string<charT>(), pt, options);
    }
    
    template <class charT>
    boost::program_options::basic_parsed_options<charT> parse_json_config_file(std::basic_istream<charT>& is, const boost::program_options::options_description& desc, bool allow_unregistered = false)
    {     
        // do any option check here
    
        boost::program_options::parsed_options result(&desc);
        parseJsonOptions(is, result);
    
        return boost::program_options::basic_parsed_options<charT>(result);
    }
    

    像这样使用它:

    po::variables_map vm;
    std::ifstream configFileStream(configFilePath_.generic_string());
    
    store(parse_json_config_file(configFileStream, algorithmsDesc_), vm);
    notify(vm);
    

    【讨论】:

    • 干得好。 +1 用于实际提出实现。这可能会在他们的文档中作为示例
    【解决方案2】:

    使用 boost 程序选项库是可能的: http://www.boost.org/doc/libs/1_64_0/doc/html/program_options.html

    在这里读取json格式的文件作为输入文件?

    不,但你可以写一个Parser Component 给它

    或者如果我在 json 文件中有一些配置,我需要自己解析它, 例如: http://www.boost.org/doc/libs/1_64_0/doc/html/property_tree.html

    你可以。请务必查看limitations。另外,请注意“json like”之类的东西。任何非标准 JSON 都可能会破坏解析器,因此如果它不是标准的,您可能需要手动处理它。

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 2013-11-17
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多