From 46526221f2c4483830b62bd13fadf7caeea4dfdb Mon Sep 17 00:00:00 2001 From: David Vickrey Date: Mon, 10 Mar 2014 15:55:41 -0700 Subject: [PATCH] Add control of floating point serialization to JSON serialization 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 | 2 +- folly/json.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/folly/json.cpp b/folly/json.cpp index 3141d1b2..24ce2b29 100644 --- a/folly/json.cpp +++ b/folly/json.cpp @@ -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(); diff --git a/folly/json.h b/folly/json.h index 8b5ce55a..3704829d 100644 --- a/folly/json.h +++ b/folly/json.h @@ -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; }; /* -- 2.34.1