int Condition::Compile(JSONNode columns) { if (operand1[0] == '\'') { operand1IsConstant = true; operand1 = operand1.substr(1, operand1.length() - 2); } else { operand1IsConstant = false; bool contains = false; for (auto j = columns.begin(); !contains && j != columns.end(); ++j) if (j->as_string() == operand1) contains = true; if (!contains) return Errors::ERR_NO_COLUMN; } if (operand2[0] == '\'') { operand2IsConstant = true; operand2 = operand2.substr(1, operand2.length() - 2); } else { operand2IsConstant = false; bool contains = false; for (auto j = columns.begin(); !contains && j != columns.end(); ++j) if (j->as_string() == operand2) contains = true; if (!contains) return Errors::ERR_NO_COLUMN; } return Errors::ERR_OK; }
static void processModel(const JSONNode & nd, MatModel& model) { bool nlayset = false; bool rangeset = false; bool lengthset = false; bool heightset = false; bool atomsset = false; for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::numlayers) { model.mNumLayers = i->as_int(); nlayset = true; } else if (i->name() == JsonNames::range) { model.mRadius = i->as_float(); rangeset = true; } else if (i->name() == JsonNames::length) { model.mLength = i->as_float(); lengthset = true; } else if (i->name() == JsonNames::height) { model.mHeight = i->as_float(); heightset = true; } else if (i->name() == JsonNames::atoms) { BNB_ASSERT(nlayset); readIntVector(*i, model.mNumLayers, model.mLayersAtoms); atomsset = true; } else { BNB_ERROR_REPORT("Illegal name on parsing model data"); } } BNB_ASSERT(nlayset && rangeset && lengthset && heightset && atomsset); }
void ResUtils::ParseJobsResponse(std::list<KP_Job>& jobs, JSONNode &n) { std::string jobId; JSONNode::const_iterator i = n.begin(); if (i != n.end() && i -> name() == TAG_JOBS_STR && i -> type() == JSON_NODE) { JSONNode::const_iterator ijs = (*i).begin(); if (ijs != n.end() && ijs -> name() == TAG_ITEMS_STR && ijs -> type() == JSON_ARRAY) { JSONNode::const_iterator ij = (*ijs).begin(); while (ij != (*ijs).end()) { if(ij -> type() == JSON_ARRAY || ij -> type() == JSON_NODE) { JSONNode::const_iterator ia = (*ij).begin(); if (ia != (*ij).end() && ia -> name() == TAG_ID_STR && ia -> type() != JSON_ARRAY && ia -> type() != JSON_NODE) { jobId = ia -> as_string(); //cout << "Bp: jobId: " << jobId << endl; jobs.push_back(KP_Job (jobId)); } } ij ++; } } } }
void ParseJSON(const JSONNode & n, std::list<KP_Service > & services){ std::string providerId, serviceId; bool isHasServiceId = false, isHasProviderId = false; JSONNode::const_iterator i = n.begin(); while (i != n.end()){ // recursively call ourselves to dig deeper into the tree if (i -> type() == JSON_ARRAY || i -> type() == JSON_NODE){ ParseJSON(*i, services); } // get the node name and value as a string std::string node_name = i -> name(); //std::cout<< "Name: "<< node_name << endl; // find out where to store the values if (node_name == TAG_SERVICE_STR){ isHasServiceId = true; serviceId = i -> as_string(); } else if (node_name == TAG_PROVIDER_STR){ isHasProviderId = true; providerId = i -> as_string(); } //increment the iterator ++i; } if(isHasServiceId && isHasProviderId) services.push_back(KP_Service (providerId, serviceId)); }
void ParseJSON(const JSONNode & n){ JSONNode::const_iterator i = n.begin(); while (i != n.end()){ // recursively call ourselves to dig deeper into the tree if (i -> type() == JSON_ARRAY || i -> type() == JSON_NODE){ ParseJSON(*i); } // get the node name and value as a string std::string node_name = i -> name(); // find out where to store the values if (node_name == "RootA"){ rootA = i -> as_string(); } else if (node_name == "ChildA"){ childA = i -> as_string(); } else if (node_name == "ChildB") childB = i -> as_int(); //increment the iterator ++i; } }
void baiduparser:: _parserJsonS ( const JSONNode & node_tree ) { JSONNode::const_iterator node_iter = node_tree.begin(); while ( node_iter != node_tree.end() ) { string node_name = node_iter->name (); if ( node_name == "data" ) { JSONNode::const_iterator arr_first_elem = node_iter->begin(); JSONNode::const_iterator node_first_elem = arr_first_elem->begin(); while ( node_first_elem != arr_first_elem->end() ) { if ( node_first_elem->name() == "dst" ) { result_ = node_first_elem->as_string(); break; } ++ node_first_elem ; } break; } node_iter ++; } }
static void readDoubleVector(const JSONNode& nd, int vecsz, double * x) { int k = 0; for (auto i = nd.begin(); i != nd.end(); i++) { double u = i->as_float(); BNB_ASSERT(k < vecsz); x[k++] = u; } BNB_ASSERT(k == vecsz); }
bool USB_device::processMessage(ClientConn& client, string& cmd, JSONNode& n){ if (cmd == "controlTransfer"){ unsigned id = jsonIntProp(n, "id", 0); uint8_t bmRequestType = jsonIntProp(n, "bmRequestType", 0xC0); uint8_t bRequest = jsonIntProp(n, "bRequest"); uint16_t wValue = jsonIntProp(n, "wValue", 0); uint16_t wIndex = jsonIntProp(n, "wIndex", 0); bool isIn = bmRequestType & 0x80; JSONNode reply(JSON_NODE); reply.push_back(JSONNode("_action", "return")); reply.push_back(JSONNode("id", id)); int ret = -1000; if (isIn){ uint16_t wLength = jsonIntProp(n, "wLength", 64); if (wLength > 64) wLength = 64; if (wLength < 0) wLength = 0; uint8_t data[wLength]; ret = controlTransfer(bmRequestType, bRequest, wValue, wIndex, data, wLength); if (ret >= 0){ JSONNode data_arr(JSON_ARRAY); for (int i=0; i<ret && i<wLength; i++){ data_arr.push_back(JSONNode("", data[i])); } data_arr.set_name("data"); reply.push_back(data_arr); } }else{ string datastr; JSONNode data = n.at("data"); if (data.type() == JSON_ARRAY){ for(JSONNode::iterator i=data.begin(); i!=data.end(); i++){ datastr.push_back(i->as_int()); } }else{ datastr = data.as_string(); } ret = controlTransfer(bmRequestType, bRequest, wValue, wIndex, (uint8_t *)datastr.data(), datastr.size()); } reply.push_back(JSONNode("status", ret)); client.sendJSON(reply); }else if(cmd == "enterBootloader"){ std::cout << "enterBootloader: "; int r = controlTransfer(0x40|0x80, 0xBB, 0, 0, NULL, 100); std::cout << "return " << r << std::endl; }else{ return false; } return true; }
static void readIntVector(const JSONNode& nd, int vecsz, int * x) { int k = 0; for (auto i = nd.begin(); i != nd.end(); i++) { int u = i->as_int(); BNB_ASSERT(k < vecsz); x[k++] = u; } BNB_ASSERT(k == vecsz); }
bool CPPModule::init(const std::string& jsonParams) { JSONNode n = libjson::parse(jsonParams); std::map<std::string, std::string> tmpParams; JSONNode::iterator i = n.begin(); for(; i!=n.end(); ++i) { tmpParams[(*i).name()] = (*i).as_string(); } return onInit(tmpParams); }
JSONNode JSONNodeHelper::findKey(JSONNode jsonNode, std::string key) { JSONNode returnNode; for (JSONNode::const_iterator i = jsonNode.begin(); i != jsonNode.end(); i++){ if (i->name() == key){ returnNode = *i; } } // TODO: throw exception if the key is not found return returnNode; }
static void processBox(const JSONNode & nd, Box<double> & box) { int n = box.mDim; for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::boxa) { readDoubleVector(*i, n, (double*) (box.mA)); } else if (i->name() == JsonNames::boxb) { readDoubleVector(*i, n, (double*) (box.mB)); } else { BNB_ERROR_REPORT("Unknown name while processing box description"); } } }
bool GameMapEncounterAreaParser::Parse(std::string json, GameMapEncounterArea *encounterArea) { JSONNode assetListNode = libjson::parse(json); JSONNode::const_iterator i = assetListNode.begin(); while (i != assetListNode.end()) { if (i->name() == "mobs" && i->type() == JSON_ARRAY) { this->logger->debug() << "parsing mobs"; JSONNode::const_iterator j = i->begin(); while (j != i->end()) { if (j->type() == JSON_NODE) { JSONNode::const_iterator k = j->begin(); while (k != j->end()) { if (k->name() == "enemies") { this->logger->debug() << "parsing enemies for mob"; JSONNode::const_iterator l = k->begin(); std::string r = ""; std::vector<std::string> mob; while (l != k->end()) { this->logger->debug() << std::setfill ('0') << std::setw(sizeof(unsigned char)*2) << std::hex << l->type(); if (l->type() == JSON_STRING) { r += l->as_string() + ", "; mob.push_back(l->as_string()); } l++; } this->logger->debug() << "parsed mob [" << r << "]"; encounterArea->mobs.push_back(mob); } k++; } } j++; } } i++; } return true; }
static void processLattice(const JSONNode & nd, int n, double& v, double* x) { for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::evalue) { v = i->as_float(); } else if (i->name() == JsonNames::lvector) { readDoubleVector(*i, n, x); } else { BNB_ERROR_REPORT("Unknown name while processing lattice description"); } } }
void Rig::loadJSON(JSONNode root) { //JSONNode::const_iterator i = root.begin(); JSONNode::iterator i = root.begin(); auto version = root.find("version"); if (version == root.end()) { Logger::log(ERR, "No version specified for input file. Aborting load."); return; } else { stringstream ss; stringstream ss2(version->as_string()); ss << LumiverseCore_VERSION_MAJOR << "." << LumiverseCore_VERSION_MINOR; float libVer; float fileVer; ss >> libVer; ss2 >> fileVer; if (fileVer < libVer) { // Friendly warning if you're loading an old file. Logger::log(WARN, "File created against earlier version of Lumiverse. Check logs for any load problems."); } else if (fileVer > libVer) { // Loading newer file with older library. Logger::log(WARN, "File created against newer version of Lumiverse. Check logs for any load problems."); } } while (i != root.end()){ // get the node name and value as a string std::string nodeName = i->name(); if (nodeName == "devices") { loadDevices(*i); Logger::log(INFO, "Device load complete"); } else if (nodeName == "patches") { i->push_back(*root.find("jsonPath")); loadPatches(*i); Logger::log(INFO, "Patch load complete"); } else if (nodeName == "refreshRate") { setRefreshRate(i->as_int()); } //increment the iterator ++i; } }
/** * Extracts JSON model from a string input * @param input JSON string * @param model model to fill in */ static void parseModelData(const std::string& input, MatModel& model) { JSONNode nd = libjson::parse(input); bool modelset = false; for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::modelname) { processModel(*i, model); modelset = true; break; } } if (!modelset) BNB_ERROR_REPORT("Model data missing\n"); }
/** * Parses lattice definition vector from JSON string * @param input JSON string * @param model material model * @param v energy value to store * @param x vector to fill in */ static void parseLatticeData(const std::string& input, const MatModel& model, double& v, double* x) { JSONNode nd = libjson::parse(input); bool latticeset = false; for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::lattice) { processLattice(*i, model.mNumLayers * 3, v, x); latticeset = true; break; } } if (!latticeset) BNB_ERROR_REPORT("Lattice data missing\n"); }
/** * Parses box data * @param input input string with JSON description * @param box to fill in */ static void parseBoxData(const std::string& input, Box<double>& box) { JSONNode nd = libjson::parse(input); bool boxset = false; for (auto i = nd.begin(); i != nd.end(); i++) { if (i->name() == JsonNames::box) { processBox(*i, box); boxset = true; break; } } if (!boxset) BNB_ERROR_REPORT("Lattice data missing\n"); }
void baiduparser:: _parserJsonW ( const JSONNode & node_tree ) { JSONNode::const_iterator node_iter = node_tree.begin(); while ( node_iter != node_tree.end() ) { string node_name = node_iter->name (); if ( node_iter->name () == "result" ) { result_.clear(); } else if ( node_name == "pre") { result_ += node_iter->as_string() + "\n"; } else if ( node_name == "cont" ) { JSONNode::const_iterator result_iter = node_iter->begin(); while ( result_iter != node_iter->end() ) { result_ += result_iter->name() + " |"; result_iter ++; } result_ += "\n"; node_iter ++ ; continue; } if ( node_iter->type() == JSON_ARRAY || node_iter->type() == JSON_NODE ) { _parserJsonW( *node_iter ); } node_iter ++; } }
void SimulationAnimationPatch::loadJSON(const JSONNode data) { string patchName = data.name(); // Load options for raytracer application. (window, ray tracer, filter) JSONNode::const_iterator i = data.begin(); while (i != data.end()) { std::string nodeName = i->name(); if (nodeName == "frameDirectory") { JSONNode fileName = *i; m_file_frameManager = new ArnoldFileFrameManager(fileName.as_string()); } i++; } }
// parse json/config, get filename and tilesize void Tilemap::parseConfig(const JSONNode& n) { JSONNode::const_iterator it = n.begin(); while (it != n.end()) { string nodeName = it->name(); // might need stupid workaround, for some reason it->as_int() doesn't link! if (nodeName == "tilesize") tileSize = atoi((it->as_string()).c_str()); if (nodeName == "tilemap") { tilemapFile = it->as_string(); } ++it; } createTexture(); }
void Rig::loadDevices(JSONNode root) { JSONNode::const_iterator i = root.begin(); // for this we want to iterate through all children and have the device class // parse the sub-element. while (i != root.end()){ // get the node name and value as a string std::string nodeName = i->name(); // Node name is the Device id Device* device = new Device(nodeName, *i); addDevice(device); //increment the iterator ++i; } }
int load_file(const string &filename, uid_user_map_t &uum, vector<msg_t> &msg_vec, log_t *log) { ifstream ifile; string line; string content; string home; long uid; int num = 0; ifile.open(filename.c_str()); if (ifile.is_open()) { while (getline(ifile, line)) { msg_t one_msg; user_t one_user = {-1, "", ""}; JSONNode node = libjson::parse(line); JSONNode::const_iterator jit = node.begin(); while (jit != node.end()) { string name = (string)jit->name(); if (name == "id") { one_msg.mid = atol(jit->as_string().c_str()); } else if (name == "user_id") { uid = atol(jit->as_string().c_str()); one_user.uid = uid; one_msg.uid = uid; } else if (name == "user_name") { one_user.name = jit->as_string(); } else if (name == "location") { one_user.home = jit->as_string(); } else if (name == "text") { one_msg.content = jit->as_string(); } ++jit; } check_user(one_user, uum); msg_vec.push_back(one_msg); ++num; } ifile.close(); } else { error(log, "open raw file %s error", filename.c_str()); } return num; }
void googleparser::_parserJson ( const JSONNode & node_tree ) { // in the google json, the useful information is in the first element // in the array . [[[]], "zh-CN/en" ... JSONNode::const_iterator first_elem_iter = node_tree.begin(); JSONNode::const_iterator second_iter = first_elem_iter->begin(); JSONNode::const_iterator info_iter = second_iter->begin(); result_.clear(); while ( info_iter != second_iter->end() ) { result_ += info_iter->as_string() + " | "; ++ info_iter ; } result_ += '\n'; }
/** * Reads the description from the string * @param json the json description as a string * @param kd the resulting knapsack data */ static void readJson(const std::string& json, KnapData< T >& kd) { struct { bool mNSet; bool mCSet; bool mWSet; bool mPSet; } checklist = {false, false, false, false}; JSONNode nd = libjson::parse(json); JSONNode::const_iterator i = nd.begin(); kd.mN = 0; kd.mTP = 0; kd.mTW = 0; kd.mCoe = NULL; while (i != nd.end()) { std::string name = i->name(); //std::cout << "node \"" << name << "\"\n"; if (name == JSON_KNAP_N) { kd.mN = i->as_int(); kd.mCoe = new KnapRecord<T>[kd.mN]; BNB_ASSERT(kd.mCoe); checklist.mNSet = true; } else if (name == JSON_KNAP_C) { kd.mC = i->as_int(); checklist.mCSet = true; } else if (name == JSON_KNAP_W) { BNB_ASSERT(kd.mCoe); parseW(*i, kd, checklist.mPSet); checklist.mWSet = true; } else if (name == JSON_KNAP_P) { BNB_ASSERT(kd.mCoe); parseP(*i, kd, checklist.mWSet); checklist.mPSet = true; } i++; } }
void Tilemap::load(char* fname) { // load tilemap.json (json) char* buffer = 0; long length; FILE* f = fopen("tilemap.json", "rb"); if (f) { fseek (f, 0, SEEK_END); length = ftell(f); fseek (f, 0, SEEK_SET); buffer = new char [length+1]; fread(buffer, 1, length, f); buffer[length] = 0; fclose(f); } JSONNode tilemap = libjson::parse(buffer); JSONNode::const_iterator it = tilemap.begin(); while (it != tilemap.end()) { string nodeName = it->name(); if (nodeName == "config") parseConfig(*it); else if (nodeName == "tiles") parseTiles(*it); //increment the iterator ++it; } // load tilemap w/ stbi_ // calc tilesperline=tilemap_width/tilesize delete buffer; }
// read through json/tiles // for each tile, calculate index, crestore in a std::vector(?) void Tilemap::parseTiles(const JSONNode& n) { JSONNode::const_iterator it = n.begin(); while (it != n.end()) { string textureId = it->name(); int idx = atoi((it->as_string()).c_str()); //it->as_int(); int x1 = idx%tilesPerLine * tileSize; int y1 = idx/tilesPerLine * tileSize; int x2 = x1+tileSize; int y2 = y1+tileSize; TilemapTexture textureInfo = {(float)x1/tilemapWidth, (float)y1/tilemapHeight, (float)x2/tilemapWidth, (float)y2/tilemapHeight}; tiles[idx] = textureInfo; tileMapping[textureId] = idx; ++it; } }
void xmm::Label::from_json(JSONNode root) { try { if (root.type() != JSON_NODE) throw JSONException("Wrong type: was expecting 'JSON_NODE'", root.name()); JSONNode::const_iterator root_it = root.begin(); if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->name() != "type") throw JSONException("Wrong name: was expecting 'type'", root_it->name()); if (root_it->type() != JSON_STRING) throw JSONException("Wrong type: was expecting 'JSON_STRING'", root_it->name()); if (root_it->as_string() == "INT") type = INT; else type = SYM; ++root_it; if (root_it == root.end()) throw JSONException("JSON Node is incomplete", root_it->name()); if (root_it->name() != "value") throw JSONException("Wrong name: was expecting 'value'", root_it->name()); if (type == INT) { if (root_it->type() != JSON_NUMBER) throw JSONException("Wrong type: was expecting 'JSON_NUMBER'", root_it->name()); intLabel_ = static_cast<int>(root_it->as_int()); } else { if (root_it->type() != JSON_STRING) throw JSONException("Wrong type: was expecting 'JSON_STRING'", root_it->name()); symLabel_ = root_it->as_string(); } } catch (JSONException &e) { throw JSONException(e, root.name()); } catch (std::exception &e) { throw JSONException(e, root.name()); } }
bool Message::is_valid(JSONNode& param) { if (!validate_params_) return true; for(JSONNode::iterator it(param.begin()); it != param.end(); ++it) { if (!is_valid(*it)) return false; std::string lowercased(it->name()); generic_util::trim(lowercased); std::transform(lowercased.begin(), lowercased.end(), lowercased.begin(), ::tolower); bool invalid(std::find(reserved_names_.begin(), reserved_names_.end(), lowercased) != reserved_names_.end()); if (invalid) { set(Optional_Fields::warning, "Some parameters are missing from this log message because they were malformed."); return false; } } return true; }
void CVkProto::PollUpdates(const JSONNode &jnUpdates) { debugLogA("CVkProto::PollUpdates"); CMStringA mids; int msgid, uid, flags, platform; MCONTACT hContact; for (auto it = jnUpdates.begin(); it != jnUpdates.end(); ++it) { const JSONNode &jnChild = (*it).as_array(); switch (jnChild[json_index_t(0)].as_int()) { case VKPOLL_MSG_DELFLAGS: if (jnChild.size() < 4) break; msgid = jnChild[1].as_int(); flags = jnChild[2].as_int(); uid = jnChild[3].as_int(); hContact = FindUser(uid); if (hContact != NULL && (flags & VKFLAG_MSGUNREAD) && !CheckMid(m_incIds, msgid)) { setDword(hContact, "LastMsgReadTime", time(NULL)); if (ServiceExists(MS_MESSAGESTATE_UPDATE)) { MessageReadData data(time(NULL), MRD_TYPE_READTIME); CallService(MS_MESSAGESTATE_UPDATE, hContact, (LPARAM)&data); } else SetSrmmReadStatus(hContact); if (m_bUserForceOnlineOnActivity) SetInvisible(hContact); if (m_bSyncReadMessageStatusFromServer) MarkDialogAsRead(hContact); } break; case VKPOLL_MSG_ADDED: // new message msgid = jnChild[1].as_int(); // skip outgoing messages sent from a client flags = jnChild[2].as_int(); if (flags & VKFLAG_MSGOUTBOX && !(flags & VKFLAG_MSGCHAT) && CheckMid(m_sendIds, msgid)) break; if (!mids.IsEmpty()) mids.AppendChar(','); mids.AppendFormat("%d", msgid); break; case VKPOLL_READ_ALL_OUT: uid = jnChild[1].as_int(); hContact = FindUser(uid); if (hContact != NULL) { setDword(hContact, "LastMsgReadTime", time(NULL)); if (ServiceExists(MS_MESSAGESTATE_UPDATE)) { MessageReadData data(time(NULL), MRD_TYPE_READTIME); CallService(MS_MESSAGESTATE_UPDATE, hContact, (LPARAM)&data); } else SetSrmmReadStatus(hContact); if (m_bUserForceOnlineOnActivity) SetInvisible(hContact); } break; case VKPOLL_READ_ALL_IN: uid = jnChild[1].as_int(); hContact = FindUser(uid); if (hContact != NULL && m_bSyncReadMessageStatusFromServer) MarkDialogAsRead(hContact); break; case VKPOLL_USR_ONLINE: uid = -jnChild[1].as_int(); if ((hContact = FindUser(uid)) != NULL) { setWord(hContact, "Status", ID_STATUS_ONLINE); platform = jnChild[2].as_int(); SetMirVer(hContact, platform); } break; case VKPOLL_USR_OFFLINE: uid = -jnChild[1].as_int(); if ((hContact = FindUser(uid)) != NULL) { setWord(hContact, "Status", ID_STATUS_OFFLINE); db_unset(hContact, m_szModuleName, "ListeningTo"); SetMirVer(hContact, -1); } break; case VKPOLL_USR_UTN: uid = jnChild[1].as_int(); hContact = FindUser(uid); if (hContact != NULL) { ForkThread(&CVkProto::ContactTypingThread, (void *)hContact); if (m_bUserForceOnlineOnActivity) SetInvisible(hContact); } break; case VKPOLL_CHAT_UTN: ForkThread(&CVkProto::ChatContactTypingThread, new CVKChatContactTypingParam(jnChild[2].as_int(), jnChild[1].as_int())); break; case VKPOLL_CHAT_CHANGED: int chat_id = jnChild[1].as_int(); CVkChatInfo *cc = m_chats.find((CVkChatInfo*)&chat_id); if (cc) RetrieveChatInfo(cc); break; } } RetrieveMessagesByIds(mids); }