static elliptics_client_state create(const std::string &conf, rapidjson::Document &doc) { FILE *cf; cf = fopen(conf.c_str(), "r"); if (!cf) { std::ostringstream str; str << "failed to open config file '" << conf << "'"; throw configuration_error(str.str().c_str()); } try { rapidjson::FileStream fs(cf); doc.ParseStream<rapidjson::kParseDefaultFlags, rapidjson::UTF8<>, rapidjson::FileStream>(fs); if (doc.HasParseError()) { std::ostringstream str; str << "can not parse config file '" << conf << "': " << doc.GetParseError(); throw configuration_error(str.str().c_str()); } fclose(cf); cf = NULL; return create(doc); } catch (...) { if (cf) fclose(cf); throw; } }
int ytopen_sdk::GetFaceInfo(rapidjson::Document &result, const string&face_id) { std::stringstream ss; ss<<host<<"/youtu/api/getfaceinfo"; string addr; addr.assign(ss.str()); string req; string rsp; StringBuffer sbuffer; Writer<StringBuffer> writer(sbuffer); writer.StartObject(); writer.String("app_id");writer.String(app_id.c_str()); writer.String("face_id");writer.String(face_id.c_str()); writer.EndObject(); req = sbuffer.GetString(); int ret = curl_method(addr, req, rsp); if(ret == 0) { result.Parse<rapidjson::kParseStopWhenDoneFlag>(rsp.c_str()); if(result.HasParseError()) { std::cout << "RapidJson parse error " << result.GetParseError() << endl; return -1; } }else { return -1; } return 0; }
bool parse_file(rapidjson::Document & doc, std::string const& file_name) { file_string file_data(file_name); if(file_data.is_valid() == false) { log_error(log_scope::ENGINE, "Unable to open json file \"{}\".", file_name); return false; } doc.Parse<0>(file_data.get_str().c_str()); if(doc.HasParseError()) { // rapidjson does not provide line/column, only character offset // hacking it here until (if) rapidjson implements it std::string const& file_data_str = file_data.get_str(); size_t const error_char_offset = doc.GetErrorOffset(); std::string::const_iterator error_it = file_data_str.cbegin() + error_char_offset; // Compute line number, using 1 as base line size_t const error_line_num = 1 + std::count_if( file_data_str.cbegin(), error_it, std::bind2nd(std::equal_to<char>(), '\n')); // Compute column (char offset into line), using 1 as base column std::string::const_reverse_iterator reverse_error_it{error_it}; auto error_line_begin_it = std::find( reverse_error_it, file_data_str.crend(), '\n'); // If this is the first line we can size_t const error_column_num = error_line_begin_it != file_data_str.crend() ? std::distance(reverse_error_it, error_line_begin_it) + 1 : error_char_offset + 1; // Log error log_error(log_scope::ENGINE, "Error parsing json file {}", file_name); log_error_no_prefix(log_line_seperator); log_error_no_prefix("== JSON ERRORS =========="); log_error_no_prefix("Line {}, Column {} - {}", error_line_num, error_column_num, doc.GetParseError()); log_error_no_prefix(log_line_seperator); return false; } return true; }
bool ComRender::readJson(const std::string &fileName, rapidjson::Document &doc) { bool ret = false; do { std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName); doc.Parse<0>(contentStr.c_str()); CC_BREAK_IF(doc.HasParseError()); ret = true; } while (0); return ret; }
bool parseJson(const char* json,rapidjson::Document& doc){ doc.Parse<0>(json); if(doc.HasParseError()){ rapidjson::ParseErrorCode e = doc.GetParseError(); size_t o = doc.GetErrorOffset(); std::cerr << "Error: " << rapidjson::GetParseError_En(e) << std::endl;; std::string slice = (strlen(json)>o+10)?std::string(json).substr(o,10):std::string(json); std::cerr << " at offset " << o << " near '" << slice << "...'" << std::endl; return false; } return true; }
bool SceneReader::readJson(const std::string &fileName, rapidjson::Document &doc) { bool bRet = false; do { std::string jsonpath = FileUtils::getInstance()->fullPathForFilename(fileName); std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath); doc.Parse<0>(contentStr.c_str()); CC_BREAK_IF(doc.HasParseError()); bRet = true; } while (0); return bRet; }
int ytopen_sdk::NewPerson(rapidjson::Document &result, const string& person_id, const string &person_name, const std::vector<string> &group_ids, const string& imagePath, int data_type, const string &tag) { string imageData; if(data_type == 0 && 0 != read_image(imagePath, imageData)) { cout << "image read failed. " << imagePath << endl; return -1; } std::stringstream ss; ss<<host<<"/youtu/api/newperson"; string addr; addr.assign(ss.str()); string req; string rsp; StringBuffer sbuffer; Writer<StringBuffer> writer(sbuffer); writer.StartObject(); writer.String("app_id"); writer.String(app_id.c_str()); if(data_type == 0) { string encode_data = b64_encode(imageData); writer.String("image"); writer.String(encode_data.c_str()); }else { writer.String("url"); writer.String(imagePath.c_str()); } writer.String("person_id"); writer.String(person_id.c_str()); writer.String("person_name"); writer.String(person_name.c_str()); writer.String("group_ids"); writer.StartArray(); for(int i = 0; i < group_ids.size(); i++) writer.String(group_ids[i].c_str()); writer.EndArray(); writer.String("tag"); writer.String(tag.c_str()); writer.EndObject(); req = sbuffer.GetString(); int ret = curl_method(addr, req, rsp); if(ret == 0) { result.Parse<rapidjson::kParseStopWhenDoneFlag>(rsp.c_str()); if(result.HasParseError()) { std::cout << "RapidJson parse error " << result.GetParseError() << endl; return -1; } }else { return -1; } return 0; }
int ytopen_sdk::FaceCompare(rapidjson::Document &result, const string& imagePathA, const string&imagePathB, int data_type) { result.SetNull(); string imageA, imageB; if(data_type == 0 && (0 != read_image(imagePathA, imageA) || 0 != read_image(imagePathB, imageB))) { cout << "image read failed. " << imagePathA << "," << imagePathB << endl; return -1; } std::stringstream ss; ss<<host<<"/youtu/api/facecompare"; string addr; addr.assign(ss.str()); string req; string rsp; StringBuffer sbuffer; Writer<StringBuffer> writer(sbuffer); writer.StartObject(); writer.String("app_id"); writer.String(app_id.c_str()); if(data_type == 0) { string encode_data = b64_encode(imageA); writer.String("imageA"); writer.String(encode_data.c_str()); encode_data = b64_encode(imageB); writer.String("imageB"); writer.String(encode_data.c_str()); }else { writer.String("urlA"); writer.String(imagePathA.c_str()); writer.String("urlB"); writer.String(imagePathB.c_str()); } writer.EndObject(); req = sbuffer.GetString(); int ret = curl_method(addr, req, rsp); if(ret == 0) { result.Parse<rapidjson::kParseStopWhenDoneFlag>(rsp.c_str()); if(result.HasParseError()) { std::cout << "RapidJson parse error " << result.GetParseError() << endl; return -1; } }else { return -1; } return 0; }
int ytopen_sdk::ImageTag(rapidjson::Document &result, const std::string &imagePath, int data_type, const string &cookie) { string imageData; if(data_type == 0 && 0 != read_image(imagePath, imageData)) { cout << "image read failed. " << imagePath << endl; return -1; } std::stringstream ss; ss<<host<<"/youtu/imageapi/imagetag"; string addr; addr.assign(ss.str()); string req; string rsp; StringBuffer sbuffer; Writer<StringBuffer> writer(sbuffer); writer.StartObject(); writer.String("app_id"); writer.String(app_id.c_str()); if(data_type == 0) { string encode_data = b64_encode(imageData); writer.String("image"); writer.String(encode_data.c_str()); }else { writer.String("url"); writer.String(imagePath.c_str()); } if(!cookie.empty()) { writer.String("cookie"); writer.String(cookie.c_str()); } writer.EndObject(); req = sbuffer.GetString(); int ret = curl_method(addr, req, rsp); if(ret == 0) { result.Parse<rapidjson::kParseStopWhenDoneFlag>(rsp.c_str()); if(result.HasParseError()) { std::cout << "RapidJson parse error " << result.GetParseError() << endl; return -1; } }else { return -1; } return 0; }
bool ComRender::readJson(const std::string &fileName, rapidjson::Document &doc) { bool ret = false; do { std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName); if (contentStr.empty()) { log("%s error: get string from %s failed!", __FUNCTION__, fileName.c_str()); break; } doc.Parse<0>(contentStr.c_str()); CC_BREAK_IF(doc.HasParseError()); ret = true; } while (0); return ret; }
bool CCComRender::readJson(const char *pszFileName, rapidjson::Document &doc) { bool bRet = false; unsigned long size = 0; unsigned char *pBytes = NULL; do { CC_BREAK_IF(pszFileName == NULL); std::string jsonpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFileName); pBytes = cocos2d::CCFileUtils::sharedFileUtils()->getFileData(jsonpath.c_str(), "r", &size); CC_BREAK_IF(pBytes == NULL || strcmp((char*)pBytes, "") == 0); std::string load_str((const char*)pBytes, size); CC_SAFE_DELETE_ARRAY(pBytes); doc.Parse<0>(load_str.c_str()); CC_BREAK_IF(doc.HasParseError()); bRet = true; } while (0); return bRet; }
int SettingRegistry::loadJSON(std::string filename, rapidjson::Document& json_document) { FILE* f = fopen(filename.c_str(), "rb"); if (!f) { cura::logError("Couldn't open JSON file.\n"); return 1; } char read_buffer[4096]; rapidjson::FileReadStream reader_stream(f, read_buffer, sizeof(read_buffer)); json_document.ParseStream(reader_stream); fclose(f); if (json_document.HasParseError()) { cura::logError("Error parsing JSON(offset %u): %s\n", (unsigned)json_document.GetErrorOffset(), GetParseError_En(json_document.GetParseError())); return 2; } return 0; }
inline bool loadDocument(const std::string &path, rapidjson::Document &document) { // Load schema JSON from file std::string file; if (!loadFile(path, file)) { std::cerr << "Failed to load json from file '" << path << "'." << std::endl; return false; } // Parse schema document.Parse<0>(file.c_str()); if (document.HasParseError()) { std::cerr << "RapidJson failed to parse the document:" << std::endl; std::cerr << "Parse error: " << document.GetParseError() << std::endl; std::cerr << "Near: " << file.substr(std::max(size_t(0), document.GetErrorOffset() - 20), 40) << std::endl; return false; } return true; }
int ytopen_sdk::DelFace(rapidjson::Document &result, const string& person_id, const std::vector<string>& face_ids) { std::stringstream ss; ss<<host<<"/youtu/api/delface"; string addr; addr.assign(ss.str()); string req; string rsp; StringBuffer sbuffer; Writer<StringBuffer> writer(sbuffer); writer.StartObject(); writer.String("app_id"); writer.String(app_id.c_str()); writer.String("person_id"); writer.String(person_id.c_str()); writer.String("face_ids"); writer.StartArray(); for(int i = 0; i < face_ids.size(); i++) { writer.String(face_ids[i].c_str()); } writer.EndArray(); writer.EndObject(); req = sbuffer.GetString(); int ret = curl_method(addr, req, rsp); if(ret == 0) { result.Parse<rapidjson::kParseStopWhenDoneFlag>(rsp.c_str()); if(result.HasParseError()) { std::cout << "RapidJson parse error " << result.GetParseError() << endl; return -1; } }else { return -1; } return 0; }
bool Utils::_loadConfig(const char *filename, rapidjson::Document &doc, KVMap &kvmap, bool &bLoaded) { std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename); std::string fileContent = FileUtils::getInstance()->getStringFromFile(fullPath); doc.Parse<0>(fileContent.c_str()); if (doc.HasParseError()) { CCLOG("GetParseError %s\n", doc.GetParseError()); return false; } for (rapidjson::Value::ConstMemberIterator it=doc.MemberonBegin(); it!=doc.MemberonEnd(); ++it) { const auto &member = *it; const char *name = member.name.GetString(); const rapidjson::Value &val = member.value; kvmap.insert(Utils::KVMapPair(name, val)); } bLoaded = true; return true; }
JSON_API void read_json(std::string const &fname, rapidjson::Document &d) { FILE* fp = fopen(fname.c_str(), "rb"); // non-Windows use "r" if(!fp) { std::stringstream str; str << "ERROR [read_json]: file " << fname << " not found!"; throw std::runtime_error(str.str()); } fseek(fp, 0, SEEK_END); size_t length = static_cast<size_t>(ftell(fp)); //std::cout << "file size = " << length << std::endl; fseek(fp, 0, SEEK_SET); char* readBuffer = static_cast<char*>(malloc(length + 1)); size_t readLength = fread(readBuffer, 1, length, fp); readBuffer[readLength] = '\0'; fclose(fp); d.Parse<rapidjson::kParseCommentsFlag>(readBuffer); if(d.HasParseError()) { std::stringstream str; str << "ERROR [read_json]: json document cannot be parsed!" << std::endl; str << "\t Parse Error " << d.GetParseError() << " (" << GetParseError_En(d.GetParseError()) << ")" << std::endl; str << "\t Error offset = " << d.GetErrorOffset() << '\n'; throw std::runtime_error(str.str()); } if(!d.IsObject()) { std::stringstream str; str << "ERROR [read_json]: json document is not an object!"; throw std::runtime_error(str.str()); } }
int ytopen_sdk::AddFace(rapidjson::Document &result, const string& person_id, const std::vector<string>& imagePaths, int data_type, const string &tag) { std::stringstream ss; ss<<host<<"/youtu/api/addface"; string addr; addr.assign(ss.str()); string req; string rsp; string imageData; StringBuffer sbuffer; Writer<StringBuffer> writer(sbuffer); writer.StartObject(); writer.String("app_id"); writer.String(app_id.c_str()); if(data_type == 0) { writer.String("images"); writer.StartArray(); for(int i = 0; i < imagePaths.size(); i++) { if(0 != read_image(imagePaths[i], imageData)) { cout << "read image failed " << imagePaths[i] << endl; continue; } string encode_data = b64_encode(imageData); writer.String(encode_data.c_str()); } writer.EndArray(); }else { writer.String("urls"); writer.StartArray(); for(int i = 0; i < imagePaths.size(); i++) { if(!imagePaths[i].empty()) { writer.String(imagePaths[i].c_str()); }else { cout << "url empty." <<endl; } } writer.EndArray(); } writer.String("person_id"); writer.String(person_id.c_str()); writer.String("tag"); writer.String(tag.c_str()); writer.EndObject(); req = sbuffer.GetString(); int ret = curl_method(addr, req, rsp); if(ret == 0) { result.Parse<rapidjson::kParseStopWhenDoneFlag>(rsp.c_str()); if(result.HasParseError()) { std::cout << "RapidJson parse error " << result.GetParseError() << endl; return -1; } }else { return -1; } return 0; }
void deserializeTextures(std::vector<Texture> &textures, rapidjson::Document &doc) { if (doc.HasParseError()) { std::cerr << "Incorrect json." << std::endl; return; } if (!doc.HasMember("textures")) { std::cerr << "Incorrect settings file. Directive lighting is absent." << std::endl; return; } rapidjson::Value& jTextures = doc["textures"]; for (rapidjson::Value& jTex : jTextures.GetArray()) { Texture tex; assert(jTex.HasMember("file_path") && jTex["file_path"].IsString()); tex.file_path = jTex["file_path"].GetString(); if (jTex.HasMember("wrap_s_repeat")) tex.wrap_s_repeat = jTex["wrap_s_repeat"].GetBool(); if (jTex.HasMember("wrap_t_repeat")) tex.wrap_t_repeat = jTex["wrap_t_repeat"].GetBool(); if (jTex.HasMember("min_filter")) { if (jTex["min_filter"].IsInt()) { unsigned int glMinFiltervi[] = { GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR }; tex.min_filter = glMinFiltervi[jTex["min_filter"].GetInt()]; } else if (jTex["min_filter"].IsString()) { std::unordered_map<std::string, unsigned int> glMinFilters({ { "nearest", GL_NEAREST }, { "linear", GL_LINEAR }, { "nearest_mipmap_nearest", GL_NEAREST_MIPMAP_NEAREST }, { "linear_mipmap_nearest", GL_LINEAR_MIPMAP_NEAREST }, { "nearest_mipmap_linear", GL_NEAREST_MIPMAP_LINEAR }, { "linear_mipmap_linear", GL_LINEAR_MIPMAP_LINEAR } }); tex.min_filter = glMinFilters[jTex["min_filter"].GetString()]; } } if (jTex.HasMember("mag_filter")) { if (jTex["mag_filter"].IsInt()) tex.mag_filter = (jTex["mag_filter"].GetInt() == 1 ? GL_NEAREST : GL_LINEAR); else if (jTex["mag_filter"].IsString()) tex.mag_filter = (jTex["mag_filter"].GetString() == "nearest" ? GL_NEAREST : GL_LINEAR); } if (jTex.HasMember("env_mode")) { if (jTex["env_mode"].IsInt()) { unsigned int glEnvModevi[]{ GL_REPLACE, GL_MODULATE, GL_ADD, GL_ADD_SIGNED, GL_INTERPOLATE, GL_SUBTRACT, GL_DOT3_RGB, GL_DOT3_RGBA }; tex.env_mode = glEnvModevi[jTex["env_mode"].GetInt()]; } else if (jTex["env_mode"].IsString()) { std::unordered_map<std::string, unsigned int> glEnvModes({ { "replace", GL_REPLACE }, { "modulate", GL_MODULATE }, { "add", GL_ADD }, { "add_signed", GL_ADD_SIGNED }, { "interpolate", GL_INTERPOLATE }, { "substract", GL_SUBTRACT }, { "dot3_rgb", GL_DOT3_RGB }, { "dot3_rgba", GL_DOT3_RGBA } }); tex.env_mode = glEnvModes[jTex["env_mode"].GetString()]; } } if (jTex.HasMember("detail_level")) tex.detail_level = jTex["detail_level"].GetInt(); textures.push_back(tex); } }