json/dynamic(动态数据类型)处理

#include 
#include 
  • 案例
std::make_unique content_;

Status Configuration::parseFromString(const std::string &content) {
    try {
        auto json = folly::parseJson(content);
        content_ = std::make_unique(std::move(json));
    } catch (std::exception &e) {
        LOG(ERROR) << e.what();
        return Status::Error("Illegal format (%s)", e.what());
    }
    return Status::OK();
}

Status Configuration::forEachKey(std::function processor) const {
    DCHECK(content_ != nullptr);
    for (auto& key : content_->keys()) {
        try {
            processor(key.asString());
        } catch (const std::exception& ex) {
            // Avoid format sercure by literal
            return Status::Error("%s", ex.what());
        }
    }
    return Status::OK();
}


Status Configuration::forEachItem(
        std::function processor) const {
    DCHECK(content_ != nullptr);
    for (auto& item : content_->items()) {
        try {
            processor(item.first.asString(), item.second);
        } catch (const std::exception& ex) {
            // Avoid format sercure by literal
            return Status::Error("%s", ex.what());
        }
    }
    return Status::OK();
}

你可能感兴趣的:(json/dynamic(动态数据类型)处理)