Skip to content

Commit 3802215

Browse files
committed
making precision unsigned int
adding precision as settings value for StreamBuilder
1 parent 039a6e3 commit 3802215

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/lib_json/json_writer.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ std::string valueToString(UInt value) {
133133

134134
#endif // # if defined(JSON_HAS_INT64)
135135

136-
std::string valueToString(double value, bool useSpecialFloats, int precision) {
136+
std::string valueToString(double value, bool useSpecialFloats, unsigned int precision) {
137137
// Allocate a buffer that is more than large enough to store the 16 digits of
138138
// precision requested below.
139139
char buffer[32];
@@ -835,7 +835,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
835835
std::string const& colonSymbol,
836836
std::string const& nullSymbol,
837837
std::string const& endingLineFeedSymbol,
838-
bool useSpecialFloats);
838+
bool useSpecialFloats,
839+
unsigned int precision);
839840
int write(Value const& root, std::ostream* sout) override;
840841
private:
841842
void writeValue(Value const& value);
@@ -863,14 +864,16 @@ struct BuiltStyledStreamWriter : public StreamWriter
863864
bool addChildValues_ : 1;
864865
bool indented_ : 1;
865866
bool useSpecialFloats_ : 1;
867+
unsigned int precision_;
866868
};
867869
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
868870
std::string const& indentation,
869871
CommentStyle::Enum cs,
870872
std::string const& colonSymbol,
871873
std::string const& nullSymbol,
872874
std::string const& endingLineFeedSymbol,
873-
bool useSpecialFloats)
875+
bool useSpecialFloats,
876+
unsigned int precision)
874877
: rightMargin_(74)
875878
, indentation_(indentation)
876879
, cs_(cs)
@@ -880,6 +883,7 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
880883
, addChildValues_(false)
881884
, indented_(false)
882885
, useSpecialFloats_(useSpecialFloats)
886+
, precision_(precision)
883887
{
884888
}
885889
int BuiltStyledStreamWriter::write(Value const& root, std::ostream* sout)
@@ -909,7 +913,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
909913
pushValue(valueToString(value.asLargestUInt()));
910914
break;
911915
case realValue:
912-
pushValue(valueToString(value.asDouble(), useSpecialFloats_, 17));
916+
pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_));
913917
break;
914918
case stringValue:
915919
{
@@ -1124,6 +1128,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
11241128
bool eyc = settings_["enableYAMLCompatibility"].asBool();
11251129
bool dnp = settings_["dropNullPlaceholders"].asBool();
11261130
bool usf = settings_["useSpecialFloats"].asBool();
1131+
unsigned int pre = settings_["precision"].asUInt();
11271132
CommentStyle::Enum cs = CommentStyle::All;
11281133
if (cs_str == "All") {
11291134
cs = CommentStyle::All;
@@ -1142,10 +1147,11 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
11421147
if (dnp) {
11431148
nullSymbol = "";
11441149
}
1150+
if (pre > 17) pre = 17;
11451151
std::string endingLineFeedSymbol = "";
11461152
return new BuiltStyledStreamWriter(
11471153
indentation, cs,
1148-
colonSymbol, nullSymbol, endingLineFeedSymbol, usf);
1154+
colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre);
11491155
}
11501156
static void getValidWriterKeys(std::set<std::string>* valid_keys)
11511157
{
@@ -1155,6 +1161,7 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
11551161
valid_keys->insert("enableYAMLCompatibility");
11561162
valid_keys->insert("dropNullPlaceholders");
11571163
valid_keys->insert("useSpecialFloats");
1164+
valid_keys->insert("precision");
11581165
}
11591166
bool StreamWriterBuilder::validate(Json::Value* invalid) const
11601167
{
@@ -1186,6 +1193,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
11861193
(*settings)["enableYAMLCompatibility"] = false;
11871194
(*settings)["dropNullPlaceholders"] = false;
11881195
(*settings)["useSpecialFloats"] = false;
1196+
(*settings)["precision"] = 17;
11891197
//! [StreamWriterBuilderDefaults]
11901198
}
11911199

0 commit comments

Comments
 (0)