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 { ...@@ -63,11 +63,11 @@ struct Printer {
case dynamic::DOUBLE: case dynamic::DOUBLE:
if (!opts_.allow_nan_inf) { if (!opts_.allow_nan_inf) {
if (std::isnan(v.asDouble())) { if (std::isnan(v.asDouble())) {
throw json::parse_error( throw json::print_error(
"folly::toJson: JSON object value was a NaN"); "folly::toJson: JSON object value was a NaN");
} }
if (std::isinf(v.asDouble())) { if (std::isinf(v.asDouble())) {
throw json::parse_error( throw json::print_error(
"folly::toJson: JSON object value was an INF"); "folly::toJson: JSON object value was an INF");
} }
} }
...@@ -107,7 +107,7 @@ struct Printer { ...@@ -107,7 +107,7 @@ struct Printer {
private: private:
void printKV(const std::pair<const dynamic, dynamic>& p) const { void printKV(const std::pair<const dynamic, dynamic>& p) const {
if (!opts_.allow_non_string_keys && !p.first.isString()) { 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 " "folly::toJson: JSON object key was not a "
"string"); "string");
} }
......
...@@ -168,11 +168,20 @@ void escapeString( ...@@ -168,11 +168,20 @@ void escapeString(
*/ */
std::string stripComments(StringPiece jsonC); 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 { class FOLLY_EXPORT parse_error : public std::runtime_error {
public: public:
using std::runtime_error::runtime_error; 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. // may be extened in future to include offset, col, etc.
struct parse_location { struct parse_location {
uint32_t line{}; // 0-indexed uint32_t line{}; // 0-indexed
......
...@@ -26,6 +26,7 @@ using folly::parseJson; ...@@ -26,6 +26,7 @@ using folly::parseJson;
using folly::parseJsonWithMetadata; using folly::parseJsonWithMetadata;
using folly::toJson; using folly::toJson;
using folly::json::parse_error; using folly::json::parse_error;
using folly::json::print_error;
TEST(Json, Unicode) { TEST(Json, Unicode) {
auto val = parseJson(u8"\"I \u2665 UTF-8\""); auto val = parseJson(u8"\"I \u2665 UTF-8\"");
...@@ -389,7 +390,7 @@ TEST(Json, Produce) { ...@@ -389,7 +390,7 @@ TEST(Json, Produce) {
// We're not allowed to have non-string keys in json. // We're not allowed to have non-string keys in json.
EXPECT_THROW( 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 // Check Infinity/Nan
folly::json::serialization_opts opts; 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