293 lines
12 KiB
C++
293 lines
12 KiB
C++
// #pragma once
|
|
#include "tariff_cfg.h"
|
|
|
|
/// <inheritdoc/>
|
|
MemberType TariffConfiguration::IdentifyJsonMember(const char* member_name)
|
|
{
|
|
if (strcmp(member_name, "Currency") == 0)
|
|
return MemberType::Currency;
|
|
if (strcmp(member_name, "PaymentMethod") == 0)
|
|
return MemberType::PaymentMethod;
|
|
if (strcmp(member_name, "PaymentRate") == 0)
|
|
return MemberType::PaymentRate;
|
|
if (strcmp(member_name, "PaymentOption") == 0)
|
|
return MemberType::PaymentOption;
|
|
if (strcmp(member_name, "Duration") == 0)
|
|
return MemberType::Duration;
|
|
if (strcmp(member_name, "WeekDays") == 0)
|
|
return MemberType::WeekDays;
|
|
if (strcmp(member_name, "WeekDaysWorktime") == 0)
|
|
return MemberType::WeekDaysWorkTime;
|
|
if (strcmp(member_name, "SpecialDaysWorktime") == 0)
|
|
return MemberType::SpecialDaysWorktime;
|
|
if (strcmp(member_name, "SpecialDays") == 0)
|
|
return MemberType::SpecialDays;
|
|
if (strcmp(member_name, "PeriodYear") == 0)
|
|
return MemberType::PeriodYear;
|
|
else return MemberType::Unknown;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
bool TariffConfiguration::ParseJson(TariffConfiguration* tariff_cfg,
|
|
const char* json) {
|
|
try
|
|
{
|
|
if (tariff_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");
|
|
|
|
TariffCurrency Currency;
|
|
TariffDuration Duration;
|
|
TariffPaymentMethod PaymentMethod;
|
|
TariffPaymentRate PaymentOption;
|
|
TariffSpecialDaysWorktime SpecialDaysWorktime;
|
|
TariffSpecialDays SpecialDays;
|
|
TariffWeekDays WeekDays;
|
|
TariffWeekDaysWorktime WeekDaysWorktime;
|
|
TariffPaymentOption VehicleType;
|
|
TariffPeriodYear 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;
|
|
|
|
//printf(" -%s\n", mb_name);
|
|
mb_type = IdentifyJsonMember(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 (rapidjson::SizeType 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::Unknown:
|
|
break;
|
|
case MemberType::Currency:
|
|
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::PaymentMethod:
|
|
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::PaymentRate:
|
|
if (strcmp(inner_obj_name, "pra_payment_option_id") == 0)
|
|
PaymentOption.pra_payment_option_id = k->value.GetInt();
|
|
else if (strcmp(inner_obj_name, "pra_payment_unit_id") == 0)
|
|
PaymentOption.pra_payment_unit_id = k->value.GetInt();
|
|
else if (strcmp(inner_obj_name, "pra_price") == 0)
|
|
PaymentOption.pra_price = k->value.GetDouble();
|
|
break;
|
|
case MemberType::PaymentOption:
|
|
if (strcmp(inner_obj_name, "pop_id") == 0)
|
|
VehicleType.pop_id = k->value.GetInt();
|
|
else if (strcmp(inner_obj_name, "pop_label") == 0)
|
|
VehicleType.pop_label = k->value.GetString();
|
|
else if (strcmp(inner_obj_name, "pop_payment_method_id") == 0)
|
|
VehicleType.pop_payment_method_id = k->value.GetInt();
|
|
else if (strcmp(inner_obj_name, "pop_day_end_time") == 0)
|
|
VehicleType.pop_day_end_time = k->value.GetString();
|
|
else if (strcmp(inner_obj_name, "pop_day_night_end_time") == 0)
|
|
VehicleType.pop_day_night_end_time = k->value.GetString();
|
|
else if (strcmp(inner_obj_name, "pop_price_night") == 0)
|
|
VehicleType.pop_price_night = k->value.GetDouble();
|
|
break;
|
|
case MemberType::Duration:
|
|
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::SpecialDaysWorktime:
|
|
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::WeekDays:
|
|
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::WeekDaysWorkTime:
|
|
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_day_in_week_id = k->value.GetInt();
|
|
else if (strcmp(inner_obj_name, "pwd_period_day_in_week_id") == 0)
|
|
WeekDaysWorktime.pwd_period_week_day_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();
|
|
break;
|
|
case MemberType::SpecialDays:
|
|
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();
|
|
break;
|
|
case MemberType::PeriodYear:
|
|
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::Unknown:
|
|
break;
|
|
case MemberType::Currency:
|
|
tariff_cfg->Currency.push_back(Currency);
|
|
break;
|
|
case MemberType::PaymentMethod:
|
|
tariff_cfg->PaymentMethod.push_back(PaymentMethod);
|
|
break;
|
|
case MemberType::PaymentRate:
|
|
tariff_cfg->PaymentRate.push_back(PaymentOption);
|
|
break;
|
|
case MemberType::PaymentOption:
|
|
tariff_cfg->PaymentOption.push_back(VehicleType);
|
|
break;
|
|
case MemberType::Duration:
|
|
tariff_cfg->Duration.push_back(Duration);
|
|
break;
|
|
case MemberType::SpecialDaysWorktime:
|
|
tariff_cfg->SpecialDaysWorktime.push_back(SpecialDaysWorktime);
|
|
break;
|
|
case MemberType::WeekDays:
|
|
tariff_cfg->WeekDays.push_back(WeekDays);
|
|
break;
|
|
case MemberType::WeekDaysWorkTime:
|
|
tariff_cfg->WeekDaysWorktime.push_back(WeekDaysWorktime);
|
|
break;
|
|
case MemberType::SpecialDays:
|
|
tariff_cfg->SpecialDays.push_back(SpecialDays);
|
|
break;
|
|
case MemberType::PeriodYear:
|
|
tariff_cfg->YearPeriod.push_back(YearPeriod);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
catch (...) {
|
|
printf("%s\n", "General exception has occurred while parsing JSON\n");
|
|
return false;
|
|
}
|
|
}
|