From: Yan Wu Date: Wed, 5 Feb 2014 09:31:38 +0000 (-0800) Subject: raise error when parsing object with NaN or INF to JSON X-Git-Tag: v0.22.0~705 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=de873ac77d23d95a72a498a90ef82cade243f752;p=folly.git 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 --- diff --git a/folly/json.cpp b/folly/json.cpp index f248ef8e..28fe5f11 100644 --- a/folly/json.cpp +++ b/folly/json.cpp @@ -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); } diff --git a/folly/json.h b/folly/json.h index 527eb235..8b5ce55a 100644 --- a/folly/json.h +++ b/folly/json.h @@ -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; }; /*