Commit c69aa19d authored by Aleksandr Sasha Sergeev's avatar Aleksandr Sasha Sergeev Committed by Facebook GitHub Bot

Add json::print_error and use it for serialization errors

Summary: To differentiate serialization and deserialization errors.

Reviewed By: yfeldblum

Differential Revision: D27743086

fbshipit-source-id: 8eb4fe1a1733e3effd0aa0478f59160381738db2
parent c6819f96
......@@ -63,11 +63,11 @@ struct Printer {
case dynamic::DOUBLE:
if (!opts_.allow_nan_inf) {
if (std::isnan(v.asDouble())) {
throw json::parse_error(
throw json::print_error(
"folly::toJson: JSON object value was a NaN");
}
if (std::isinf(v.asDouble())) {
throw json::parse_error(
throw json::print_error(
"folly::toJson: JSON object value was an INF");
}
}
......@@ -107,7 +107,7 @@ struct Printer {
private:
void printKV(const std::pair<const dynamic, dynamic>& p) const {
if (!opts_.allow_non_string_keys && !p.first.isString()) {
throw json::parse_error(
throw json::print_error(
"folly::toJson: JSON object key was not a "
"string");
}
......
......@@ -168,11 +168,20 @@ void escapeString(
*/
std::string stripComments(StringPiece jsonC);
// Parse error can be thrown when deserializing json (ie., converting string
// into json).
class FOLLY_EXPORT parse_error : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
// Print error can be thrown when serializing json (ie., converting json into
// string).
class FOLLY_EXPORT print_error : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
// may be extened in future to include offset, col, etc.
struct parse_location {
uint32_t line{}; // 0-indexed
......
......@@ -26,6 +26,7 @@ using folly::parseJson;
using folly::parseJsonWithMetadata;
using folly::toJson;
using folly::json::parse_error;
using folly::json::print_error;
TEST(Json, Unicode) {
auto val = parseJson(u8"\"I \u2665 UTF-8\"");
......@@ -389,7 +390,7 @@ TEST(Json, Produce) {
// We're not allowed to have non-string keys in json.
EXPECT_THROW(
toJson(dynamic::object("abc", "xyz")(42.33, "asd")), parse_error);
toJson(dynamic::object("abc", "xyz")(42.33, "asd")), print_error);
// Check Infinity/Nan
folly::json::serialization_opts opts;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment