Fix Infinity
authorNima Aghdaii <naghdaii@fb.com>
Thu, 30 Jul 2015 05:19:54 +0000 (22:19 -0700)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Thu, 30 Jul 2015 06:22:04 +0000 (23:22 -0700)
Summary: folly only accepts "Infinity" while deserializing but writes "infinity" when serializing.
This means folly cannot deserialize what it serialized before.
If we agree on this, we could update ##fbcode/common## as well: https://fburl.com/136793901

Reviewed By: @marcinpe

Differential Revision: D2293627

folly/Conv.h
folly/test/JsonTest.cpp

index 1d1c056128072b67727e7d876e2b0119d5863bdc..05b60779c8b98a75ec284fe5603b9fb6f3bf7f5c 100644 (file)
@@ -636,7 +636,7 @@ toAppend(
   using namespace double_conversion;
   DoubleToStringConverter
     conv(DoubleToStringConverter::NO_FLAGS,
-         "infinity", "NaN", 'E',
+         "Infinity", "NaN", 'E',
          detail::kConvMaxDecimalInShortestLow,
          detail::kConvMaxDecimalInShortestHigh,
          6,   // max leading padding zeros
index 75d081662f3ff8173cd68f1412430979a6a56ca0..c54b0b54108431fa39b56a59fb0f09efdd89c9cc 100644 (file)
@@ -77,7 +77,10 @@ TEST(Json, Parse) {
   // case matters
   EXPECT_THROW(parseJson("infinity"), std::runtime_error);
   EXPECT_THROW(parseJson("inf"), std::runtime_error);
+  EXPECT_THROW(parseJson("Inf"), std::runtime_error);
+  EXPECT_THROW(parseJson("INF"), std::runtime_error);
   EXPECT_THROW(parseJson("nan"), std::runtime_error);
+  EXPECT_THROW(parseJson("NAN"), std::runtime_error);
 
   auto array = parseJson(
     "[12,false, false  , null , [12e4,32, [], 12]]");
@@ -169,6 +172,14 @@ TEST(Json, Produce) {
   // We're not allowed to have non-string keys in json.
   EXPECT_THROW(toJson(dynamic::object("abc", "xyz")(42.33, "asd")),
                std::runtime_error);
+
+  // Check Infinity/Nan
+  folly::json::serialization_opts opts;
+  opts.allow_nan_inf = true;
+  EXPECT_EQ("Infinity",
+            folly::json::serialize(parseJson("Infinity"), opts).toStdString());
+  EXPECT_EQ("NaN",
+            folly::json::serialize(parseJson("NaN"), opts).toStdString());
 }
 
 TEST(Json, JsonEscape) {