inline bool fromJson(const rapidjson::Value &v, const char *field, T &dst) { const rapidjson::Value::Member *member = v.FindMember(field); if (!member) return false; return fromJson(member->value, dst); }
name_t::name_t(const rapidjson::Value &json_value) { auto last_iter = json_value.FindMember("last"); if ( last_iter != json_value.MemberEnd() ) { assert(last_iter->value.IsString()); last = last_iter->value.GetString(); } auto first_iter = json_value.FindMember("first"); if ( first_iter != json_value.MemberEnd() ) { assert(first_iter->value.IsString()); first = first_iter->value.GetString(); } }
bool JSASTLabeledStatement::Parse(const rapidjson::Value& value) { JSASTStatement::Parse(value); const Value::Member* body = value.FindMember("body"); assert(body); const Value::Member* label = value.FindMember("label"); assert(label); body_ = ParseStatement(body->value); label_ = (JSASTIdentifier*) ParseExpression(label->value, JSAST_IDENTIFIER); return body_ && label_; }
bool JSASTIdentifier::Parse(const rapidjson::Value& value) { JSASTExpression::Parse(value); if (value.FindMember("name")) name_ = value["name"].GetString(); return true; }
void ensure_has_object_mem(rapidjson::Value & _val, rapidjson::Value::StringRefType _name, rapidjson::Document & _doc) { if (!_val.HasMember(_name)) { rapidjson::Value v(rapidjson::kObjectType); _val.AddMember(_name, v, _doc.GetAllocator()); } RAPIDJSON_ASSERT(_val.FindMember(_name)->value.IsObject()); }
int32_t fetch_event_imstate::parse(const rapidjson::Value& _node_event_data) { auto iter_imstates = _node_event_data.FindMember("imStates"); if (iter_imstates == _node_event_data.MemberEnd() || !iter_imstates->value.IsArray()) return wpie_error_parse_response; for (auto iter_state = iter_imstates->value.Begin(); iter_state != iter_imstates->value.End(); ++iter_state) { imstate ustate; auto iter_request_id = iter_state->FindMember("sendReqId"); if (iter_request_id == iter_state->MemberEnd() || !iter_request_id->value.IsString()) continue; ustate.set_request_id(rapidjson_get_string(iter_request_id->value)); auto iter_msg_id = iter_state->FindMember("msgId"); if (iter_msg_id == iter_state->MemberEnd() || !iter_msg_id->value.IsString()) continue; ustate.set_msg_id(rapidjson_get_string(iter_msg_id->value)); auto iter_state_state = iter_state->FindMember("state"); if (iter_state_state == iter_state->MemberEnd() || !iter_state_state->value.IsString()) continue; const std::string state = rapidjson_get_string(iter_state_state->value); if (state == "failed") ustate.set_state(imstate_sent_state::failed); else if (state == "sent") ustate.set_state(imstate_sent_state::sent); else if (state == "delivered") ustate.set_state(imstate_sent_state::delivered); auto iter_hist_msg_id = iter_state->FindMember("histMsgId"); if (iter_hist_msg_id != iter_state->MemberEnd() && iter_hist_msg_id->value.IsInt64()) { ustate.set_hist_msg_id(iter_hist_msg_id->value.GetInt64()); } auto iter_before_hist_msg_id = iter_state->FindMember("beforeHistMsgId"); if (iter_before_hist_msg_id != iter_state->MemberEnd() && iter_before_hist_msg_id->value.IsInt64()) { ustate.set_before_hist_msg_id(iter_before_hist_msg_id->value.GetInt64()); } auto iter_error_code = iter_state->FindMember("errorCode"); if (iter_error_code != iter_state->MemberEnd() && iter_error_code->value.IsInt()) { ustate.set_error_code(iter_error_code->value.GetInt()); } states_.push_back(ustate); } return 0; }
bool JSASTComment::Parse(const rapidjson::Value& value) { JSASTNode::Parse(value); // Block or Line if (value.FindMember("type")) { String type = value["type"].GetString(); isBlock_ = type == "Block"; } if (value.FindMember("value")) { value_ = value["value"].GetString(); } return true; }
/*! Sets box geometry from given rapidjson object. Given object must have "start" and "end" items and both must be arrays of 3 numbers. */ void set_geometry(const rapidjson::Value& object) { const auto& obj_start_i = object.FindMember("start"); if (obj_start_i == object.MemberEnd()) { throw std::invalid_argument(__FILE__ ": Given object doesn't have a start item."); } const auto& obj_start = obj_start_i->value; if (not obj_start.IsArray()) { throw std::invalid_argument(__FILE__ ": Start item isn't an array."); } if (obj_start.Size() != 3) { throw std::invalid_argument(__FILE__ ": Start item doesn't have a length of 3."); } this->start_rw[0] = obj_start[0].GetDouble(); this->start_rw[1] = obj_start[1].GetDouble(); this->start_rw[2] = obj_start[2].GetDouble(); const auto& obj_end_i = object.FindMember("end"); if (obj_end_i == object.MemberEnd()) { throw std::invalid_argument(__FILE__ ": Given object doesn't have an end item."); } const auto& obj_end = obj_end_i->value; if (not obj_end.IsArray()) { throw std::invalid_argument(__FILE__ ": End item isn't an array."); } if (obj_end.Size() != 3) { throw std::invalid_argument(__FILE__ ": End item doesn't have a length of 3."); } this->end_rw[0] = obj_end[0].GetDouble(); this->end_rw[1] = obj_end[1].GetDouble(); this->end_rw[2] = obj_end[2].GetDouble(); for (size_t i = 0; i < 3; i++) { if (this->end_rw[i] <= this->start_rw[i]) { throw std::invalid_argument(__FILE__ ": Given object ends before it starts."); } } }
static void convert_struct_by_name(rapidjson::Document & /*doc*/, const rapidjson::Value & val, const std::string & tname, const std::size_t lvl) { const char * ename = tname.c_str(); //bool is_msg = is_as_msg(val); //const char * fn_prefix = is_msg ? "" : "static inline "; const rapidjson::Value & fields = val.FindMember("fields")->value; filebuf & outf = s_outf_converter; if (fields.MemberCount() > 0) { outf.pf(lvl, "/// @%s\n", ename); outf.pf(lvl, "static inline rapidjson::Value c2json(rapidjson::Document & jd, const %s & src)\n", ename); outf.pf(lvl, "{\n"); outf.pf(lvl+1, "rapidjson::Value v(rapidjson::kObjectType);\n"); for (rapidjson::Value::ConstMemberIterator itr = fields.MemberBegin(); itr != fields.MemberEnd(); ++itr) { const char * mname = itr->name.GetString(); auto & mval = itr->value; if (mval.FindMember("maybenull")->value.GetBool()) { outf.pf(lvl+1, "ENC_MEM_IF(jd, \"%s\", v, src.%s, src.has_%s);\n", mname, mname, mname); } else { outf.pf(lvl+1, "ENC_MEM(jd, \"%s\", v, src.%s);\n", mname, mname); } } outf.pf(0, "\n"); outf.pf(lvl+1, "return v;\n"); outf.pf(lvl, "}\n"); outf.pf(lvl, "static inline void json2c(%s & dst, const rapidjson::Value & src)\n", ename); outf.pf(lvl, "{\n"); for (rapidjson::Value::ConstMemberIterator itr = fields.MemberBegin(); itr != fields.MemberEnd(); ++itr) { const char * mname = itr->name.GetString(); auto & mval = itr->value; if (mval.FindMember("maybenull")->value.GetBool()) { outf.pf(lvl+1, "DEC_MEM_IF(\"%s\", src, dst.%s, dst.has_%s);\n", mname, mname, mname); } else { outf.pf(lvl+1, "DEC_MEM(\"%s\", src, dst.%s);\n", mname, mname); } } outf.pf(lvl, "}\n"); outf.pf(0, "\n"); } else { outf.pf(lvl, "static inline rapidjson::Value c2json(rapidjson::Document & /*jd*/, const %s & /*src*/) {\n", ename); outf.pf(lvl+1, "return rapidjson::Value(rapidjson::kObjectType);\n"); outf.pf(lvl, "}\n"); outf.pf(lvl, "static inline void json2c(%s & /*dst*/, const rapidjson::Value & /*src*/) {}\n", ename); outf.pf(0, "\n"); } }
QueryError OnCommand(const rapidjson::Value &p_Args, std::string *p_Result) { // Get the "command" argument auto s_CommandValue = p_Args.FindMember("command"); if (s_CommandValue == p_Args.MemberEnd() || !s_CommandValue->value.IsString()) { *p_Result = "Bad query: A \"command\" argument is required and must be a string"; return QueryError_BadQuery; } // Get the "internal" argument (defaults to false) auto s_InternalValue = p_Args.FindMember("internal"); auto s_IsInternal = (s_InternalValue != p_Args.MemberEnd() && s_InternalValue->value.IsBool() && s_InternalValue->value.GetBool()); // Run the command if (!Modules::CommandMap::Instance().ExecuteCommandWithStatus(s_CommandValue->value.GetString(), !s_IsInternal, p_Result)) return QueryError_CommandFailed; return QueryError_Ok; }
int32_t robusto_add_client::parse_results(const rapidjson::Value& _node_results) { auto iter_client_id = _node_results.FindMember("clientId"); if (iter_client_id == _node_results.MemberEnd()) return wpie_http_parse_response; client_id_ = iter_client_id->value.GetUint(); return 0; }
bool JSASTBlockStatement::Parse(const rapidjson::Value& value) { if (value.FindMember("body")) { ParseStatementArray(value["body"], body_); } return true; }
int32_t favorite::unserialize(const rapidjson::Value& _node) { auto iter_aimid = _node.FindMember("aimId"); if (iter_aimid == _node.MemberEnd() || !iter_aimid->value.IsString()) return -1; aimid_ = iter_aimid->value.GetString(); auto iter_time = _node.FindMember("time"); if (iter_time == _node.MemberEnd() || !iter_time->value.IsInt64()) { time_ = 0; return 0; } time_ = iter_time->value.GetInt64(); return 0; }
inline auto GetIterator(const rapidjson::Value& value, const char* name) { auto it = value.FindMember(name); if (it == value.MemberEnd()) { SDR::Error::Make("Member Json value \"%s\" not found", name); } return it; }
MapPoint deserializePoint(rapidjson::Value& value) { MapPoint p; rapidjson::Value& position = value.FindMember("position")->value; if(position.IsArray()) { p.point.x = position[0].GetDouble(); p.point.y = position[1].GetDouble(); } rapidjson::Value& name = value.FindMember("name")->value; if(name.IsString()) { p.name = name.GetString(); } rapidjson::Value& desc = value.FindMember("description")->value; if(desc.IsString()) { p.description = desc.GetString(); } return p; }
bool JSASTNode::ParseLoc(const rapidjson::Value& value) { // SpiderMonkey can have this for loc :/ if (value.IsNull()) return true; assert(value.IsObject()); const Value::Member* mstart = value.FindMember("start"); assert(mstart); const Value::Member* mend = value.FindMember("end"); assert(mend); loc_.startLine_ = mstart->value["line"].GetInt(); loc_.startColumn_ = mstart->value["column"].GetInt(); loc_.endLine_ = mend->value["line"].GetInt(); loc_.endColumn_ = mend->value["column"].GetInt(); return true; }
bool JSASTExpressionStatement::Parse(const rapidjson::Value& value) { JSASTStatement::Parse(value); const Value::Member* expr = value.FindMember("expression"); assert(expr); expression_ = ParseExpression(expr->value); return expression_ != NULL; }
void etcd_packer::unpack_int(rapidjson::Value &json_val, const char *key, uint64_t &out) { rapidjson::Value::MemberIterator iter = json_val.FindMember(key); if (iter == json_val.MemberEnd()) { out = 0; } else { if (iter->value.IsUint64()) { out = iter->value.GetUint64(); } else if (iter->value.IsString()) { const char *val = iter->value.GetString(); ::util::string::str2int(out, val); } } }
bool etcd_packer::unpack_string(rapidjson::Value &json_val, const char *key, std::string &val) { rapidjson::Value::MemberIterator iter = json_val.FindMember(key); if (iter == json_val.MemberEnd()) { return false; } switch (iter->value.GetType()) { case rapidjson::kNullType: { val.clear(); break; } case rapidjson::kFalseType: { val = "false"; break; } case rapidjson::kTrueType: { val = "true"; break; } case rapidjson::kObjectType: { val = "[object object]"; break; } case rapidjson::kArrayType: { val = "[object array]"; break; } case rapidjson::kStringType: { val = iter->value.GetString(); break; } case rapidjson::kNumberType: { std::stringstream ss; if (iter->value.IsDouble()) { ss << iter->value.GetDouble(); } else if (iter->value.IsInt()) { ss << iter->value.GetInt(); } else if (iter->value.IsUint()) { ss << iter->value.GetUint(); } else if (iter->value.IsInt64()) { ss << iter->value.GetInt64(); } else { ss << iter->value.GetUint64(); } val = ss.str(); break; } } return true; }
int32_t my_info::unserialize(const rapidjson::Value& _node) { auto iter_aimid = _node.FindMember("aimId"); if (iter_aimid != _node.MemberEnd() && iter_aimid->value.IsString()) aimId_ = iter_aimid->value.GetString(); auto iter_displayId = _node.FindMember("displayId"); if (iter_displayId != _node.MemberEnd() && iter_displayId->value.IsString()) displayId_ = iter_displayId->value.GetString(); auto iter_friendlyName = _node.FindMember("friendly"); if (iter_friendlyName != _node.MemberEnd() && iter_friendlyName->value.IsString()) friendlyName_ = iter_friendlyName->value.GetString(); auto iter_state = _node.FindMember("state"); if (iter_state != _node.MemberEnd() && iter_state->value.IsString()) state_ = iter_state->value.GetString(); auto iter_userType = _node.FindMember("userType"); if (iter_userType != _node.MemberEnd() && iter_userType->value.IsString()) userType_ = iter_userType->value.GetString(); auto iter_phoneNumber = _node.FindMember("attachedPhoneNumber"); if (iter_phoneNumber != _node.MemberEnd() && iter_phoneNumber->value.IsString()) phoneNumber_ = iter_phoneNumber->value.GetString(); auto iter_flags = _node.FindMember("globalFlags"); if (iter_flags != _node.MemberEnd() && iter_flags->value.IsUint()) flags_ = iter_flags->value.GetUint(); auto iter_expressions = _node.FindMember("expressions"); if (iter_expressions != _node.MemberEnd()) { const auto & expression_node = iter_expressions->value; auto iter_large_icon = expression_node.FindMember("largeBuddyIcon"); if (iter_large_icon != expression_node.MemberEnd() && iter_large_icon->value.IsString()) largeIconId_ = iter_large_icon->value.GetString(); } if (state_ == "occupied" || state_ == "na" || state_ == "busy") state_ = "dnd"; else if (state_ == "away") state_ = "away"; else if (state_ != "offline" && state_ != "invisible") state_ = "online"; return 0; }
QueryError OnSendChat(const rapidjson::Value &p_Args, std::string *p_Result) { auto message = p_Args.FindMember("message"); auto teamChat = p_Args.FindMember("teamChat"); if (message == p_Args.MemberEnd() || !message->value.IsString()) { *p_Result = "Bad query: A \"message\" argument is required and must be a string"; return QueryError_BadQuery; } if (teamChat == p_Args.MemberEnd() || !teamChat->value.IsBool()) { *p_Result = "Bad query: A \"teamChat\" argument is required and must be a boolean"; return QueryError_BadQuery; } if(teamChat->value.GetBool()) *p_Result = Server::Chat::SendTeamMessage(message->value.GetString()); else *p_Result = Server::Chat::SendGlobalMessage(message->value.GetString()); return QueryError_Ok; }
void ArchiveMetadata::loadJson(const rapidjson::Value& metadataDoc) { rapidjson::Value::ConstMemberIterator itr = metadataDoc.FindMember("archive_name"); if (itr != metadataDoc.MemberEnd()) archiveName.assign(itr->value.GetString()); archiveFormatName.assign(metadataDoc["archive_format_name"].GetString()); archiveFormat = metadataDoc["archive_format"].GetUint64(); focusedEntry = metadataDoc["focused_entry"].GetString(); for (const auto &entryVal : metadataDoc["archive_structure"].GetArray()) { entryMetadata.push_back(ArchiveEntryMetadata::fromJson(entryVal)); } }
static inline void read_groups_array(std::vector<int> *result, const char *name, const rapidjson::Value &value) { if (const auto *m = value.FindMember(name)) { if (m->value.IsArray()) { std::transform(m->value.Begin(), m->value.End(), std::back_inserter(*result), std::bind(&rapidjson::Value::GetInt, std::placeholders::_1) ); } else { std::ostringstream str; str << name << "value must be of array type"; throw configuration_error(str.str().c_str()); } } }
QueryError OnCaptureInput(const rapidjson::Value &p_Args, std::string *p_Result) { // Get the "capture" argument auto s_CaptureValue = p_Args.FindMember("capture"); if (s_CaptureValue == p_Args.MemberEnd() || !s_CaptureValue->value.IsBool()) { *p_Result = "Bad query: A \"capture\" argument is required and must be a boolean"; return QueryError_BadQuery; } // Toggle input capture Web::Ui::ScreenLayer::CaptureInput(s_CaptureValue->value.GetBool()); return QueryError_Ok; }
std::string get_value_as_string(const rapidjson::Value& object, const char* key) { assert(object.IsObject()); const auto it = object.FindMember(key); if (it == object.MemberEnd()) { return ""; } if (it->value.IsString()) { return it->value.GetString(); } throw config_error{std::string{"Value for name '"} + key + "' must be a string."}; }
bool JSASTVariableDeclarator::Parse(const rapidjson::Value& value) { JSASTExpression::Parse(value); id_ = ParseExpression(value["id"]); const Value::Member* init = value.FindMember("init"); if (init && init->value.IsObject()) { init_ = ParseExpression(init->value); } return true; }
bool etcd_packer::unpack_base64(rapidjson::Value &json_val, const char *key, std::string &val) { rapidjson::Value::MemberIterator iter = json_val.FindMember(key); if (iter == json_val.MemberEnd()) { return false; } if (!iter->value.IsString()) { return false; } const char *base64_val = iter->value.GetString(); size_t base64_val_sz = strlen(base64_val); return 0 == util::base64_decode(val, reinterpret_cast<const unsigned char *>(base64_val), base64_val_sz); }
BlockDescriptor(const std::string &name, const rapidjson::Value &v, ModelResolver &resolver) : _name(name) { const rapidjson::Value::Member *variants = v.FindMember("variants"); if (variants && variants->value.IsObject()) { const rapidjson::Value::Member *begin = variants->value.MemberBegin(); const rapidjson::Value::Member *end = variants->value.MemberEnd(); for (auto i = begin; i < end; ++i) { if (i->value.IsObject() || i->value.IsArray()) _variants.emplace_back(i->name.GetString(), i->value, resolver); } } }
void GeoJson::extractLayer(const rapidjson::Value& _in, Layer& _out, const MapTile& _tile) { const auto& featureIter = _in.FindMember("features"); if (featureIter == _in.MemberEnd()) { logMsg("ERROR: GeoJSON missing 'features' member\n"); return; } const auto& features = featureIter->value; for (auto featureJson = features.Begin(); featureJson != features.End(); ++featureJson) { _out.features.emplace_back(); extractFeature(*featureJson, _out.features.back(), _tile); } }
bool search_contacts_response::unserialize_contacts(const rapidjson::Value& _node) { auto contacts_node = _node.FindMember("data"); if (contacts_node != _node.MemberEnd()) { if (!contacts_node->value.IsArray()) return false; contacts_data_.clear(); for (auto node = contacts_node->value.Begin(), nend = contacts_node->value.End(); node != nend; ++node) { contact_chunk c; { auto i = node->FindMember("sn"); if (i != node->MemberEnd() && i->value.IsString()) c.aimid_ = rapidjson_get_string(i->value); } { auto i = node->FindMember("stamp"); if (i != node->MemberEnd() && i->value.IsString()) c.stamp_ = rapidjson_get_string(i->value); } { auto i = node->FindMember("type"); if (i != node->MemberEnd() && i->value.IsString()) c.type_ = rapidjson_get_string(i->value); } { auto i = node->FindMember("score"); if (i != node->MemberEnd() && i->value.IsInt()) c.score_ = i->value.GetInt(); } { auto a = node->FindMember("anketa"); if (a != node->MemberEnd() && a->value.IsObject()) { const auto& sub_node = a->value; { auto i = sub_node.FindMember("firstName"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.first_name_ = rapidjson_get_string(i->value); } { auto i = sub_node.FindMember("lastName"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.last_name_ = rapidjson_get_string(i->value); } { auto i = sub_node.FindMember("nickname"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.nick_name_ = rapidjson_get_string(i->value); } { auto i = sub_node.FindMember("city"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.city_ = rapidjson_get_string(i->value); } { auto i = sub_node.FindMember("state"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.state_ = rapidjson_get_string(i->value); } { auto i = sub_node.FindMember("country"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.country_ = rapidjson_get_string(i->value); } { auto i = sub_node.FindMember("gender"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.gender_ = rapidjson_get_string(i->value); } { auto b = sub_node.FindMember("birthDate"); if (b != sub_node.MemberEnd() && b->value.IsObject()) { const auto& sub_sub_node = b->value; { auto i = sub_sub_node.FindMember("year"); if (i != sub_sub_node.MemberEnd() && i->value.IsInt()) c.birthdate_.year_ = i->value.GetInt(); } { auto i = sub_sub_node.FindMember("month"); if (i != sub_sub_node.MemberEnd() && i->value.IsInt()) c.birthdate_.month_ = i->value.GetInt(); } { auto i = sub_sub_node.FindMember("day"); if (i != sub_sub_node.MemberEnd() && i->value.IsInt()) c.birthdate_.day_ = i->value.GetInt(); } } } { auto i = sub_node.FindMember("aboutMeCut"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.about_ = rapidjson_get_string(i->value); } } //{ auto i = node->FindMember("mutualCount"); if (i != node->MemberEnd() && i->value.IsInt()) c.score_ = i->value.GetInt(); }//TODO fix name when server side is done } contacts_data_.emplace_back(std::move(c)); } } return true; }