简体   繁体   English

c++ boost json ptree中的自定义get方法

[英]Custom get method in c++ boost json ptree

I'm trying to access missing values in boost json ptree.我正在尝试访问 boost json ptree 中的缺失值。 When the key was not matched, a method should be called, in which I can define the returned value.当键不匹配时,应该调用一个方法,我可以在其中定义返回值。 For calculation of returned value I need to access the ptree.为了计算返回值,我需要访问 ptree。 Example: the ptree is: { "1": 10, "3": 30 } I would like to interpolate the returned value, ie when I query for "2" i want 20 to be returned.示例: ptree 是: { "1": 10, "3": 30 }我想插入返回值,即当我查询"2"时,我希望返回20 Is it possible?可能吗?

Everything is possible in a Turing-complete programming language (hint: that's all of them).在图灵完备的编程语言中一切皆有可能(提示:仅此而已)。

On actual property trees you could use get_optional and then value_or_eval :在实际的属性树上,您可以使用get_optional然后value_or_eval

Live On Wandbox / Compiler Explorer Live On Wandbox /编译器资源管理器

#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/property_tree/json_parser.hpp>
#include <iostream>

int main() {
    boost::property_tree::ptree pt;
    pt.add("1", 10);
    pt.add("3", 30);

    for (auto probe : { "1", "2", "3" }) {
        auto lookup = pt.get_optional<double>(probe).value_or_eval([&pt] {
            write_json(std::cout << "(interpolate from ", pt);
            std::cout << ")\n";
            return 42;
        });

        std::cout << "probe " << probe << " -> " << lookup << "\n";
    }
}

Printing印刷

probe 1 -> 10
(interpolate from {
    "1": "10",
    "3": "30"
}
)
probe 2 -> 42
probe 3 -> 30

The problem with this is that it's neither JSON nor strongly typed.问题在于它既不是 JSON 也不是强类型。

Alternatives备择方案

Here's a random take (literally) and it uses a Json library as opposed to Property Tree:这是一个随机的(字面意思),它使用 Json 库而不是属性树:

Live On Compiler Explorer Live On 编译器资源管理器

#include <boost/json.hpp>
#include <boost/json/src.hpp> // header-only
#include <boost/lexical_cast.hpp>
#include <map>
#include <iostream>
#include <iomanip>
#include <random>
using boost::conversion::try_lexical_convert;
namespace json = boost::json;

template <typename Key = double, typename Value = double>
struct MyLookup {
    MyLookup(std::string const& text) : data(parse(text)) 
    { }

    using Table = std::map<Key, Value>;
    static Table parse(std::string const& text) {
        auto raw = json::value_to<std::map<std::string, Value>>(
            json::parse(text));

        Table converted;
        auto key_convert = [](auto &raw_pair) -> std::pair<Key, Value> {
            return {boost::lexical_cast<Key>(raw_pair.first),
                    raw_pair.second};
        };
        std::transform(raw.begin(), raw.end(), inserter(converted, converted.end()), key_convert);
        return converted;
    }

    Value operator[](Key key) const {
        auto [low,up] = data.equal_range(key);
        if (low == data.end())
            throw std::range_error("out of bounds");

        if (up == std::next(low)) {
            return low->second;
        } else {
            if (low == data.begin() || up == data.end())
                throw std::range_error("out of bounds");

            low = std::prev(low);
            auto dx    = up->first  - low->first;
            auto dy    = up->second - low->second;
            auto slope = dy/dx;

            return low->second + (key - (low->first)) * slope;
        }
    }

    auto begin() const { return data.begin(); }
    auto end()   const { return data.end();   }

private:
    Table data;
};

int main() {
    MyLookup data(R"({ "1": 10, "3": 30 })");

    for (auto [k,v] : data) {
        std::cout << "table: " << k << " -> " << v << "\n";
    }

    std::mt19937 prng;
    std::uniform_real_distribution<double> probe(0,5);
    for (int i = 10; --i;) {
        auto key = probe(prng);
        std::cout << "Linear interpolation: " << std::setw(7) << key << " -> ";
        try {
            std::cout << data[key] << '\n';
        } catch (std::exception const &e) {
            std::cout << e.what() << '\n';
        }
    }
}

Prints eg打印例如

table: 1 -> 10
table: 3 -> 30
Linear interpolation: 0.677385 -> out of bounds
Linear interpolation: 4.17504 -> out of bounds
Linear interpolation: 4.84434 -> out of bounds
Linear interpolation: 1.10517 -> 11.0517
Linear interpolation: 1.54084 -> 15.4084
Linear interpolation:  2.7361 -> 27.361
Linear interpolation: 0.94191 -> out of bounds
Linear interpolation: 4.96441 -> out of bounds
Linear interpolation: 4.98231 -> out of bounds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM