@@ -133,7 +133,7 @@ std::string valueToString(UInt value) {
133
133
134
134
#endif // # if defined(JSON_HAS_INT64)
135
135
136
- std::string valueToString (double value, bool useSpecialFloats, int precision) {
136
+ std::string valueToString (double value, bool useSpecialFloats, unsigned int precision) {
137
137
// Allocate a buffer that is more than large enough to store the 16 digits of
138
138
// precision requested below.
139
139
char buffer[32 ];
@@ -835,7 +835,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
835
835
std::string const & colonSymbol,
836
836
std::string const & nullSymbol,
837
837
std::string const & endingLineFeedSymbol,
838
- bool useSpecialFloats);
838
+ bool useSpecialFloats,
839
+ unsigned int precision);
839
840
int write (Value const & root, std::ostream* sout) override ;
840
841
private:
841
842
void writeValue (Value const & value);
@@ -863,14 +864,16 @@ struct BuiltStyledStreamWriter : public StreamWriter
863
864
bool addChildValues_ : 1 ;
864
865
bool indented_ : 1 ;
865
866
bool useSpecialFloats_ : 1 ;
867
+ unsigned int precision_;
866
868
};
867
869
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
868
870
std::string const & indentation,
869
871
CommentStyle::Enum cs,
870
872
std::string const & colonSymbol,
871
873
std::string const & nullSymbol,
872
874
std::string const & endingLineFeedSymbol,
873
- bool useSpecialFloats)
875
+ bool useSpecialFloats,
876
+ unsigned int precision)
874
877
: rightMargin_(74 )
875
878
, indentation_(indentation)
876
879
, cs_(cs)
@@ -880,6 +883,7 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
880
883
, addChildValues_(false )
881
884
, indented_(false )
882
885
, useSpecialFloats_(useSpecialFloats)
886
+ , precision_(precision)
883
887
{
884
888
}
885
889
int BuiltStyledStreamWriter::write (Value const & root, std::ostream* sout)
@@ -909,7 +913,7 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
909
913
pushValue (valueToString (value.asLargestUInt ()));
910
914
break ;
911
915
case realValue:
912
- pushValue (valueToString (value.asDouble (), useSpecialFloats_, 17 ));
916
+ pushValue (valueToString (value.asDouble (), useSpecialFloats_, precision_ ));
913
917
break ;
914
918
case stringValue:
915
919
{
@@ -1124,6 +1128,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
1124
1128
bool eyc = settings_[" enableYAMLCompatibility" ].asBool ();
1125
1129
bool dnp = settings_[" dropNullPlaceholders" ].asBool ();
1126
1130
bool usf = settings_[" useSpecialFloats" ].asBool ();
1131
+ unsigned int pre = settings_[" precision" ].asUInt ();
1127
1132
CommentStyle::Enum cs = CommentStyle::All;
1128
1133
if (cs_str == " All" ) {
1129
1134
cs = CommentStyle::All;
@@ -1142,10 +1147,11 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
1142
1147
if (dnp) {
1143
1148
nullSymbol = " " ;
1144
1149
}
1150
+ if (pre > 17 ) pre = 17 ;
1145
1151
std::string endingLineFeedSymbol = " " ;
1146
1152
return new BuiltStyledStreamWriter (
1147
1153
indentation, cs,
1148
- colonSymbol, nullSymbol, endingLineFeedSymbol, usf);
1154
+ colonSymbol, nullSymbol, endingLineFeedSymbol, usf, pre );
1149
1155
}
1150
1156
static void getValidWriterKeys (std::set<std::string>* valid_keys)
1151
1157
{
@@ -1155,6 +1161,7 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
1155
1161
valid_keys->insert (" enableYAMLCompatibility" );
1156
1162
valid_keys->insert (" dropNullPlaceholders" );
1157
1163
valid_keys->insert (" useSpecialFloats" );
1164
+ valid_keys->insert (" precision" );
1158
1165
}
1159
1166
bool StreamWriterBuilder::validate (Json::Value* invalid) const
1160
1167
{
@@ -1186,6 +1193,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
1186
1193
(*settings)[" enableYAMLCompatibility" ] = false ;
1187
1194
(*settings)[" dropNullPlaceholders" ] = false ;
1188
1195
(*settings)[" useSpecialFloats" ] = false ;
1196
+ (*settings)[" precision" ] = 17 ;
1189
1197
// ! [StreamWriterBuilderDefaults]
1190
1198
}
1191
1199
0 commit comments