Skip to content

Commit

Permalink
Adjust get_string logic, fixes #355.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Jul 15, 2020
1 parent c6cfcfe commit a01fc96
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/utils/input_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ inline Coordinates parse_coordinates(const rapidjson::Value& object,
}

inline std::string get_string(const rapidjson::Value& object, const char* key) {
assert(object.HasMember(key));
return object[key].GetString();
std::string value;
if (object.HasMember(key)) {
if (!object[key].IsString()) {
throw Exception(ERROR::INPUT, "Invalid " + std::string(key) + " value.");
}
value = object[key].GetString();
}
return value;
}

inline unsigned get_amount_size(const rapidjson::Value& json_input) {
Expand Down Expand Up @@ -411,9 +417,10 @@ Input parse(const CLArgs& cl_args) {

input.add_vehicle(vehicle);

bool has_profile = json_vehicle.HasMember("profile");
std::string current_profile =
(has_profile) ? get_string(json_vehicle, "profile") : DEFAULT_PROFILE;
std::string current_profile = get_string(json_vehicle, "profile");
if (current_profile.empty()) {
current_profile = DEFAULT_PROFILE;
}

if (common_profile.empty()) {
// First iteration only.
Expand Down Expand Up @@ -545,9 +552,10 @@ Input parse(const CLArgs& cl_args) {

input.add_vehicle(vehicle);

bool has_profile = json_vehicle.HasMember("profile");
std::string current_profile =
(has_profile) ? get_string(json_vehicle, "profile") : DEFAULT_PROFILE;
std::string current_profile = get_string(json_vehicle, "profile");
if (current_profile.empty()) {
current_profile = DEFAULT_PROFILE;
}

if (common_profile.empty()) {
// First iteration only.
Expand Down

0 comments on commit a01fc96

Please sign in to comment.