Skip to content

Commit

Permalink
Add control of floating point serialization to JSON serialization
Browse files Browse the repository at this point in the history
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
  • Loading branch information
David Vickrey authored and Dave Watson committed Mar 18, 2014
1 parent c8ad790 commit 4652622
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion folly/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions folly/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
};

/*
Expand Down

0 comments on commit 4652622

Please sign in to comment.