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
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: {
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);
}
, 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
// Replace invalid utf8 characters with U+FFFD and continue
bool skip_invalid_utf8;
+
+ // true to allow NaN or INF values
+ bool allow_nan_inf;
};
/*