示例#1
0
 bool option_parser::find(const json::object_t& set, const std::string& key, const json*& sub) const {
     auto f = set.find(key);
     if (f != set.end()) {
         sub = &f->second;
         return true;
     }
     return false;
 }
示例#2
0
    void option_target::assign_default(const json::object_t& schema) {
        auto& o = this->result->as_object();
        if (o.find(this->name) != o.end()) return;

        auto f = schema.find("default");
        if (f != schema.end()) {
            o.emplace(std::make_pair(this->name, f->second));
            this->assigned = true;
        }
        else if (this->container != json::content_type::null) {
            o.emplace(std::make_pair(this->name, this->container));
        }
        else if (this->element != json::content_type::boolean) {
            o.emplace(std::make_pair(this->name, this->element));
        }
        else {
            o.emplace(std::make_pair(this->name, true));
        }
    }
type GetElement(json::object_t root, char *path, type pDefault)
{
    char *p = path;
    char token[128];
    while (true)
    {
        for (; *p != '/' && *p != 0 && *p != '['; p++);
        memcpy(token, path, p - path);
        token[p - path] = 0;

        printf(" %s\n", token);
        json::object_t::iterator it = root.find(token);
        if (it == root.end())
            return pDefault;

        if (*p == '[')
        {
            p++;
            int i = atoi(p);
            for (; *p != 0 && *p != ']'; p++);
            root = it->second.at(i).get<json::object_t>();
            p++;
        }
        else
        {
            if (it->second.is_object())
                root = it->second.get<json::object_t>();
            else
            {
                return it->second.get<type>();
            }
        }
        p++;
        path = p;
    }

    return pDefault;
}