Commit de873ac7 authored by Yan Wu's avatar Yan Wu Committed by Sara Golemon

raise error when parsing object with NaN or INF to JSON

Summary:
NaN or INF is not allowed according to JSON specification.
This will cause problems when other places try to encode the output
of toJson with NaN or INF.
raise error when parsing object with NaN or INF to JSON.

Test Plan: manually test, raise error for object w/ NaN

Reviewed By: delong.j@fb.com

FB internal diff: D1143853
parent 34922793
......@@ -126,6 +126,11 @@ struct Printer {
void operator()(dynamic const& v) const {
switch (v.type()) {
case dynamic::DOUBLE:
if (!opts_.allow_nan_inf &&
(isnan(v.asDouble()) || isinf(v.asDouble()))) {
throw std::runtime_error("folly::toJson: JSON object value was a "
"NaN or INF");
}
toAppend(v.asDouble(), &out_);
break;
case dynamic::INT64: {
......@@ -751,6 +756,7 @@ fbstring toPrettyJson(dynamic const& dyn) {
void dynamic::print_as_pseudo_json(std::ostream& out) const {
json::serialization_opts opts;
opts.allow_non_string_keys = true;
opts.allow_nan_inf = true;
out << json::serialize(*this, opts);
}
......
......@@ -61,6 +61,7 @@ namespace json {
, allow_trailing_comma(false)
, sort_keys(false)
, skip_invalid_utf8(false)
, allow_nan_inf(false)
{}
// If true, keys in an object can be non-strings. (In strict
......@@ -93,6 +94,9 @@ namespace json {
// Replace invalid utf8 characters with U+FFFD and continue
bool skip_invalid_utf8;
// true to allow NaN or INF values
bool allow_nan_inf;
};
/*
......
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