added serial_bridge_utils none_or_double_from, none_or_bool_from

pull/30/head
Paul Shapiro 5 years ago
parent bb3baad2ee
commit 3743ca94d5

@ -81,6 +81,35 @@ string serial_bridge_utils::string_from_nettype(network_type nettype)
}
}
//
// Shared - Parsing - Values
optional<double> serial_bridge_utils::none_or_double_from(const boost::property_tree::ptree &json, const string &key)
{
optional<string> str = json.get_optional<string>(key);
if (str != none) {
return stod(*str); // this may throw an exception - allowing it to bubble up here
}
optional<double> dbl_orNone = json.get_optional<double>(key);
//
return dbl_orNone;
}
optional<bool> serial_bridge_utils::none_or_bool_from(const boost::property_tree::ptree &json, const string &key)
{
optional<string> str = json.get_optional<string>(key);
if (str != none) {
if (*str == "true" || *str == "1") {
return true;
} else if (*str == "false" || *str == "0") {
return false;
} else {
BOOST_THROW_EXCEPTION(logic_error("Unable to parse bool string"));
return none;
}
}
optional<bool> bool_orNone = json.get_optional<bool>(key);
//
return bool_orNone;
}
//
// Shared - Parsing - Args
bool serial_bridge_utils::parsed_json_root(const string &args_string, boost::property_tree::ptree &json_root)
{

@ -73,6 +73,9 @@ namespace serial_bridge_utils
return o.str();
}
};
optional<double> none_or_double_from(const boost::property_tree::ptree &json, const string &key);
optional<bool> none_or_bool_from(const boost::property_tree::ptree &json, const string &key);
//../
string ret_json_from_root(const boost::property_tree::ptree &root);
string error_ret_json_from_message(const string &err_msg);
string error_ret_json_from_code(int code, optional<string> err_msg);

Loading…
Cancel
Save