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:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user