#include "configuration.h" /// MemberType Configuration::IdentifyJsonMember(const char* member_name) { if (strcmp(member_name, "Currency") == 0) return MemberType::CurrencyType; if (strcmp(member_name, "PaymentMethod") == 0) return MemberType::PaymentMethodType; if (strcmp(member_name, "PaymentRate") == 0) return MemberType::PaymentRateType; if (strcmp(member_name, "PaymentOption") == 0) return MemberType::PaymentOptionType; if (strcmp(member_name, "Duration") == 0) return MemberType::DurationType; //if (strcmp(member_name, "WeekDays") == 0) return MemberType::WeekDaysType; if (strcmp(member_name, "WeekDaysWorktime") == 0) return MemberType::WeekDaysWorkTimeType; if (strcmp(member_name, "SpecialDaysWorktime") == 0) return MemberType::SpecialDaysWorktimeType; if (strcmp(member_name, "SpecialDays") == 0) return MemberType::SpecialDaysType; if (strcmp(member_name, "PeriodYear") == 0) return MemberType::PeriodYearType; else return MemberType::UnknownType; } /// bool Configuration::ParseJson(Configuration* cfg, const char* json) { try { if (cfg == nullptr) { printf("TariffConfiguration pointer not set\n"); return false; } if (json == NULL) { printf("%s", "Input JSON string is NULL\n"); return false; } // Parse JSON to document Document document; document.Parse(json); // Return if parse error has been found ParseErrorCode err = document.GetParseError(); if (err != 0) { printf("%s %d (%s)\n", "Unable to parse JSON, error code:", err, GetParseError_En(err)); return false; } // Validate JSON, check if it's a JSON object printf("%s", "JSON parsing has been successful\n"); if (!document.IsObject()) { printf("%s", "Error: not a (valid) JSON object\n"); return false; } printf("%s", "Valid JSON object identified\n"); // Validate JSON, check configuration members if (!document.HasMember("Currency") || !document.HasMember("PaymentMethod") || !document.HasMember("PaymentOption") || !document.HasMember("PaymentRate") || !document.HasMember("Duration") //|| !document.HasMember("WeekDays") //|| !document.HasMember("SpecialDaysWorktime") //|| !document.HasMember("SpecialDays") ) { printf("%s", "Error: not a valid configuration JSON\n"); return false; } printf("%s", "Valid JSON configuration identified\n"); ATBCurrency Currency; ATBDuration Duration; ATBPaymentMethod PaymentMethod; ATBPaymentRate PaymentRate; ATBSpecialDaysWorktime SpecialDaysWorktime; ATBSpecialDays SpecialDays; ATBWeekDays WeekDays; ATBWeekDaysWorktime WeekDaysWorktime; ATBPaymentOption PaymentOption; ATBPeriodYear YearPeriod; MemberType mb_type; // Get all JSON object members // This code should run only once (to load JSON variables into memory) for (auto i = document.MemberBegin(); i != document.MemberEnd(); i++) { // Get name of all general members of JSON, don't print name if NULL const char* mb_name = i->name.GetString(); if (mb_name == NULL) continue; if (document[mb_name].IsString()) { QString const _mb_name(mb_name); if (_mb_name.startsWith("Project", Qt::CaseInsensitive)) { cfg->project.project = document[mb_name].GetString(); continue; } if (_mb_name.startsWith("Version", Qt::CaseInsensitive)) { cfg->project.version = document[mb_name].GetString(); continue; } if (_mb_name.startsWith("Info", Qt::CaseInsensitive)) { cfg->project.info = document[mb_name].GetString(); continue; } } // ... everything else should be an array if (!document[mb_name].IsArray()) { continue; } //printf(" -%s\n", mb_name); // Get array for each JSON object member auto mb_array = document[mb_name].GetArray(); if (mb_array.Size() <= 0) break; //Iterate over provided array for (auto j = 0; j < mb_array.Size(); j++) { // Get all inner objects, don't print name if NULL auto inner_obj = mb_array[j].GetObject(); if (inner_obj.MemberCount() <= 0) break; // Iterate over inner object JSON members for (auto k = inner_obj.MemberBegin(); k != inner_obj.MemberEnd(); k++) { // Get inner object JSON member, don't print name if NULL const char* inner_obj_name = k->name.GetString(); if (inner_obj_name == NULL) { printf("Inner object name is NULL\n"); continue; } // Identify member type mb_type = IdentifyJsonMember(mb_name); //#pragma region Get_values switch (mb_type) { case MemberType::UnknownType: break; case MemberType::CurrencyType: if (strcmp(inner_obj_name, "pcu_id") == 0) Currency.pcu_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pcu_sign") == 0) Currency.pcu_sign = k->value.GetString(); else if (strcmp(inner_obj_name, "pcu_major") == 0) Currency.pcu_major = k->value.GetString(); else if (strcmp(inner_obj_name, "pcu_minor") == 0) Currency.pcu_minor = k->value.GetString(); else if (strcmp(inner_obj_name, "pcu_active") == 0) Currency.pcu_active = k->value.GetBool(); break; case MemberType::PaymentMethodType: if (strcmp(inner_obj_name, "pme_id") == 0) PaymentMethod.pme_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pme_label") == 0) PaymentMethod.pme_label = k->value.GetString(); break; case MemberType::PaymentRateType: if (strcmp(inner_obj_name, "pra_payment_option_id") == 0) PaymentRate.pra_payment_option_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pra_payment_unit_id") == 0) PaymentRate.pra_payment_unit_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pra_price") == 0) PaymentRate.pra_price = k->value.GetDouble(); break; case MemberType::PaymentOptionType: if (strcmp(inner_obj_name, "pop_id") == 0) PaymentOption.pop_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_label") == 0) PaymentOption.pop_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pop_payment_method_id") == 0) PaymentOption.pop_payment_method_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_day_end_time") == 0) PaymentOption.pop_day_end_time = k->value.GetString(); else if (strcmp(inner_obj_name, "pop_day_night_end_time") == 0) PaymentOption.pop_day_night_end_time = k->value.GetString(); else if (strcmp(inner_obj_name, "pop_price_night") == 0) PaymentOption.pop_price_night = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_min_time") == 0) PaymentOption.pop_min_time = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_max_time") == 0) PaymentOption.pop_max_time = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_min_price") == 0) PaymentOption.pop_min_price = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_carry_over") == 0) PaymentOption.pop_carry_over = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_daily_card_price") == 0) PaymentOption.pop_daily_card_price = k->value.GetInt(); break; case MemberType::DurationType: if (strcmp(inner_obj_name, "pun_id") == 0) Duration.pun_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pun_label") == 0) Duration.pun_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pun_duration") == 0) Duration.pun_duration = k->value.GetDouble(); break; case MemberType::SpecialDaysWorktimeType: if (strcmp(inner_obj_name, "pedwt_id") == 0) SpecialDaysWorktime.pedwt_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pedwt_period_exc_day_id") == 0) SpecialDaysWorktime.pedwt_period_exc_day_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pedwt_time_from") == 0) SpecialDaysWorktime.pedwt_time_from = k->value.GetString(); else if (strcmp(inner_obj_name, "pedwt_time_to") == 0) SpecialDaysWorktime.pedwt_time_to = k->value.GetString(); else if (strcmp(inner_obj_name, "pedwt_price") == 0) SpecialDaysWorktime.pedwt_price = k->value.GetDouble(); break; /*case MemberType::WeekDaysType: if (strcmp(inner_obj_name, "pdiw_id") == 0) WeekDays.pdiw_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pdiw_label") == 0) WeekDays.pdiw_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pdiw_index") == 0) WeekDays.pdiw_index = k->value.GetInt(); break;*/ case MemberType::WeekDaysWorkTimeType: if (strcmp(inner_obj_name, "pwd_id") == 0) WeekDaysWorktime.pwd_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pwd_period_week_day_id") == 0) WeekDaysWorktime.pwd_period_week_day_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pwd_period_day_in_week_id") == 0) WeekDaysWorktime.pwd_period_day_in_week_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pwd_time_from") == 0) WeekDaysWorktime.pwd_time_from = k->value.GetString(); else if (strcmp(inner_obj_name, "pwd_time_to") == 0) WeekDaysWorktime.pwd_time_to = k->value.GetString(); case MemberType::SpecialDaysType: if (strcmp(inner_obj_name, "ped_id") == 0) SpecialDays.ped_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "ped_label") == 0) SpecialDays.ped_label = k->value.GetString(); else if (strcmp(inner_obj_name, "ped_date_start") == 0) SpecialDays.ped_date_start = k->value.GetString(); else if (strcmp(inner_obj_name, "ped_date_end") == 0) SpecialDays.ped_date_end = k->value.GetString(); else if (strcmp(inner_obj_name, "ped_period_special_day_id") == 0) SpecialDays.ped_period_special_day_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "ped_year") == 0) SpecialDays.ped_year = k->value.GetInt(); break; case MemberType::PeriodYearType: if (strcmp(inner_obj_name, "pye_id") == 0) YearPeriod.pye_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_label") == 0) YearPeriod.pye_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pye_start_month") == 0) YearPeriod.pye_start_month = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_start_day") == 0) YearPeriod.pye_start_day = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_end_month") == 0) YearPeriod.pye_end_month = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_end_day") == 0) YearPeriod.pye_end_day = k->value.GetInt(); break; default: break; } //#pragma endregion } // Push to specific list depending on member type switch (mb_type) { case MemberType::UnknownType: break; case MemberType::PaymentMethodType: cfg->PaymentMethod.insert(pair(PaymentMethod.pme_id, PaymentMethod)); break; case MemberType::PaymentRateType: cfg->PaymentRate.insert(pair(PaymentRate.pra_payment_option_id, PaymentRate)); break; case MemberType::PaymentOptionType: cfg->PaymentOption.insert(pair(PaymentOption.pop_payment_method_id, PaymentOption)); break; case MemberType::DurationType: cfg->Duration.insert(pair(Duration.pun_id, Duration)); break; case MemberType::SpecialDaysWorktimeType: cfg->SpecialDaysWorktime.insert(pair(SpecialDaysWorktime.pedwt_period_exc_day_id, SpecialDaysWorktime)); break; //case MemberType::WeekDaysType: // cfg->WeekDays.insert(pair(WeekDays.pdiw_index, WeekDays)); // break; case MemberType::WeekDaysWorkTimeType: cfg->WeekDaysWorktime.insert(pair(WeekDaysWorktime.pwd_period_day_in_week_id, WeekDaysWorktime)); break; case MemberType::SpecialDaysType: cfg->SpecialDays.insert(pair(SpecialDays.ped_id, SpecialDays)); break; case MemberType::PeriodYearType: cfg->YearPeriod.insert(pair(YearPeriod.pye_id, YearPeriod)); break; default: break; } } } return true; } catch (...) { printf("%s\n", "General exception has occurred while parsing JSON\n"); return false; } }