bool etcd_packer::parse_object(rapidjson::Document &doc, const char *data) { try { doc.Parse(data); return doc.IsObject(); } catch (...) { return false; } }
bool CFileBuilder::Do(ISceneImpl* const& rpScene, const rapidjson::Value& roConfig, rapidjson::Document& rDoc) const { const rapidjson::Value& jvalue = roConfig["value"]; assert(jvalue.IsString()); if (!jvalue.IsString()) return false; std::string file_context; rpScene->GetAssetPtr()->LoadFile(std::string(rpScene->GetDataPath()) + "/" + jvalue.GetString(), file_context); rDoc.Parse(file_context.c_str()); return true; }
bool JsonUtils::ParseJson(rapidjson::Document& doc, fastcgi::DataBuffer& buffer) { std::string json; buffer.toString(json); rapidjson::ParseResult ok = doc.Parse(json.c_str()); if (ok.IsError()) printf( "JSON parse error: %s (%lu)\n", rapidjson::GetParseError_En(ok.Code()), ok.Offset()); return !ok.IsError(); }
void MapInfo::load(const std::string& json_file) { std::ifstream file(json_file); if (!file) { logger << Log::WARN << "Did not find minimap: " << json_file << Log::ENDL; map_json.Parse("{}"); // disable map entirely this->tex = nullptr; this->current_map = ""; return; } const std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); const auto& res = map_json.Parse(str.c_str()); DM_ASSERT(map_json.IsObject(), "Could not parse map JSON file properly!"); if (res.HasParseError()) { DM_ASSERT(false, "Could not parse map JSON file properly!"); } if (map_json.HasMember("filename")) { this->set_map(map_json["filename"].GetString()); } this->mask.enabled = map_json.HasMember("mask"); if (this->mask.enabled) { auto mask_json = map_json["mask"].GetObject(); if (mask_json.HasMember("active")) { this->mask.enabled = mask_json["active"].GetBool(); } if (this->mask.enabled) { this->mask.radius = mask_json["radius"].GetInt(); this->mask.border = mask_json["border"].GetInt(); if (this->map_changed) { // create new mask this->mask.reset(tex->getWidth(), tex->getHeight()); } } } // play area is used to restrict the player icon // the player location is transformed from the world into this area if (map_json.HasMember("play_area")) { auto area = map_json["play_area"].GetArray(); this->play_area = glm::ivec4(area[0].GetInt(), area[1].GetInt(), area[2].GetInt(), area[3].GetInt()); } else { // use whole image if play area is not specified this->play_area = glm::ivec4(0, 0, tex->getWidth(), tex->getHeight()); } this->gridmode = map_json.HasMember("grid"); if (this->gridmode) { auto& grid = map_json["grid"]; this->grid_base_coord = glm::ivec2(grid["base_x"].GetInt(), grid["base_y"].GetInt()); this->grid_dimensions = glm::ivec2(grid["size_x"].GetInt(), grid["size_y"].GetInt()); // grid to pixel multiplier const int w = this->grid_pixel_multiplier().x; const int h = this->grid_pixel_multiplier().y; logger << Log::INFO << "Map screen is a grid of " << w << "x" << h << " pixels" << Log::ENDL; } else { const int w = this->tex->getWidth(); const int h = this->tex->getHeight(); // not a grid logger << Log::INFO << "Map screen is a bitmap of size " << w << "x" << h << " pixels" << Log::ENDL; } this->icons.clear(); if (map_json.HasMember("icons") == false) return; auto& icon_json = map_json["icons"]; const std::string icon_file = icon_json["filename"].GetString(); this->sprite_texid = textures.add(icon_file); for (auto& m : icon_json.GetObject()) { if (std::string("icon") == m.name.GetString()) { const auto icon = m.value.GetObject(); int px = icon["x"].GetInt(); int py = icon["y"].GetInt(); assert(icon.HasMember("sprite") && "Icons must have a sprite object"); const auto sprite = icon["sprite"].GetObject(); short x = sprite["x"].GetInt(); short y = sprite["y"].GetInt(); short w = sprite["w"].GetInt(); short h = sprite["h"].GetInt(); const Sprite spr {x, y, w, h, sprite_texid}; this->icons.push_back(Icon{spr, glm::ivec2(px, py)}); } } /// border clouds & shadows /// this->border.clear(); if (map_json.HasMember("clouds")) { assert(map_json.HasMember("shadows") && "Clouds must have a shadow array"); auto clouds = map_json["clouds"].GetArray(); auto shadows = map_json["shadows"].GetArray(); const size_t count = std::max(clouds.Size(), shadows.Size()); for (size_t i = 0; i < count; i++) { // load clouds & shadows separately std::unique_ptr<Texture> cldtex = nullptr; if (clouds.Size() > i) { cldtex = load_texture(clouds[i].GetString()); if (cldtex == nullptr) { logger << Log::WARN << "Missing cloud layer: " << clouds[i].GetString() << Log::ENDL; } } std::unique_ptr<Texture> shdtex = nullptr; if (shadows.Size() > i) { shdtex = load_texture(shadows[i].GetString()); if (shdtex == nullptr) { logger << Log::WARN << "Missing shadow layer: " << shadows[i].GetString() << Log::ENDL; } } border.push_back(BorderLayer{ std::move(cldtex), std::move(shdtex), glm::mat4{} }); } logger << Log::INFO << "Found " << count << " cloud & shadow layers" << Log::ENDL; } }
void _Parse(const std::string& json) { document_.Parse(json.c_str()); _CheckError(); }