void Pet::fromJson(nlohmann::json& val) { if(val.find("id") != val.end()) { setId(val.at("id")); } if(val.find("category") != val.end()) { if(!val["category"].is_null()) { Category newItem(Category()); newItem.fromJson(val["category"]); setCategory( newItem ); } } setName(val.at("name")); { m_PhotoUrls.clear(); nlohmann::json jsonArray; for( auto& item : val["photoUrls"] ) { m_PhotoUrls.push_back(item); } } { m_Tags.clear(); nlohmann::json jsonArray; if(val.find("tags") != val.end()) { for( auto& item : val["tags"] ) { if(item.is_null()) { m_Tags.push_back( Tag(nullptr) ); } else { Tag newItem(Tag()); newItem.fromJson(item); m_Tags.push_back( newItem ); } } } } if(val.find("status") != val.end()) { setStatus(val.at("status")); } }
void deserializeChatbotActions(const nlohmann::json& chatbot, ChatbotActionModel& chatbotActionModel) { nlohmann::json::const_iterator it; it = chatbot.find("replies"); if (it != chatbot.end() && (*it).is_object()) { deserializeReplies(*it, chatbotActionModel); } it = chatbot.find("replies_by_state_action"); if (it != chatbot.end() && (*it).is_object()) { deserializeReplyIdsByStateAndActionId(*it, chatbotActionModel); } }
void deserializeDictionaryModel(const nlohmann::json& input, DictionaryModel& dictionaryModel) { nlohmann::json::const_iterator it = input.find("entities"); if (it != input.end() && (*it).is_object()) { deserializeDictionary(*it, dictionaryModel); } }
inline std::optional<nlohmann::json::const_iterator> find_json(const nlohmann::json& json, const std::string& key) { auto it = json.find(key); if (it != std::end(json)) { return it; } return std::nullopt; }
void ExprFunction<T>::set(const nlohmann::json &j, const Tvarvec &vars) { Tconstvec consts; auto it = j.find("constants"); if (it != j.end()) for (auto i = it->begin(); i != it->end(); ++i) consts.push_back({i.key(), i.value()}); set(j.at("function"), vars, consts); }
inline nlohmann::json find_copy(const nlohmann::json& json, const std::string& key, const nlohmann::json& fallback_value) { auto it = json.find(key); if (it != std::end(json)) { return it.value(); } return fallback_value; }
void deserializeChatbotActionModel(const nlohmann::json& input, ChatbotActionModel& chatbotActionModel) { nlohmann::json::const_iterator it; it = input.find("chatbot"); if (it != input.end() && (*it).is_object()) { deserializeChatbotActions(*it, chatbotActionModel); } }
void deserializeIntentStoryModel(const nlohmann::json& input, IntentStoryModel& intentStoryModel) { nlohmann::json::const_iterator it; it = input.find("intent_story"); if (it != input.end() && (*it).is_object()) { deserializeIntentStory(*it, intentStoryModel); } }
void deserializeIntentModel(const nlohmann::json& input, const DictionaryModel& dictionaryModel, IntentModel& intentModel) { nlohmann::json::const_iterator it = input.find("intents"); if (it != input.end() && (*it).is_array()) { const nlohmann::json::array_t& intents = *it; deserializeIntents(intents, dictionaryModel, intentModel); } }
bool BrokerMessage::isValid(const nlohmann::json& jsonObj) const { auto it = jsonObj.find("command"); if (it == jsonObj.end() || !it->is_string()) { return false; } std::unordered_set<std::string> properties; if (*it == "publish") { properties = { "topics", "payload" }; } else if (*it == "subscribe") { properties = { "topics" }; } else if (*it == "unsubscribe") { properties = { "topics" }; } else { return false; } if (properties.find("topics") != properties.end()) { it = jsonObj.find("topics"); if (it == jsonObj.end() || !it->is_array()) { return false; } } if (properties.find("payload") != properties.end()) { it = jsonObj.find("payload"); if (it == jsonObj.end() || !it->is_string()) { return false; } } return true; }
void deserializeIntentStory(const nlohmann::json& story, IntentStoryModel& intentStoryModel) { nlohmann::json::const_iterator it; it = story.find("root"); if (it != story.end() && (*it).is_string()) { IntentStoryModel::VertexInfo vInfo; vInfo.stateId = *it; IntentStoryModel::StoryGraph::Vertex v = intentStoryModel.graph.addVertex(vInfo); intentStoryModel.vertexByStateId[vInfo.stateId] = v; intentStoryModel.rootStateId = vInfo.stateId; } it = story.find("graph"); if (it != story.end() && (*it).is_object()) { deserializeGraph(*it, intentStoryModel); } }
inline std::optional<T> find(const nlohmann::json& json, const std::string& key) { auto it = json.find(key); if (it != std::end(json)) { try { return it.value().get<T>(); } catch (std::exception&) { } } return std::nullopt; }
void User::fromJson(nlohmann::json& val) { if(val.find("id") != val.end()) { setId(val.at("id")); } if(val.find("username") != val.end()) { setUsername(val.at("username")); } if(val.find("firstName") != val.end()) { setFirstName(val.at("firstName")); } if(val.find("lastName") != val.end()) { setLastName(val.at("lastName")); } if(val.find("email") != val.end()) { setEmail(val.at("email")); } if(val.find("password") != val.end()) { setPassword(val.at("password")); } if(val.find("phone") != val.end()) { setPhone(val.at("phone")); } if(val.find("userStatus") != val.end()) { setUserStatus(val.at("userStatus")); } }
/* * The check_equal function determines if the labels of the datanode and the querynode are equal. * The function iterates through all the labels of the query node and compares its value to that of the datanode. * It returns true only if all the labels match; returns false otherwises. */ bool check_equal(nlohmann::json datanode, nlohmann::json querynode) { for (nlohmann::json::iterator it = querynode.begin(); it != querynode.end(); ++it) { if ((it.key() != "id") && (it.key() != "out_degree")) { if (datanode.find(it.key()) != datanode.end()) { if(datanode[it.key()].is_string()) { std::string d = datanode[it.key()], q= it.value(); if(d.find(q)== std::string::npos) return false; } else if (datanode[it.key()] != it.value()) return false; } else return false; } } return true; }
bool Contains(const nlohmann::json& json, const std::string& member) { return json.is_object() && json.find(member) != json.end(); }
void Difference::PrepareGrade(const nlohmann::json& j) { std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; std::cout << "PREPARE GRADE" << std::endl; // -------------------------------------------------------- //std::cout << "json " << j.dump(4) << std::endl; if (j.find("max_char_changes") != j.end()) { std::cout << "MAX CHAR CHANGES" << std::endl; int max_char_changes = j.value("max_char_changes", -1); assert (max_char_changes > 0); int min_char_changes = j.value("min_char_changes",0); assert (min_char_changes >= 0); assert (min_char_changes < max_char_changes); assert (total_char > 0); if (max_char_changes > total_char) { std::cout << "WARNING! max_char_changes > total_char)" << std::endl; max_char_changes = total_char; if (min_char_changes > max_char_changes) { min_char_changes = max_char_changes-1; } assert (min_char_changes >= 0); } assert (max_char_changes <= total_char); int char_changes = char_added + char_deleted; std::cout << "char_changes=" << char_changes << " min=" << min_char_changes << " max=" << max_char_changes << std::endl; int min_max_diff = max_char_changes-min_char_changes; int lower_bar = std::max(0,min_char_changes-min_max_diff); int upper_bar = max_char_changes + min_max_diff; assert (0 <= lower_bar && lower_bar <= min_char_changes && min_char_changes <= max_char_changes && max_char_changes <= upper_bar); float grade; if (char_changes < lower_bar) { std::cout << "too few char changes (zero credit)" << std::endl; messages.push_back(std::make_pair(MESSAGE_FAILURE,"ERROR! Approx " + std::to_string(char_changes) + " characters added and/or deleted. Significantly fewer character changes than allowed.")); } else if (char_changes < min_char_changes) { std::cout << "less than min char changes (partial credit)" << std::endl; float numer = min_char_changes - char_changes; float denom = min_max_diff; std::cout << "numer " << numer << " denom= " << denom << std::endl; assert (denom > 0); grade = 1 - numer/denom; messages.push_back(std::make_pair(MESSAGE_FAILURE,"ERROR! Approx " + std::to_string(char_changes) + " characters added and/or deleted. Fewer character changes than allowed.")); } else if (char_changes < max_char_changes) { messages.push_back(std::make_pair(MESSAGE_SUCCESS,"Approx " + std::to_string(char_changes) + " characters added and/or deleted. Character changes within allowed range.")); std::cout << "between min and max char changes (full credit)" << std::endl; grade = 1.0; } else if (char_changes < upper_bar) { std::cout << "more than max char changes (partial credit)" << std::endl; float numer = char_changes - max_char_changes; float denom = min_max_diff; assert (denom > 0); grade = 1 - numer/denom; std::cout << "numer " << numer << " denom= " << denom << std::endl; messages.push_back(std::make_pair(MESSAGE_FAILURE,"ERROR! Approx " + std::to_string(char_changes) + " characters added and/or deleted. More character changes than allowed.")); } else { std::cout << "too many char changes (zero credit)" << std::endl; messages.push_back(std::make_pair(MESSAGE_FAILURE,"ERROR! Approx " + std::to_string(char_changes) + " characters added and/or deleted. Significantly more character changes than allowed.")); grade = 0.0; } std::cout << "grade " << grade << std::endl; assert (grade >= -0.00001 & grade <= 1.00001); this->setGrade(grade); } // -------------------------------------------------------- else if (this->extraStudentOutputOk) { // only missing lines (deletions) are a problem int count_of_missing_lines = 0; for (int x = 0; x < this->changes.size(); x++) { int num_b_lines = this->changes[x].b_changes.size(); if (num_b_lines > 0) { count_of_missing_lines += num_b_lines; } } int output_length = this->output_length_b; std::cout << "COMPARE outputlength=" << output_length << " missinglines=" << count_of_missing_lines << std::endl; assert (count_of_missing_lines <= output_length); float grade = 1.0; if (output_length > 0) { //std::cout << "SES [ESOO] calculating grade " << this->distance << "/" << output_length << std::endl; //grade -= (this->distance / (float) output_length ); grade -= count_of_missing_lines / float(output_length); std::cout << "grade: missing_lines [ " << count_of_missing_lines << "] / output_length " << output_length << "]\n"; //std::cout << "SES [ESOO] calculated grade = " << std::setprecision(1) << std::fixed << std::setw(5) << grade << " " << std::setw(5) << (int)floor(5*grade) << std::endl; if (grade < 1.0 && this->only_whitespace_changes) { std::cout << "ONLY WHITESPACE DIFFERENCES! adjusting grade: " << grade << " -> "; // FIXME: Ugly, but with rounding, this will be only a -1 point grade for this test case grade = std::max(grade,0.99f); std::cout << grade << std::endl; } else { std::cout << "MORE THAN JUST WHITESPACE DIFFERENCES! " << std::endl; } } else { assert (output_length == 0); std::cout << "NO OUTPUT, GRADE IS ZERO" << std::endl; grade = 0; } std::cout << "this test grade = " << grade << std::endl; this->setGrade(grade); } // -------------------------------------------------------- else { // both missing lines (deletions) and extra lines are a deduction int max_output_length = std::max(this->output_length_a, this->output_length_b); float grade = 1.0; if (max_output_length == 0) { grade = 0; } else { //std::cout << "SES calculating grade " << this->distance << "/" << max_output_length << std::endl; grade -= (this->distance / (float) max_output_length ); std::cout << "grade: this->distance [ " << this->distance << "] / max_output_length " << max_output_length << "]\n"; //std::cout << "SES calculated grade = " << grade << std::endl; } this->setGrade(grade); } // =================================================== std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; }
nlohmann::json collection::query(nlohmann::json::object_t const& selector, nlohmann::json::object_t const& modifier, int flags) throw(ejdb_exception) { nlohmann::json q1 = selector; nlohmann::json q2 = modifier; bool with_modifier = !q2.empty(); if(with_modifier) { std::swap(q1, q2); } auto* ejdb_query = ejdbcreatequery(_db.get(), convert_to_bson(q1).get(), with_modifier ? convert_to_bson(q2).get() : nullptr, (int)with_modifier, nullptr); if(!ejdb_query) { throw_last_ejdb_exception(); } uint32_t count; std::shared_ptr<TCXSTR> log(tcxstrnew(), tcxstrdel); auto cursor = ejdbqryexecute(_coll.get(), ejdb_query, &count, flags, log.get()); ejdbquerydel(ejdb_query); nlohmann::json updates; nlohmann::json upserts; nlohmann::json dropall; nlohmann::json const json_log = evaluate_log(std::string(log->ptr, log->size)); if(json_log.find("updating_mode") != json_log.end() && json_log["updating_mode"]) { if(json_log.find("$update") != json_log.end()) { updates = json_log["$update"]; if(updates.type() != nlohmann::json::value_t::array) { updates = nlohmann::json::array_t({ updates }); } } if(json_log.find("$upsert") != json_log.end()) { upserts = json_log["$upsert"]; if(upserts.type() != nlohmann::json::value_t::array) { upserts = nlohmann::json::array_t({ upserts }); } for(nlohmann::json::object_t doc: upserts) { std::string const id = doc["_id"]; doc.erase("_id"); document_added(id, doc); } } if(json_log.find("$dropall") != json_log.end()) { for(std::string const& id: json_log["$dropall"]) { dropall.push_back(id); } } } nlohmann::json return_val; if(flags & JBQRYCOUNT) { return_val = count; } else { std::vector<nlohmann::json::object_t> results; results.reserve(count); for(int i = 0; i < count; ++i) { int data_size = 0; auto const& result = convert_to_json(std::shared_ptr<bson>(bson_create_from_buffer(static_cast<char const*>(ejdbqresultbsondata(cursor, i, &data_size)), data_size), bson_destroy)); if(i < updates.size()) { auto const& update = updates[i]; if(result != update) { auto const diff = modified_fields(result, update); document_pre_changed(result["_id"], result, update); document_changed(result["_id"], diff["fields"], diff["cleared"]); } } if(i < dropall.size()) { std::string const& id = dropall[i]; if(result["_id"] == id) { document_pre_removed(id, result); document_removed(id); } } results.push_back(result); } ejdbqresultdispose(cursor); return_val = results; } if(flags & JBQRYFINDONE) { return_val = return_val.empty() ? nlohmann::json::object() : return_val[0]; } return return_val; }