rjson: Add exception overloads

To avoid copying error message composing, as well as forcing
said code info rjson.cc.
Also helps caller to determine fault by catch type.
This commit is contained in:
Calle Wilund
2020-05-18 08:57:34 +00:00
parent 7ef50d7c71
commit 72ec525045
2 changed files with 34 additions and 6 deletions

View File

@@ -127,6 +127,21 @@ std::string print(const rjson::value& value) {
return std::string(buffer.GetString());
}
rjson::malformed_value::malformed_value(std::string_view name, const rjson::value& value)
: malformed_value(name, print(value))
{}
rjson::malformed_value::malformed_value(std::string_view name, std::string_view value)
: error(format("Malformed value {} : {}", name, value))
{}
rjson::missing_value::missing_value(std::string_view name)
// TODO: using old message here, but as pointed out.
// "parameter" is not really a JSON concept. It is a value
// missing according to (implicit) schema.
: error(format("JSON parameter {} not found", name))
{}
rjson::value copy(const rjson::value& value) {
return rjson::value(value, the_allocator);
}
@@ -171,20 +186,18 @@ rjson::value& get(rjson::value& value, std::string_view name) {
// Luckily, the variant taking a GenericValue doesn't share this bug,
// and we can create a string GenericValue without copying the string.
auto member_it = value.FindMember(rjson::value(name.data(), name.size()));
if (member_it != value.MemberEnd())
if (member_it != value.MemberEnd()) {
return member_it->value;
else {
throw rjson::error(format("JSON parameter {} not found", name));
}
throw missing_value(name);
}
const rjson::value& get(const rjson::value& value, std::string_view name) {
auto member_it = value.FindMember(rjson::value(name.data(), name.size()));
if (member_it != value.MemberEnd())
if (member_it != value.MemberEnd()) {
return member_it->value;
else {
throw rjson::error(format("JSON parameter {} not found", name));
}
throw missing_value(name);
}
rjson::value from_string(const std::string& str) {

View File

@@ -80,6 +80,21 @@ using string_buffer = rapidjson::GenericStringBuffer<encoding>;
using writer = rapidjson::Writer<string_buffer, encoding>;
using type = rapidjson::Type;
/**
* exception specializations.
*/
class malformed_value : public error {
public:
malformed_value(std::string_view name, const rjson::value& value);
malformed_value(std::string_view name, std::string_view value);
};
class missing_value : public error {
public:
missing_value(std::string_view name);
};
// Returns an object representing JSON's null
inline rjson::value null_value() {
return rjson::value(rapidjson::kNullType);