Add control of floating point serialization to JSON serialization
authorDavid Vickrey <davidvickrey@fb.com>
Mon, 10 Mar 2014 22:55:41 +0000 (15:55 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 18 Mar 2014 17:01:36 +0000 (10:01 -0700)
Summary: Title.  Note that this is a no-op with default parameters because folly::toAppend(double, &dest) calls folly::toAppend(double, &dest, DtoaMode, numDigits) with DtoaMode = SHORTEST and numDigits = 0.

Test Plan: Tested new functionality in D1212547.

Reviewed By: kelarini@fb.com

FB internal diff: D1212617

folly/json.cpp
folly/json.h

index 3141d1b2006bce870858a5723468707fbf8008e9..24ce2b298f982fb19b7e5d3a360065d1bd3988b6 100644 (file)
@@ -131,7 +131,7 @@ struct Printer {
         throw std::runtime_error("folly::toJson: JSON object value was a "
           "NaN or INF");
       }
-      toAppend(v.asDouble(), &out_);
+      toAppend(v.asDouble(), &out_, opts_.double_mode, opts_.double_num_digits);
       break;
     case dynamic::INT64: {
       auto intval = v.asInt();
index 8b5ce55a2f4e26d6df87b9e9a485399a32decfa4..3704829d623d9953e7b73e4427824e4866a9368c 100644 (file)
@@ -62,6 +62,8 @@ namespace json {
       , sort_keys(false)
       , skip_invalid_utf8(false)
       , allow_nan_inf(false)
+      , double_mode(double_conversion::DoubleToStringConverter::SHORTEST)
+      , double_num_digits(0) // ignored when mode is SHORTEST
     {}
 
     // If true, keys in an object can be non-strings.  (In strict
@@ -97,6 +99,11 @@ namespace json {
 
     // true to allow NaN or INF values
     bool allow_nan_inf;
+
+    // Options for how to print floating point values.  See Conv.h
+    // toAppend implementation for floating point for more info
+    double_conversion::DoubleToStringConverter::DtoaMode double_mode;
+    unsigned int double_num_digits;
   };
 
   /*